id、状态和日期上的 CTE 导致结果平方

发布于 2024-10-20 14:59:49 字数 1172 浏览 3 评论 0原文

我有一个表定义为

CREATE TABLE ItemDetail (
    ItemNumber        bigint not null,
    SiteId            int not null,
    Status            int not null,
    ScanDate          datetime not null,
)

我想要做的是按站点、按状态、按天获取计数。我有一个已定义的 CTE

;WITH statCTE AS (
    SELECT
        Count(ItemNumber) over(partition by SiteId, Status, DATEADD(dd, 0, DATEDIFF(dd, 0, ScanDate))) as ItemCount,
        SiteId,
        Status,
        DATEADD(dd, 0, DATEDIFF(dd, 0, ScanDate)) AS ScanDate
    FROM
        ItemDetail
)

问题是,当我运行时

select * from statCTE where siteid = 119 and scandate = '3/3/2011'

,我得到

ItemCount SiteId Status ScanDate
2   119 0   2011-03-03 00:00:00.000
2   119 0   2011-03-03 00:00:00.000
6   119 5   2011-03-03 00:00:00.000
6   119 5   2011-03-03 00:00:00.000
6   119 5   2011-03-03 00:00:00.000
6   119 5   2011-03-03 00:00:00.000
6   119 5   2011-03-03 00:00:00.000
6   119 5   2011-03-03 00:00:00.000

结果集应该是 2 行,其中 2 行表示状态 0,一行 6 表示状态 6。因此,我的分区无法转换日期只是一个日期对象并选择结果。我可以在最终查询(数据透视)中对 ItemCount 总数求平方根,但这更像是一种黑客行为,而不是解决实际问题。

I have a table defined as

CREATE TABLE ItemDetail (
    ItemNumber        bigint not null,
    SiteId            int not null,
    Status            int not null,
    ScanDate          datetime not null,
)

What I am trying to do is get counts by site, by status, by day. I have a CTE as defined

;WITH statCTE AS (
    SELECT
        Count(ItemNumber) over(partition by SiteId, Status, DATEADD(dd, 0, DATEDIFF(dd, 0, ScanDate))) as ItemCount,
        SiteId,
        Status,
        DATEADD(dd, 0, DATEDIFF(dd, 0, ScanDate)) AS ScanDate
    FROM
        ItemDetail
)

The problem is that when I then run

select * from statCTE where siteid = 119 and scandate = '3/3/2011'

I get

ItemCount SiteId Status ScanDate
2   119 0   2011-03-03 00:00:00.000
2   119 0   2011-03-03 00:00:00.000
6   119 5   2011-03-03 00:00:00.000
6   119 5   2011-03-03 00:00:00.000
6   119 5   2011-03-03 00:00:00.000
6   119 5   2011-03-03 00:00:00.000
6   119 5   2011-03-03 00:00:00.000
6   119 5   2011-03-03 00:00:00.000

The result set should be 2 rows, one with 2 for status 0 and one with 6 for status 6. So, my partition isn't working with transforming the date into just a date object and picking the results. I could just square root the ItemCount totals in my final query (a pivot), but that's more of a hack than fixing the actual problem.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

神经暖 2024-10-27 14:59:49

您误解了窗口函数(或排名函数)的使用。在您的情况下,计数不是聚合。此用途将函数的结果添加到查询返回的每一行。通常,聚合是在 select 子句之前处理的,但在本例中,它是在 ALONG select 子句之后处理的。因此,TSQL 会计算有多少行满足您的分区,并将其沿着查询返回的每一行输出。

您有 2 个分区,计数分别为 2 和 6(由窗口分区函数计算),然后整个结果集会附加附加列。

如果您想要 2 和 6 结果,您需要使用 group by 子句编写查询

WITH statCTE AS (
    SELECT
        Count(ItemNumber)as ItemCount,
        SiteId,
        Status,
        DATEADD(dd, 0, DATEDIFF(dd, 0, ScanDate)) AS ScanDate
    FROM
        ItemDetail
    group by  SiteId, Status, DATEADD(dd, 0, DATEDIFF(dd, 0, ScanDate))
)

You are misunderstaning the use of Window functions (or ranking functions). In Your case Count is NOT an aggregate. This use add the result of the function to EVERY row returned by the query. Normally the aggregate is processed BEFORE the select clause, however in this example it is processes ALONG select clause. So TSQL counts how many rows satisfy Your partition and output it along EVERY row returned by the query.

You have 2 partitions with respectable count of 2 and 6 (calculated by the window partition function) and then whole result set is appended with additional column.

If You want the 2 and 6 results YOu need to write the query using the group by clause

WITH statCTE AS (
    SELECT
        Count(ItemNumber)as ItemCount,
        SiteId,
        Status,
        DATEADD(dd, 0, DATEDIFF(dd, 0, ScanDate)) AS ScanDate
    FROM
        ItemDetail
    group by  SiteId, Status, DATEADD(dd, 0, DATEDIFF(dd, 0, ScanDate))
)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文