完全加入两个查询

时间:2012-10-12 01:18:19

标签: sql sql-server-2008

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

来自query1的结果

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',或者只是做一个完全加入。 (第二天做sql)

3 个答案:

答案 0 :(得分: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

答案 1 :(得分: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 

答案 2 :(得分:0)

我会写下面的内容:

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