这次问题完整了,本想把问题简化,自己再做修正的,结果 。。。
还是把问题完整提出来吧,感谢在我先前帖子里回复的老师(http://bbs.csdn.net/topics/390931139)! 其中已收款也有可能分多次收进的
表a: 应收款,表b:已收款,要求查询未收款(即同一ID,应收款、已收款差值不是0的),其中税款不包括在应收款内
表a:
ID 应收明细 应收金额
1 税款 10000.00
1 服务费 500.00
1 交通费 200.00
1 材料费 1000.00
2 服务费 400.00
3 服务费 500.00
3 交通费 300.00
表b:
ID 已收金额
1 700.00
1 300.00
2 400.00
3 300.00
3 200.00
表c
ID NAME
1 张三
2 李四
3 王五
要求得到:
ID 应收总额 已收金额 未收金额
张三 1700 1000 700
王五 800 500 300
------解决思路----------------------
----------------------------------------------------------------
-- Author :DBA_HuangZJ(發糞塗牆)
-- Date :2014-11-12 15:00:16
-- Version:
-- Microsoft SQL Server 2012 (SP1) - 11.0.3153.0 (X64)
-- Jul 22 2014 15:26:36
-- Copyright (c) Microsoft Corporation
-- Enterprise Edition (64-bit) on Windows NT 6.2 <X64> (Build 9200: ) (Hypervisor)
--
----------------------------------------------------------------
--> 测试数据:[A]
if object_id('[A]') is not null drop table [A]
go
create table [A]([ID] int,[应收明细] varchar(6),[应收金额] numeric(7,2))
insert [A]
select 1,'税款',10000.00 union all
select 1,'服务费',500.00 union all
select 1,'交通费',200.00 union all
select 1,'材料费',1000.00 union all
select 2,'服务费',400.00 union all
select 3,'服务费',500.00 union all
select 3,'交通费',300.00
--> 测试数据:[B]
if object_id('[B]') is not null drop table [B]
go
create table [B]([ID] int,[已收金额] numeric(6,2))
insert [B]
select 1,1000.00 union all
select 2,400.00 union all
select 3,500.00
--> 测试数据:[C]
if object_id('[C]') is not null drop table [C]
go
create table [C]([ID] int,[NAME] varchar(4))
insert [C]
select 1,'张三' union all
select 2,'李四' union all
select 3,'王五'
--------------开始查询--------------------------
select c.NAME,sum(应收金额)应收金额,已收金额,sum(应收金额)-已收金额 as 未收金额
from [A] left join [B] on a.id=b.id
INNER JOIN C ON A.ID=C.ID
where a.应收明细<>'税款'
group by C.NAME,已收金额
having sum(应收金额)-已收金额<>0
----------------结果----------------------------
/*
NAME 应收金额 已收金额 未收金额
---- --------------------------------------- --------------------------------------- ---------------------------------------
王五 800.00 500.00 300.00
张三 1700.00 1000.00 700.00
*/
------解决思路----------------------
你试下
SELECT T1.ID,T3.Name,T1.[应收总额],ISNULL(T2.[已收金额],0)[已收金额],T1.[应收总额]-ISNULL(T2.[已收金额],0) [未收金额]
FROM(SELECT ID,SUM(应收金额)[应收总额] FROM A WHERE T1.应收明细<>'税款'GROUP BY ID)T1
LEFT JOIN(SELECT ID,SUM(已收金额)[已收金额] FROM B GROUP BY ID)T2 ON T1.ID=T2.ID
LEFT JOIN C T3 ON T1.ID=T3.ID
WHERE T1.[应收总额]<>ISNULL(T2.[已收金额],0)
------解决思路----------------------
WITH a(ID,应收明细,应收金额) AS (
SELECT 1,'税款',10000.00 UNION ALL
SELECT 1,'服务费',500.00 UNION ALL
SELECT 1,'交通费',200.00 UNION ALL
SELECT 1,'材料费',1000.00 UNION ALL
SELECT 2,'服务费',400.00 UNION ALL
SELECT 3,'服务费',500.00 UNION ALL
SELECT 3,'交通费',300.00
)
,b(ID,已收金额) AS (
SELECT 1,700.00 UNION ALL
SELECT 1,300.00 UNION ALL
SELECT 2,400.00 UNION ALL
SELECT 3,300.00 UNION ALL
SELECT 3,200.00
)
,c(ID,NAME) AS (
SELECT 1,'张三' UNION ALL
SELECT 2,'李四' UNION ALL
SELECT 3,'王五'
)
SELECT c.NAME,
ISNULL(ta.应收总额,0) AS 应收总额,
ISNULL(tb.已收总额,0) AS 已收总额,
ISNULL(ta.应收总额,0) - ISNULL(tb.已收总额,0) AS 未收金额
FROM c
LEFT JOIN (
SELECT ID,SUM(应收金额) AS 应收总额
FROM a
WHERE 应收明细<>'税款'
GROUP BY ID
) ta
ON c.ID = ta.ID
LEFT JOIN (
SELECT ID,SUM(已收金额) AS 已收总额
FROM b
GROUP BY ID
) tb
ON c.ID = tb.ID
WHERE (ISNULL(ta.应收总额,0) - ISNULL(tb.已收总额,0)) <> 0
NAME 应收总额 已收总额 未收金额
---- ------------------ ---------------- --------------
张三 1700.00 1000.00 700.00
王五 800.00 500.00 300.00
------解决思路----------------------
从数据库来讲,分组汇总后就没有具体的明细。
从界面上来讲:
A)报表控件可以自动分组显示明细,并且对分组进行汇总。只需要查询明细就可以了。
B)表格控件通常没有汇总功能,不过有合并单元格的功能。你可以把汇总子查询和明细再JOIN起来,这样每条记录都有汇总数,就能合并单元格了。