943,625 Members | Top Members by Rank

Ad:
  • MS SQL Discussion Thread
  • Marked Solved
  • Views: 1607
  • MS SQL RSS
Dec 23rd, 2008
0

Need help in getting Average using SQL Cross Tab

Expand Post »
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:-

MS SQL Syntax (Toggle Plain Text)
  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:	47
Size:	16.7 KB
ID:	8641
Reputation Points: 10
Solved Threads: 0
Newbie Poster
dfs3000my is offline Offline
21 posts
since Dec 2008
Dec 27th, 2008
0

Re: Need help in getting Average using SQL Cross Tab

Make it subquery like below:

MS SQL Syntax (Toggle Plain Text)
  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]
Reputation Points: 10
Solved Threads: 13
Light Poster
huangzhi is offline Offline
48 posts
since Feb 2008
Dec 30th, 2008
0

Re: Need help in getting Average using SQL Cross Tab

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.

Reputation Points: 10
Solved Threads: 0
Newbie Poster
dfs3000my is offline Offline
21 posts
since Dec 2008
Dec 30th, 2008
0

Re: Need help in getting Average using SQL Cross Tab

I have managed to find a solution to this problem

MS SQL Syntax (Toggle Plain Text)
  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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
dfs3000my is offline Offline
21 posts
since Dec 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in MS SQL Forum Timeline: Debugger for PL/SQL in sql server
Next Thread in MS SQL Forum Timeline: Crazy CrossTab problems





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC