Click here to Skip to main content
15,884,099 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
[^][^]Please find below query
SQL
select 'Closed' as 'Medium',
sum(case when Medium='Application' then 1 else 0 end) 'Application',
sum(case when Medium='E-mail' then 1 else 0 end) 'E-mail',
sum(case when Medium='Phone' then 1 else 0 end) 'Phone',
sum(case when Medium='Visit' then 1 else 0 end) 'Visit',
sum(case when Medium='Web' then 1 else 0 end) 'Web',
sum(case when Medium='Application' then 1 else 0 end)+
sum(case when Medium='E-mail' then 1 else 0 end)+
sum(case when Medium='Phone' then 1 else 0 end)+
sum(case when Medium='Visit' then 1 else 0 end)+
sum(case when Medium='Web' then 1 else 0 end) as 'Total'
from im_ticketmaster where status='Closed'
union 
select 'Resolved' as 'Medium',
sum(case when Medium='Application' then 1 else 0 end) 'Application',
sum(case when Medium='E-mail' then 1 else 0 end) 'E-mail',
sum(case when Medium='Phone' then 1 else 0 end) 'Phone',
sum(case when Medium='Visit' then 1 else 0 end) 'Visit',
sum(case when Medium='Web' then 1 else 0 end) 'Web',
sum(case when Medium='Application' then 1 else 0 end)+
sum(case when Medium='E-mail' then 1 else 0 end)+
sum(case when Medium='Phone' then 1 else 0 end)+
sum(case when Medium='Visit' then 1 else 0 end)+
sum(case when Medium='Web' then 1 else 0 end) as 'Total'
from im_ticketmaster where status='Resolved'
union
select 'Pending' as 'Medium',
sum(case when Medium='Application' then 1 else 0 end) 'Application',
sum(case when Medium='E-mail' then 1 else 0 end) 'E-mail',
sum(case when Medium='Phone' then 1 else 0 end) 'Phone',
sum(case when Medium='Visit' then 1 else 0 end) 'Visit',
sum(case when Medium='Web' then 1 else 0 end) 'Web',
sum(case when Medium='Application' then 1 else 0 end)+
sum(case when Medium='E-mail' then 1 else 0 end)+
sum(case when Medium='Phone' then 1 else 0 end)+
sum(case when Medium='Visit' then 1 else 0 end)+
sum(case when Medium='Web' then 1 else 0 end) as 'Total'
from im_ticketmaster where status='Pending'

for the above query output is
Medium  Application     E-mail  Phone   Visit   Web     Total
Closed	        0       0       0       0       1       1
Pending	        0       0       2       0       4       6
Resolved        2       0       36      0       489     527

but need the output like below
Medium  Application     E-mail  Phone   Visit   Web     Total
Closed	        0       0       0       0       1       1
Pending	        0       0       2       0       4       6
Resolved        2       0       36      0       489     527
Grand Total     2       0       38      0       494	
Posted
Updated 3-May-15 21:31pm
v2
Comments
Tomas Takac 4-May-15 3:31am    
Why to do all this in SQL? This is clearly a task for your reporting tool.

1 solution

You need to pivot[^] data!

Try this:
SQL
SELECT [status], [Application], [E-mail], [Phone], [Visit], [Web], [Application] + [E-mail] + [Phone] + [Visit] + [Web] AS [Total]
FROM (
    SELECT [status], Medium
    FROM im_ticketmaster
    UNION ALL
    SELECT 'Total' AS [status], Medium
    FROM im_ticketmaster
    ) AS DT
PIVOT(COUNT([Medium]) FOR [status] IN ( [Application], [E-mail], [Phone], [Visit], [Web])) AS PT


More at CP KnowlegdeBase:
Simple Way To Use Pivot In SQL Query[^]
Pivoting DataTable Simplified[^]
C# Pivot Table[^]
Working with Pivot and UnPivot Transformation[^]
How to Perform Pivot Operation in T-SQL..?[^]
Simple & Advanced Pivots with C# and ASP.NET[^]
SQL - Pivot with Grand Total Column and Row[^]
 
Share this answer
 
v2
Comments
Sascha Lefèvre 4-May-15 5:06am    
+5!

CP KnowlegdeBase ;-)
Maciej Los 4-May-15 5:32am    
;) Thank you, Sascha ;)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900