Need help in getting Average using SQL Cross Tab

Please support our MS SQL advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Dec 2008
Posts: 19
Reputation: dfs3000my is an unknown quantity at this point 
Solved Threads: 0
dfs3000my dfs3000my is offline Offline
Newbie Poster

Need help in getting Average using SQL Cross Tab

 
0
  #1
Dec 23rd, 2008
Hi all,

I have managed to get my average using cross tabs but only if I have grouped it by their categories. If I remove the GROUP BY clause, the cross tabbing doesn't seem to work. Can anyone help me? I would need to get something like:-

  1. Year | Month | Benefits Rating | Facilities Rating
  2. -----------------------------------------------------
  3. 2007 | December | 2 | 3
  4. 2008 | November | 3 | 3
  5. 2008 | October | 2 | 1
  6. 2008 | September| 2 | 2

Thanks

p/s: example is shown as an image below.

Click image for larger version

Name:	sql.PNG
Views:	4
Size:	16.7 KB
ID:	8641
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 42
Reputation: huangzhi is an unknown quantity at this point 
Solved Threads: 13
huangzhi huangzhi is offline Offline
Light Poster

Re: Need help in getting Average using SQL Cross Tab

 
0
  #2
Dec 27th, 2008
Make it subquery like below:

  1. SELECT [Year], [Month], sum([Benefits Rating]) AS [Benefits Rating], sum([Facilities Rating]) AS [Facilities Rating]
  2. FROM (...... your origin query ......) X
  3. GROUP BY [Year], [Month]
Hence Wijaya
www.ex-Soft.tk
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 19
Reputation: dfs3000my is an unknown quantity at this point 
Solved Threads: 0
dfs3000my dfs3000my is offline Offline
Newbie Poster

Re: Need help in getting Average using SQL Cross Tab

 
0
  #3
Dec 30th, 2008
Hi,

I've tried removing the question.category_id from the GROUP BY clause but AVG() doesn't seem to work. It works perfectly fine for SUM() but not AVG().

Below is how the output looks without the question.category_id in the GROUP BY clause.

Attachment 8701
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 19
Reputation: dfs3000my is an unknown quantity at this point 
Solved Threads: 0
dfs3000my dfs3000my is offline Offline
Newbie Poster

Re: Need help in getting Average using SQL Cross Tab

 
0
  #4
Dec 30th, 2008
I have managed to find a solution to this problem

  1. SELECT DATEPART(year, q.attempted_date) AS Year, DATENAME(MM, q.attempted_date) AS Month,
  2. AVG(CASE WHEN w.category_id = 1 THEN CAST(SUBSTRING(a.answer, 4, 1) AS int) ELSE NULL END) AS [Average Benefits Rating],
  3. AVG(CASE WHEN w.category_id = 2 THEN CAST(SUBSTRING(a.answer, 4, 1) AS int) ELSE NULL END) AS [Average Facilities Rating]
  4. FROM questionnaire AS q INNER JOIN
  5. employee AS e ON q.employee_id = e.employee_number INNER JOIN
  6. answer AS a ON e.id = a.employee_id INNER JOIN
  7. question AS w ON a.question_id = w.id
  8. WHERE (w.question_type = 1) AND (a.answer <> 'ansNA') AND (q.attempted_date IS NOT NULL)
  9. GROUP BY DATEPART(year, q.attempted_date), DATENAME(MM, q.attempted_date)
  10. ORDER BY Year, Month DESC
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Other Threads in the MS SQL Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC