加入两个结果集,在T-SQL中创建一个结果集

时间:2010-05-27 21:38:23

标签: tsql

将两个结果集合并到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 个答案:

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