t-sql“按”分区。结果不如预期

发布于 2025-01-29 12:35:03 字数 851 浏览 1 评论 0原文

我要做的是通过使用分区逻辑获得“ emailaddresses”的总数。正如您在结果集电子表格中看到的那样,第一个记录是正确的 - 此特定的电子邮件地址存在109次。但是,第二个记录,相同的电子邮件地址,numberFemailAddresses列显示108。显然,我并不是正确地编写此SQL,我希望对我可能做错的事情有一些反馈。

我想看到的是,对于此特定的电子邮件地址,数字109始终在列numberFemailAddresses下方。我可能做错了什么?

这是我的代码:

select
    Q1.SubscriberKey,
    Q1.EmailAddress,
    Q1.numberOfEmailAddresses
from
    (select
        sub.SubscriberKey as SubscriberKey,
        sub.EmailAddress as EmailAddress,
        count(*) over (partition by sub.EmailAddress order by sub.SubscriberKey asc) as numberOfEmailAddresses
    from    
        ent._Subscribers sub) Q1

这是我的结果集,由“ numberFemailAddresses”订购:

What I'm trying to do is get a total count of "EmailAddresses" via using partitioning logic. As you can see in the result set spreadsheet, the first record is correct - this particular email address exists 109 times. But, the second record, same email address, the numberOfEmailAddresses column shows 108. And so on - just keeps incrementing downward by 1 on the same email address. Clearly, I'm not writing this SQL right and I was hoping to get some feedback as to what I might be doing wrong.

What I would like to see is the number 109 consistently down the column numberOfEmailAddresses for this particular email address. What might I be doing wrong?

Here's my code:

select
    Q1.SubscriberKey,
    Q1.EmailAddress,
    Q1.numberOfEmailAddresses
from
    (select
        sub.SubscriberKey as SubscriberKey,
        sub.EmailAddress as EmailAddress,
        count(*) over (partition by sub.EmailAddress order by sub.SubscriberKey asc) as numberOfEmailAddresses
    from    
        ent._Subscribers sub) Q1

And here's my result set, ordered by "numberOfEmailAddresses":
enter image description here

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

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

发布评论

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

评论(1

旧梦荧光笔 2025-02-05 12:35:03
select distinct
    Q1.SubscriberKey,
    Q1.EmailAddress,
(select count(*) from ent._Subscribers sub where sub.EmailAddress = Q1.EmailAddress) as numberOfEmailAddress
from ent._Subscribers Q1

会给你想要的东西。我认为由您的分区功能包含订单是导致下降计数的原因。按照我理解,在分区函数中订购进一步细分了分区。

select
    Q1.SubscriberKey,
    Q1.EmailAddress,
    Q1.numberOfEmailAddresses
from
    (select
        sub.SubscriberKey as SubscriberKey,
        sub.EmailAddress as EmailAddress,
        count(*) over (partition by sub.EmailAddress) as numberOfEmailAddresses
    from    
        ent._Subscribers sub) Q1

可能也有效,但我找不到适合测试的数据集。

select distinct
    Q1.SubscriberKey,
    Q1.EmailAddress,
(select count(*) from ent._Subscribers sub where sub.EmailAddress = Q1.EmailAddress) as numberOfEmailAddress
from ent._Subscribers Q1

will get you what you want. I think the inclusion of the order by in your partition function is what is causing the descending count. Ordering in a partition function further subdivides the partition as I understand it.

select
    Q1.SubscriberKey,
    Q1.EmailAddress,
    Q1.numberOfEmailAddresses
from
    (select
        sub.SubscriberKey as SubscriberKey,
        sub.EmailAddress as EmailAddress,
        count(*) over (partition by sub.EmailAddress) as numberOfEmailAddresses
    from    
        ent._Subscribers sub) Q1

May also work but I can't find a suitable dataset to test.

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