0

I have following table structure

TicketID    Status  Duration
-----------------------------
1234           8        2
1233           8       10
1232           4        5
1231           8       12
1230           4       50
  • status 8 means Closed
  • status 4 means Open

It is required to have output in following way. Please do the need ful. If possible I wanted it in a single sql query.

Please help me to produce output in following way.

Status  Closed (sum)    Open(Sum)
---------------------------------
   8       24              0
   4        0             55
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • 3
    Welcome to StackOverflow: if you post code, XML or data samples, **please** highlight those lines in the text editor and click on the "code samples" button ( `{ }` ) on the editor toolbar to nicely format and syntax highlight it! – marc_s Dec 04 '14 at 06:52
  • 1
    Did you try anything at all ? Read http://msdn.microsoft.com/en-us/library/ms177673.aspx and http://msdn.microsoft.com/en-us/library/ms177673.aspx – Mahesh Dec 04 '14 at 06:56
  • @SandipkTatva You might be looking for `GROUP BY` (links posted in the comments above), or `PIVOT` if your actual case is more complex than what you posted (i.e. there are more statuses in reality, etc) http://technet.microsoft.com/en-us/library/ms177410%28v=sql.105%29.aspx and http://stackoverflow.com/questions/24470/sql-server-pivot-examples – Ruslan Dec 04 '14 at 07:45

4 Answers4

0

A case expression could filter out the unwanted values, so this can be done in a single aggregate query:

SELECT   status, 
         SUM (CASE status WHEN 8 THEN duration ELSE 0 END) AS closed_sum,
         SUM (CASE status WHEN 4 THEN duration ELSE 0 END) AS open_sum
FROM     my_table
GROUP BY status
Mureinik
  • 297,002
  • 52
  • 306
  • 350
0
select status
       , sum( case when status = 4 then Duration else 0 end ) as [Open(sum)]
       , sum( case when status = 8 then Duration else 0 end ) as [Closed(sum)]
from @test
group by status
order by status desc
Deepshikha
  • 9,896
  • 2
  • 21
  • 21
0
WITH T AS (SELECT Status, SUM(Duration) AS Duration FROM Table1 GROUP BY Status)
SELECT Status, Duration, 0 FROM T WHERE Status = 8
UNION ALL
SELECT Status, 0, Duration FROM T WHERE Status = 4
CrimsonKing
  • 2,696
  • 1
  • 14
  • 11
  • To produce the EXACT expected output. I am aware that the WITH part gives all the information needed. The UNION part is to produce the EXPECTED output in the question. See the format in the question. – CrimsonKing Dec 04 '14 at 07:56
0

use union all

    SELECT [Status],sum([Duration]) [Closed(sum)], '0' as   [Open(Sum)]
             from [dbo].[tblx] 
             where [Status]=8
             GROUP BY [Status] 


UNION all            
              SELECT [Status], '0' as [Closed(sum)],sum([Duration]) [Close(sum)]
             from [dbo].[tblx] 
             WHERE [Status]=4
             GROUP BY [Status] 

enter image description here

Dudi Konfino
  • 1,126
  • 2
  • 13
  • 24