Michael Kniskern
Michael Kniskern

Reputation: 25260

Join two results sets to make one result set in T-SQL

What would be the best approach to combine the two results sets in one result set in 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

Desired result set

Number of Responses | Total Number of participants | Department

Upvotes: 0

Views: 529

Answers (1)

Philip Kelley
Philip Kelley

Reputation: 40289

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 

Upvotes: 1

Related Questions