如何进行多次计数
我正在尝试监视某些 ID 上的某些 DQ,并且我有一个如下所示的表:
ID | Flag 1 | Flag 2 | Flag 3 | Date |
---|---|---|---|---|
001 | Error | 一月 | ||
002 | Error | Error | 一月 | |
003 | Error | Error | 一月 | |
001 | Error | 二月 | ||
002 | Error | 二月 | ||
003 | Error | 二月 | ||
001 | Error | 错误 | 错误 | 三月 |
002 | 三月 | |||
003 | 错误 | 错误 | 错误 | 三月 |
001 | 四月 | |||
002 | 错误 | 四月 | ||
003 | 错误 | 四月 |
并希望将其旋转这个:
- | 一月 二月 | 三月 | 四月 | Flag1 |
---|---|---|---|---|
2 | 2 | 1 | 0 | Flag2 |
2 | 1 | 1 | 1 | Flag3 |
1 | 0 | 1 | 1 | 到目前为止 |
,我已经尝试过这样的操作
SELECT * FROM (
SELECT
Account, date, Flag1, Flag2
from Main_Table
where Flag1 is not null and len(Rule_Account_Name) >1
) Accounts
PIVOT (
count(Account)
FOR date in ([2021-08-21],[2021-09-21],[2021-10-22],[2021-11-22],[2021-12-31],[2022-01-31],[2022-02-28])
) AS PivotTable
,我感谢您的帮助,重点是监视每个月的错误数标志能够对标志进行排名,以投入更多精力修复前 3 个最差的标志
I am trying to monitor some DQ on some ID's and I have a table that looks like this:
ID | Flag 1 | Flag 2 | Flag 3 | Date |
---|---|---|---|---|
001 | Error | January | ||
002 | Error | Error | January | |
003 | Error | Error | January | |
001 | Error | February | ||
002 | Error | February | ||
003 | Error | February | ||
001 | Error | Error | Error | March |
002 | March | |||
003 | Error | Error | Error | March |
001 | April | |||
002 | Error | April | ||
003 | Error | April |
and would like to pivot it like this:
- | January | February | March | April |
---|---|---|---|---|
Flag1 | 2 | 2 | 1 | 0 |
Flag2 | 2 | 1 | 1 | 1 |
Flag3 | 1 | 0 | 1 | 1 |
So far I have tried it like this
SELECT * FROM (
SELECT
Account, date, Flag1, Flag2
from Main_Table
where Flag1 is not null and len(Rule_Account_Name) >1
) Accounts
PIVOT (
count(Account)
FOR date in ([2021-08-21],[2021-09-21],[2021-10-22],[2021-11-22],[2021-12-31],[2022-01-31],[2022-02-28])
) AS PivotTable
I appreciate the help, the point would be to monitor the number of errors each month for each of the flags to be able to rank the flags to put more effort in fixing the top 3 worst flags
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要首先un对数据进行透视,以便可以将
Flag
值放入行中,然后将月份透视到列中。您还可以将
UNPIVOT
替换为CROSS APPLY (VALUES
),并将PIVOT
替换为条件聚合。db<>小提琴
You need to first un-pivot the data so that you can get the
Flag
values into rows, then pivot the months into columns.You can also replace
UNPIVOT
withCROSS APPLY (VALUES
and replacePIVOT
with conditional aggregation.db<>fiddle