1

I have table like this

ID Specified TIN
-----------------
1       0   tin1   
2       0   tin1  
3       1   tin1    
4       0   tin2  
5       0   tin3   
6       1   tin3 
7       1   tin3  

I need to count rows groupped by TIN, Specified columns - but result should one row for each TIN:

TIN   ZEROSpecified   NOTZEROSpecified
tin1       2                 1
tin2       0                 1
tin3       1                 2

Important notice - i have only 2 values for Specified column - 0 and 1

2 Answers 2

6
SELECT TIN, 
SUM(case when Specified=0 then 1 else 0 end) as ZeroSpecified,
SUM(case when Specified<>0 then 1 else 0 end) as NOTZEROSpecified
FROM table
GROUP BY TIN
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you my friend! i forgot about sum function)
You need to edit the second case, this isn't correct; 'Specified<>1'
1

Pretty Simple;

SELECT
TIN
,SUM(CASE WHEN Specified = 0 THEN 1 ELSE 0 END) ZEROSpecified
,SUM(CASE WHEN Specified <> 0 THEN 1 ELSE 0 END) NotZEROSpecified
FROM TableName
GROUP BY TIN

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.