我正在尝试对两个 SQL 查询进行完全联接,如下所示:
第一个查询:
SELECT
ID
,SUM(CASE WHEN reason = 4 THEN 0 ELSE quantity*price END) AS TValue
,COUNT(*) AS CountAll
FROM table1
WHERE Date>=@StartDate AND Date<=@EndDate
GROUP BY ID
第二个查询:
SELECT
ID
,SUM(CASE WHEN reason = 1 THEN 1 ELSE 0 END) AS New
,SUM(CASE WHEN reason = 6 THEN 1 ELSE 0 END) AS Amend
,SUM(CASE WHEN reason = 5 THEN 1 ELSE 0 END) AS Cancel
FROM Table2
WHERE Date2 >=@StartDate AND Date2<= @EndDate
GROUP BY ID
查询1的结果
ID CountAll TValue
-------------------------
id1 24 1020
id2 13 2030
id3 4 120
查询 2 的结果:
ID New Amend Cancel
--------------------------------
id1 12 4 6
id2 7 6 1
id4 2 1 2
需要的输出:
ID TValue CountAll New Amend Cancel Total(countall+new+amend+cancel)
----------------------------------------------------------------------------------------
Id1 1020 24 12 4 6 46
Id2 2030 13 7 6 1 27
id3 120 4 0 0 0 4
Id4 0 0 2 1 2 5
如果需要,我会发布我当前的解决方案,但它离工作还很远。
我一直在做一些研究,我认为我需要建立一个工会来加入 ID'S,或者只是做一个完全加入。(第二天做sql)