View Single Post
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