如何进行多次计数

发布于 2025-01-12 09:54:39 字数 1893 浏览 0 评论 0原文

我正在尝试监视某些 ID 上的某些 DQ,并且我有一个如下所示的表:

IDFlag 1Flag 2Flag 3Date
001Error一月
002ErrorError一月
003ErrorError一月
001Error二月
002Error二月
003Error二月
001Error错误错误三月
002三月
003错误错误错误三月
001四月
002错误四月
003错误四月

并希望将其旋转这个:

-一月 二月三月四月Flag1
2210Flag2
2111Flag3
1011到目前为止

,我已经尝试过这样的操作

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:

IDFlag 1Flag 2Flag 3Date
001ErrorJanuary
002ErrorErrorJanuary
003ErrorErrorJanuary
001ErrorFebruary
002ErrorFebruary
003ErrorFebruary
001ErrorErrorErrorMarch
002March
003ErrorErrorErrorMarch
001April
002ErrorApril
003ErrorApril

and would like to pivot it like this:

-JanuaryFebruaryMarchApril
Flag12210
Flag22111
Flag31011

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情感失落者 2025-01-19 09:54:39

您需要首先un对数据进行透视,以便可以将Flag值放入行中,然后将月份透视到列中。

SELECT *
FROM (
    SELECT
      [Flag 1],
      [Flag 2],
      [Flag 3],
      Date
    FROM Main_Table mt
) mt
UNPIVOT (
    FlagValue FOR FlagName IN (
        [Flag 1],
        [Flag 2],
        [Flag 3]
    )
) u
PIVOT (
    COUNT(FlagValue) FOR Date IN (
        January,
        February,
        March,
        April
    )
) p;

您还可以将 UNPIVOT 替换为 CROSS APPLY (VALUES),并将 PIVOT 替换为条件聚合。

SELECT
  u.FlagName,
  January  = COUNT(CASE WHEN Date = 'January'  THEN FlagValue END),
  February = COUNT(CASE WHEN Date = 'February' THEN FlagValue END),
  March    = COUNT(CASE WHEN Date = 'March'    THEN FlagValue END),
  April    = COUNT(CASE WHEN Date = 'April'    THEN FlagValue END)
FROM Main_Table mt
CROSS APPLY (VALUES
    ('Flag 1', [Flag 1]),
    ('Flag 2', [Flag 2]),
    ('Flag 3', [Flag 3])
) u(FlagName, FlagValue)
GROUP BY
  u.FlagName;

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.

SELECT *
FROM (
    SELECT
      [Flag 1],
      [Flag 2],
      [Flag 3],
      Date
    FROM Main_Table mt
) mt
UNPIVOT (
    FlagValue FOR FlagName IN (
        [Flag 1],
        [Flag 2],
        [Flag 3]
    )
) u
PIVOT (
    COUNT(FlagValue) FOR Date IN (
        January,
        February,
        March,
        April
    )
) p;

You can also replace UNPIVOT with CROSS APPLY (VALUES and replace PIVOT with conditional aggregation.

SELECT
  u.FlagName,
  January  = COUNT(CASE WHEN Date = 'January'  THEN FlagValue END),
  February = COUNT(CASE WHEN Date = 'February' THEN FlagValue END),
  March    = COUNT(CASE WHEN Date = 'March'    THEN FlagValue END),
  April    = COUNT(CASE WHEN Date = 'April'    THEN FlagValue END)
FROM Main_Table mt
CROSS APPLY (VALUES
    ('Flag 1', [Flag 1]),
    ('Flag 2', [Flag 2]),
    ('Flag 3', [Flag 3])
) u(FlagName, FlagValue)
GROUP BY
  u.FlagName;

db<>fiddle

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文