我正在构建一个发送电子邮件的系统,我需要进入[User]表来检索要发送的电子邮件,以及将用于取消订阅目的的GUID(uniqueidentifier类型)。
这适用于电子商务解决方案,因为匿名用户和登录用户可以拥有相同的电子邮件地址,因此会创建重复的电子邮件条目,这些条目需要在查询中有所区别。
我无法找到检索电子邮件和guid的解决方案,但只能在电子邮件中有所区别。到目前为止,这是我的查询。
SELECT Email, User_GUID
FROM [User]
WHERE
IsActive = 1 AND
IsEmailValid = 1 AND
IsNotActiveBecauseUnsubscribed = 0 AND
Subscribed = 1
GROUP BY Email, User_GUID
答案 0 :(得分:8)
with cte
as
(
select *, row_number() over (partition by Email order by User_GUID) RowNumber
from [User]
where
IsActive = 1 and
IsEmailValid = 1 and
IsNotActiveBecauseUnsubscribed = 0 and
Subscribed = 1
)
select Email, User_GUID
from cte
where RowNumber = 1
或
select Email, User_GUID
from
(
select *, row_number() over (partition by Email order by User_GUID) RowNumber
from [User]
where
IsActive = 1 and
IsEmailValid = 1 and
IsNotActiveBecauseUnsubscribed = 0 and
Subscribed = 1
) tt
where RowNumber = 1
答案 1 :(得分:0)
你可以做到
SELECT Distinct Email, User_GUID
FROM [User]
WHERE IsActive = 1 AND IsEmailValid = 1 And IsNotActiveBecauseUnsubscribed = 0 AND Subscribed = 1
GROUP BY Email, User_GUID