我设置了以下表格:
CREATE TABLE app_detail
(
CustID1 int,
CustID2 int,
AppDate datetime
)
CREATE TABLE inv_detail
(
CustID1 int,
CustID2 int,
PostDate datetime,
ClearDate datetime,
Amt float
)
INSERT INTO app_detail
VALUES(583,246,'2013-04-30 00:00:00.000')
INSERT INTO app_detail
VALUES(583,246,'2015-06-17 00:00:00.000')
INSERT INTO inv_detail
VALUES(583,246,'2013-04-05 00:00:00.000',NULL,17.56)
INSERT INTO inv_detail
VALUES(583,246,'2013-04-05 00:00:00.000','2013-12-31 00:00:00.000',667.97)
INSERT INTO inv_detail
VALUES(583,246,'2013-04-05 00:00:00.000','2014-10-05 00:00:00.000',3.96)
INSERT INTO inv_detail
VALUES(583,246,'2013-04-05 00:00:00.000',NULL,48.40)
INSERT INTO inv_detail
VALUES(583,246,'2013-01-08 00:00:00.000','2013-12-31 00:00:00.000',332.03)
INSERT INTO inv_detail
VALUES(583,246,'2013-01-08 00:00:00.000','2013-12-31 00:00:00.000',63.10)
INSERT INTO inv_detail
VALUES(583,246,'2013-07-09 00:00:00.000',NULL,1062.29)
INSERT INTO inv_detail
VALUES(583,246,'2013-04-05 00:00:00.000',NULL,17.56)
INSERT INTO inv_detail
VALUES(583,246,'2013-04-05 00:00:00.000',NULL,48.40)
select * from app_detail
select * from inv_detail
我想得到以下输出:
SELECT
'583' AS CustID1
,'246' AS CustID2
,'2013-04-30 00:00:00.000' AS AppDate
,'1133.02' AS TotalAmount
UNION
SELECT
'583'
,'246'
,'2015-06-17 00:00:00.000'
,'1128.25'
CustID1 CustID2 AppDate TotalAmount
583 246 2013-04-30 1133.02
583 246 2015-06-17 1128.25
第一个表包含有关在特定日期创建应用程序的客户的数据。 第二个表格包含这些客户的发票详细信息,发票发送时间和付款(清算)时间。 我想知道客户在申请日期尚未结清的发票金额。 如果已支付发票,则ClearedDate列中将有一个日期。如果从未支付发票,则它将为NULL。
我想到的唯一方法是通过一个循环,我一次只将一个AppDate传递给WHERE
子句。但是我希望我可以取消它。
任何帮助都将不胜感激。
答案 0 :(得分:2)
您可以使用outer apply
而不是手动循环执行此操作:
select a.*, due.Total
from app_detail a
outer apply (
select sum(Amt) [Total]
from inv_detail i
where
i.CustID1=a.CustID1 and i.CustID2=a.CustID2 and
i.PostDate <= a.AppDate and (i.ClearDate is null or i.ClearDate > a.AppDate)
) due
在我从测试数据中删除了两个重复的行后,它返回了正确的结果(17.56
和48.40
都有两个条目。)
这不是一种特别有效的方法 - 它会导致inv_detail
中每行app_detail
的表扫描(或可能是索引扫描,如果有适当的索引)。但是,在这种情况下,我不相信有这种方法 - 它不是一个简单的聚合,因为来自inv_detail
的一行可能涉及到许多行的计算app_detail
。
答案 1 :(得分:2)
借用Blorgbeard的回答来说明如何在没有子查询的情况下完成这项工作。
DECLARE @app_detail TABLE (CustID1 int,CustID2 int,AppDate datetime)
DECLARE @inv_detail TABLE(CustID1 int,CustID2 int,PostDate datetime,ClearDate datetime,Amt float)
INSERT INTO @app_detail VALUES(583,246,'2013-04-30 00:00:00.000')
INSERT INTO @app_detail VALUES(583,246,'2015-06-17 00:00:00.000')
INSERT INTO @inv_detail VALUES(583,246,'2013-04-05 00:00:00.000',NULL,17.56)
INSERT INTO @inv_detail VALUES(583,246,'2013-04-05 00:00:00.000','2013-12-31 00:00:00.000',667.97)
INSERT INTO @inv_detail VALUES(583,246,'2013-04-05 00:00:00.000','2014-10-05 00:00:00.000',3.96)
INSERT INTO @inv_detail VALUES(583,246,'2013-04-05 00:00:00.000',NULL,48.40)
INSERT INTO @inv_detail VALUES(583,246,'2013-01-08 00:00:00.000','2013-12-31 00:00:00.000',332.03)
INSERT INTO @inv_detail VALUES(583,246,'2013-01-08 00:00:00.000','2013-12-31 00:00:00.000',63.10)
INSERT INTO @inv_detail VALUES(583,246,'2013-07-09 00:00:00.000',NULL,1062.29)
INSERT INTO @inv_detail VALUES(583,246,'2013-04-05 00:00:00.000',NULL,17.56)
INSERT INTO @inv_detail VALUES(583,246,'2013-04-05 00:00:00.000',NULL,48.40)
select a.*, sum(Amt) AS [Total]
from @app_detail a
LEFT JOIN @inv_detail i ON i.CustID1=a.CustID1
AND i.CustID2=a.CustID2
AND i.PostDate <= a.AppDate
AND (i.ClearDate is null or i.ClearDate > a.AppDate)
GROUP BY a.CustID1,a.CustID2,a.AppDate