使用和使用带有多个列的Pivot?

时间:2014-06-18 22:57:01

标签: sql sql-server-2008

我有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'并显示该月和部门的正确数字 但它每个月显示一行,我只能显示' q1'当我想展示q1-q5。

我可能采取多长/错误的方式来做这件事,也许使用枢轴是错误的方式。

有没有办法可以在相应的月份添加q1-q5并显示?

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

1 个答案:

答案 0 :(得分: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列