t-sql“按”分区。结果不如预期
我要做的是通过使用分区逻辑获得“ 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
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":
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
会给你想要的东西。我认为由您的分区功能包含订单是导致下降计数的原因。按照我理解,在分区函数中订购进一步细分了分区。
可能也有效,但我找不到适合测试的数据集。
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.
May also work but I can't find a suitable dataset to test.