简体   繁体   English

如何在SQL Server中使用带有左联接枢轴的where子句

[英]How to use where clause with left join pivot in sql server

I am using pivot but my output not correct I don't know what is my mistake. 我正在使用数据透视,但是我的输出不正确,我不知道我的错误是什么。 I want only data Year 2018. please help me 我只想要2018年数据。请帮助我

Current Output 电流输出

+--------------+----+------+------+------+------+------+------+------+------+------+------+------+------+
| 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  |
+--------------+----+------+------+------+------+------+------+------+------+------+------+------+------+

Expected Output 预期产量

+--------------+----+------+------+------+------+------+------+------+------+------+------+------+------+
| 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  |
+--------------+----+------+------+------+------+------+------+------+------+------+------+------+------+

Table Dashboard_new 表格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

Table ControllerName 表ControllerName

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

My Query 我的查询

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

you should limit your output in source query before pivot like below 您应先在源数据查询之前限制输出,如下所示

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM