Click here to Skip to main content
15,899,475 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi
I wrote the below query to get total count of particular user to produce the production report

My Sql Query is:

SELECT distinct(resources), work_area, scope,
CASE
WHEN (scope = 'DataEntry' and work_area='Coding') THEN image_count
WHEN (scope = 'DataEntry' and work_area='QC') THEN image_count/4
WHEN (scope='Technical' ) THEN sum(convert(int,[duration]*6.25))
ELSE ''
END 'Total'
FROM work where DATEPART(MM,dat_e)='07' and DATEPART(YEAR,dat_e)='2016' Group BY resources, scope, image_count, duration, work_area;


and I am getting results like below:


HTML
resources	work_area	scope	  Total
John	     Other	   Technical	65
John	     QA	       Technical	150
Mary	     Other	   Technical    40
Rita	     Coding	   DataEntry	800
Rita	     Coding	   DataEntry	900
Rita	     Coding	   DataEntry	950
Rita	     Coding	   DataEntry	700
Rita	     QC	       DataEntry	800
Rita	     Coding	   DataEntry	850
Rita	     QC	       DataEntry	700
sharo	     Coding	   DataEntry	900
sharo	     Other	   Technical    200
sharo	     Coding	   DataEntry	800



and I want the output like below

HTML
resources Technical DataEntry Total
John	   215		           215
Mary	   40		           40
Rita	               5700	   5700
Sharo	   200	       1700	   1900


Can anyone please help me to resolve this issue

What I have tried:

I have tried to transpose the data into rows but i am not able to get the desired result can anyone plase help me on this.
Posted
Updated 17-Jul-16 18:20pm
v2
Comments
[no name] 16-Jul-16 11:49am    
You request something more than just only transpose. Have a look to this CP article: SQL - Pivot with Grand Total Column and Row[^].
Probably you will also find Solutions without the Need of stored procedure.
Kornfeld Eliyahu Peter 17-Jul-16 10:27am    
Check this too: https://msdn.microsoft.com/en-us/library/ms177410.aspx

Option 1: Use Pivot as 0x01AA suggested.
Option 2: Use JOINS against the same table to slice the data the way you want it.
Based on what you have provided, the below should work.
SQL
SELECT 
    a.Resources,
    COALESCE(SUM(CONVERT(INT, d.duration*6.25)), 0) AS Technical,
    COALESCE(SUM(b.image_count), 0) + (COALESCE(SUM(c.image_Count), 0)/4) AS DataEntry,
    COALESCE(SUM(CONVERT(INT, d.duration*6.25)), 0) + COALESCE(SUM(b.image_count), 0) +
    (COALESCE(SUM(c.image_Count), 0)/4) AS Total
FROM 
    work a
    LEFT JOIN work d ON a.resources = d.resources AND d.scope = 'Technical' AND YEAR(d.dat_e) = '2016' AND MONTH(d.dat_e) = '7'
    LEFT JOIN work b ON a.resources = b.resources AND b.scope = 'DataEntry' AND b.work_area = 'Coding' AND YEAR(b.dat_e) = '2016' AND MONTH(b.dat_e) = '7'
    LEFT JOIN work c ON a.resources = c.resources AND c.scope = 'Technical' AND c._work_area = 'QC' AND YEAR(c.dat_e) = '2016' AND MONTH(c.dat_e) = '7'
GROUP BY
    a.Resources

Kind Regards
 
Share this answer
 
v2
Comments
SukirtiShetty 18-Jul-16 0:29am    
Hithank you for providing answer
Query worked for me except the Total

If any values is 'NULL' then Total also showing 'NULL' its not clculating other values from different scopes

now it displays results like this it calculates sum only for the entries in all the fields


resources Technical DataEntry Total
John 215 NULL NULL
Mary 40 NULL NULL
Rita NULL 5700 NULL
Sharo 200 1700 1900
an0ther1 18-Jul-16 17:38pm    
Try the updated solution - this should be a bit more robust

Kind Regards
SukirtiShetty 19-Jul-16 0:01am    
thank you so much. It worked fine for me...
Take a look at this link hope this will help you out:
sql server - Simple way to transpose columns and rows in Sql? - Stack Overflow[^]
 
Share this answer
 

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