Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 SQL query for grouping and join

Author  Topic 

ishitab
Starting Member

1 Post

Posted - 2014-08-22 : 09:59:32

Hi,
I am new to SQL. I have below data in one of my table:

Calls|Status
4|-6
12|-11
1|0
1|-10

Desired Output required :
Total_calls|Zero|Non-Zero
18|1|17


Please help.

gbritton
Master Smack Fu Yak Hacker

2780 Posts

Posted - 2014-08-22 : 10:16:36
What have you tried so far? (I think you should give it a try first!)

Hint: use the SUM() function like this:


SELECT SUM(...), SUM(...) ...etc
FROM myTable
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2014-08-22 : 11:52:39
SELECT SUM(Calls) AS [Total_calls], SUM(CASE WHEN Status = 0 THEN 1 ELSE 0 END) AS Zero, SUM(CASE WHEN Status = 0 THEN 0 ELSE 1 END) AS [Non-zero]
FROM dbo.Table1


Microsoft SQL Server MVP, MCT, MCSE, MCSA, MCP, MCITP, MCTS, MCDBA
Go to Top of Page

gbritton
Master Smack Fu Yak Hacker

2780 Posts

Posted - 2014-08-22 : 12:57:55
Hey Peter! You should have let him give it a try!
Go to Top of Page
   

- Advertisement -