我有以下的表格設置:更換循環查詢
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列中會有一個日期。如果發票從未付款,則它將爲空。
我曾經想過這樣做的唯一方法是通過一個循環,我只是一次將一個AppDate傳遞給WHERE
子句。但我希望我可以做到無效。
任何幫助,將不勝感激。
你如何計算總金額?我無法跟隨。抱歉。 –