2014-06-18 23 views
1

我QUERY:使用數據透視與多個列使用總和?

SELECT dept,csedept_name,January,February,March,April,May,June,July,August,September,October,November,December 
FROM (SELECT CAST(employeedept AS INT) as dept, 
     ROUND(AVG(case when rating1>0 THEN CAST(rating1 AS FLOAT) ELSE null END), 2) as q1, 
     ROUND(AVG(case when rating2>0 THEN CAST(rating2 AS FLOAT) ELSE null END), 2) as q2, 
     ROUND(AVG(case when rating3>0 THEN CAST(rating3 AS FLOAT) ELSE null END), 2) as q3, 
     ROUND(AVG(case when rating4>0 THEN CAST(rating4 AS FLOAT) ELSE null END), 2) as q4, 
     ROUND(AVG(case when rating5>0 THEN CAST(rating5 AS FLOAT) ELSE null END), 2) as q5, 
count(*) as 'totalstars',month_cse= datename(month,execoffice_date),YEAR_cse =YEAR(execoffice_date) 
     FROM CSEReduxResponses 
     WHERE 
     execoffice_status = 1 
     and employeedept =17 
     group by employeedept,month(execoffice_date),YEAR(execoffice_date),DATENAME(month,execoffice_date) 

    ) 
    AS r JOIN CSEReduxDepts d 
ON d.csedept_id = r.dept and d.csedept_id=17 
PIVOT(SUM(q1) 
    FOR [month_cse] IN (
     [January],[February],[March],[April],[May],[June],[July],[August], [September],[October],[November],[December] 
     )) AS pvt 

其中在每個月根據該部門得到的平均值。 在上面的查詢即時獲得'q1'的總和,並顯示該月份和部門的正確數字 ,但它顯示每個月在1行,我只能顯示'q1'時,我想顯示q1-q5 。

我可能會採取長/錯的方式來做到這一點,也許使用樞軸是錯誤的方式去。

有沒有辦法我可以添加q1-q5並在相應的月份上顯示?

我做了一個http://sqlfiddle.com/#!3/05390/1

回答

0

怎麼樣這個查詢:

SQL Fiddle

查詢1

;with 
years as 
(select distinct year(execoffice_date) y from CSEReduxResponses), 
months as 
(select 'January' as m, 1 n 
union select 'February', 2 n 
union select 'March', 3 n 
union select 'April', 4 n 
union select 'May', 5 n 
union select 'June', 6 n 
union select 'July', 7 n 
union select 'August', 8 n 
union select 'September', 9 n 
union select 'October', 10 n 
union select 'November', 11 n 
union select 'December', 12 n), 
cse as (SELECT CAST(employeedept AS INT) as dept, 
     ROUND(AVG(case when rating1>0 THEN CAST(rating1 AS FLOAT) ELSE null END), 2) as q1, 
     ROUND(AVG(case when rating2>0 THEN CAST(rating2 AS FLOAT) ELSE null END), 2) as q2, 
     ROUND(AVG(case when rating3>0 THEN CAST(rating3 AS FLOAT) ELSE null END), 2) as q3, 
     ROUND(AVG(case when rating4>0 THEN CAST(rating4 AS FLOAT) ELSE null END), 2) as q4, 
     ROUND(AVG(case when rating5>0 THEN CAST(rating5 AS FLOAT) ELSE null END), 2) as q5, 
     count(*) as 'totalstars', YEAR(execoffice_date) as YEAR_cse, month(execoffice_date) as m_cse 
     FROM CSEReduxResponses 
     group by employeedept, month(execoffice_date), YEAR(execoffice_date) 

    ) 
SELECT csedept_id, d.csedept_name, years.y, months.m, q1, q2, q3, q4, q5, totalstars 
FROM years cross join months 
cross join CSEReduxDepts d 
left join cse on 
months.n = cse.m_cse and d.csedept_id = cse.dept 
where d.csedept_id = 17 
order by years.y, months.n, csedept_id, csedept_name 

Results

| CSEDEPT_ID | CSEDEPT_NAME | Y |   M |  Q1 |  Q2 |  Q3 |  Q4 |  Q5 | TOTALSTARS | 
|------------|---------------|------|-----------|--------|--------|--------|--------|--------|------------| 
|   17 | department 17 | 2014 | January | (null) | (null) | (null) | (null) | (null) |  (null) | 
|   17 | department 17 | 2014 | February | (null) | (null) | (null) | (null) | (null) |  (null) | 
|   17 | department 17 | 2014 |  March | (null) | (null) | (null) | (null) | (null) |  (null) | 
|   17 | department 17 | 2014 |  April | (null) | (null) | (null) | (null) | (null) |  (null) | 
|   17 | department 17 | 2014 |  May | 2.83 | 4.5 | 3.67 | 1.75 |  1 |   6 | 
|   17 | department 17 | 2014 |  June | 2.33 |  4 | 3.33 |  2 |  1 |   3 | 
|   17 | department 17 | 2014 |  July | (null) | (null) | (null) | (null) | (null) |  (null) | 
|   17 | department 17 | 2014 | August | (null) | (null) | (null) | (null) | (null) |  (null) | 
|   17 | department 17 | 2014 | September | (null) | (null) | (null) | (null) | (null) |  (null) | 
|   17 | department 17 | 2014 | October | (null) | (null) | (null) | (null) | (null) |  (null) | 
|   17 | department 17 | 2014 | November | (null) | (null) | (null) | (null) | (null) |  (null) | 
|   17 | department 17 | 2014 | December | (null) | (null) | (null) | (null) | (null) |  (null) | 

您甚至可以對最後的where d.csedept_id = 17子句發表評論以檢索所有部門的詳細信息。我不認爲每個月在單獨的列中顯示q1,q2,q3,q4,q5,TOTALSTARS將是明智的。你會最終在這種情況下60列

+0

哇,是的太多 – user3591637

+0

感謝這個解決方案,我最終想要做的是加起來所有可能q1-q5並得到這個平均值,這將是查詢中可能嗎? – user3591637