日期:2014-05-16 浏览次数:20417 次
----------------------------------------------------------------
-- Author :fredrickhu(小F,向高手学习)
-- Date :2014-03-29 17:28:11
-- Verstion:
-- Microsoft SQL Server 2008 (RTM) - 10.0.1600.22 (Intel X86)
-- Jul 9 2008 14:43:34
-- Copyright (c) 1988-2008 Microsoft Corporation
-- Enterprise Edition on Windows NT 6.1 <X86> (Build 7601: Service Pack 1)
--
----------------------------------------------------------------
--> 测试数据:[T1]
if object_id('[T1]') is not null drop table [T1]
go
create table [T1]([合同] varchar(3),[客户] varchar(4),[金额] int,[合同日期] datetime)
insert [T1]
select '001','王五',100,'2013-1-1' union all
select '002','李四',300,'2014-3-20' union all
select '003','张三',200,'2014-3-29'
--> 测试数据:[T2]
if object_id('[T2]') is not null drop table [T2]
go
create table [T2]([合同] varchar(3),[客户] varchar(4),[操作方式] varchar(4),[付款] int,[日期] datetime)
insert [T2]
select '001','王五','已付',20,'2013-1-3' union all
select '002','李四','已付',40,'2014-3-20' union all
select '002','李四','已付',30,'2014-3-27' union all
select '002','李四','已付',10,'2014-3-28' union all
select '003','张三','已付',10,'2014-3-29'
--------------开始查询--------------------------
SELECT DISTINCT
'呈前' AS 合同, '' AS 客户, (SELECT
SUM(金额) AS 金额
&n