2010-05-27 114 views
0

將兩個結果集合併到T-SQL中的一個結果集中的最佳方法是什麼?加入兩個結果集以在T-SQL中創建一個結果集

SQL statment#1:

SELECT 
    COUNT(t.col1) as 'Number of Responses', 
    t.col2 as 'Department' 
FROM table t 
WHERE col3 IS NOT NULL 
GROUP BY t.col1 
ORDER BY t.col1 

SQL Statment#1:

SELECT 
    COUNT(t.col1) as 'Total number of participants', 
    t.col2 as 'Department' 
FROM table t 
GROUP BY t.col1 ORDER by t.col1 

所需的結果集

答覆數|參與者總數爲 |部門

回答

1
SELECT 
    SUM(case when t.col3 is not null then 1 else 0 end) 'Number of Responses', 
    COUNT(t.col1) as 'Total number of participants', 
    t.col2 as 'Department' 
FROM table t 
GROUP BY t.col1 
ORDER BY t.col1