如何在sql server中使用带有left join pivot的where子句

时间:2018-04-20 06:49:29

标签: sql sql-server

我正在使用枢轴,但我的输出不正确我不知道我的错误是什么。 我只想要2018年的数据。 请帮帮我

当前输出

+--------------+----+------+------+------+------+------+------+------+------+------+------+------+------+
| ControllerNo | id |  jan |  feb |  mar |  apr |  may |  jun |  jul |  aug |  sep |  oct |  nov |  dec |
+--------------+----+------+------+------+------+------+------+------+------+------+------+------+------+
| IT.1         |  1 |   0  |    1 |   0  |   0  |  0   |   0  |   0  |   0  |  0   |   0  |   0  |   0  |
| IT.2         |  2 |   0  |    1 |   0  |   0  |  0   |   0  |   0  |   0  |  0   |   0  |   0  |   0  |
| IT.3         |  3 |   0  |    0 |   0  |   0  |  0   |   0  |   0  |   0  |  0   |   0  |   0  |   0  |
| IT.4         |  4 |   0  |    0 |   0  |   0  |  0   |   0  |   0  |   0  |  0   |   0  |   0  |   0  |
| IT.5         |  5 |   0  |    0 |   0  |   0  |  0   |   0  |   0  |   0  |  0   |   0  |   0  |   0  |
+--------------+----+------+------+------+------+------+------+------+------+------+------+------+------+

预期输出

+--------------+----+------+------+------+------+------+------+------+------+------+------+------+------+
| ControllerNo | id |  jan |  feb |  mar |  apr |  may |  jun |  jul |  aug |  sep |  oct |  nov |  dec |
+--------------+----+------+------+------+------+------+------+------+------+------+------+------+------+
| IT.1         |  1 |   0  |    0 |   0  |   0  |  0   |   0  |   0  |   0  |  0   |   0  |   0  |   0  |
| IT.2         |  2 |   0  |    1 |   0  |   0  |  0   |   0  |   0  |   0  |  0   |   0  |   0  |   0  |
| IT.3         |  3 |   0  |    0 |   0  |   0  |  0   |   0  |   0  |   0  |  0   |   0  |   0  |   0  |
| IT.4         |  4 |   0  |    0 |   0  |   0  |  0   |   0  |   0  |   0  |  0   |   0  |   0  |   0  |
| IT.5         |  5 |   0  |    0 |   0  |   0  |  0   |   0  |   0  |   0  |  0   |   0  |   0  |   0  |
+--------------+----+------+------+------+------+------+------+------+------+------+------+------+------+

表格Dashboard_new

ControllerNo  Year    MonthName     DepartmentName
IT.2          2020       FEB        Department-1
IT.1          2019       FEB        Department-2
IT.1          2019       FEB        Department-1
IT.2          2018       FEB        Department-1
IT.2          2019       FEB        Department-3

表ControllerName

Id   ControllerNo
1     IT.1
2     IT.2
3     IT.3
4     IT.4
5     IT.5

我的查询

SELECT *
FROM(
    SELECT
    ControllerNo as ControlIdNo,ControllerName.Id , [MonthName] as [month],
    (select DepartmentName from Dashboard_new where Dashboard_new.Year='2018' group by DepartmentName) as Amount
    FROM ControllerName 
    left join Dashboard_new on ControllerName.ControlIdNo = Dashboard_new.ControlIdNo
    group by ControllerNo ,ControllerName.Id , [MonthName]

) as s
PIVOT
(
    count(Amount)
    FOR[month] IN(jan, feb, mar, apr,
    may, jun, jul, aug, sep, oct, nov, dec)
)AS pvt

1 个答案:

答案 0 :(得分:2)

你应该在像下面的透视之前那样限制源查询中的输出

SELECT *
FROM(
    SELECT
    ControllerNo as ControlIdNo,
    ControllerName.Id , 
    [MonthName] as [month],
    (select DepartmentName from Dashboard_new where Dashboard_new.Year='2018' group by DepartmentName) as Amount -- this part also looks wrong
    FROM ControllerName 
    left join Dashboard_new on ControllerName.ControlIdNo = Dashboard_new.ControlIdNo 
    and Dashboard_new.Year='2018'--- this is the line to add
    group by ControllerNo ,ControllerName.Id , [MonthName]

) as s
PIVOT
(
    count(Amount)
    FOR[month] IN(jan, feb, mar, apr,
    may, jun, jul, aug, sep, oct, nov, dec)
)AS pvt