2

我正在尝试对两个 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)

4

3 回答 3

2

试试这个,

SELECT *
FROM
(
    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
) a FULL JOIN
(
    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
) b ON a.ID = b.ID
于 2012-10-12T01:23:34.907 回答
1

我会写如下内容:

select decode (a.id, null, b.id, a.id) as ID, a.TValue, CountAll, b.new, b.Amend, b.cancel 
   from (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
        ) a FULL OUTER JOIN
        (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
        ) b
     on a.id = b.id
于 2012-10-12T01:28:43.870 回答
1

你试过这个...

select isnull (a.id,b.id) as ID, a.TValue, CountAll, b.new, b.Amend, b.cancel   
from (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         ) a 
                FULL OUTER JOIN (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         ) b  on a.id = b.id 
于 2012-10-12T06:16:05.150 回答