select
u.Resnet_Agent_ID,
u.First_Name + ' ' + u.Last_Name AS [Agent Name],
u.Email,
[Current Membership Level] = CASE
WHEN u.UserMembershiptType_CodeID = 1 THEN 'Standard'
WHEN u.UserMembershiptType_CodeID = 2 THEN 'Premium Membership'
WHEN u.UserMembershiptType_CodeID = 3 THEN 'Pro Plus Membership'
WHEN u.UserMembershiptType_CodeID = 4 THEN 'Pro Plus Trial'
WHEN u.UserMembershiptType_CodeID = 5 THEN 'Premium Free Trial'
WHEN u.UserMembershiptType_CodeID = 6 THEN 'Pro'
WHEN u.UserMembershiptType_CodeID = 7 THEN 'Pro Plus Comp - Trial'
ELSE NULL END,
[Prior Paid Membership Level] = CASE
WHEN mh.MembershipType_CodeID = 2 THEN 'Premium Membership'
WHEN mh.MembershipType_CodeID = 3 THEN 'Pro Plus Membership'
WHEN mh.MembershipType_CodeID = 4 THEN 'Pro Plus Trial'
WHEN mh.MembershipType_CodeID = 5 THEN 'Premium Free Trial'
WHEN mh.MembershipType_CodeID = 6 THEN 'Pro'
WHEN mh.MembershipType_CodeID = 7 THEN 'Pro Plus Comp - Trial'
ELSE NULL END, mh.Description
from amp.dbo.tbl_User u (nolock)
join amp.dbo.tbl_MembershipHistoryNew mh (nolock)
on u.UniversalUserId = mh.AgentUniversalUserId
where u.IsAgent = 1 and mh.MembershipType_CodeID <> 1
如何将当前会员级别分组为仅显示“一个当前会员级别,仅一个Resnet_Agent_ID,仅一个代理名称,仅一个电子邮件”?
答案 0 :(得分:2)
一种选择是使用cross apply()
根据某些列获取最新行,例如到期日期:
select
u.Resnet_Agent_id
, [Agent Name]= u.First_Name + ' ' + u.Last_Name
, u.Email
, [Current Membership Level] = case
when u.UserMembershiptType_Codeid = 1 then 'Standard'
when u.UserMembershiptType_Codeid = 2 then 'Premium Membership'
when u.UserMembershiptType_Codeid = 3 then 'Pro Plus Membership'
when u.UserMembershiptType_Codeid = 4 then 'Pro Plus Trial'
when u.UserMembershiptType_Codeid = 5 then 'Premium Free Trial'
when u.UserMembershiptType_Codeid = 6 then 'Pro'
when u.UserMembershiptType_Codeid = 7 then 'Pro Plus Comp - Trial'
else null end
, [Prior Paid Membership Level] = case
when mh.MembershipType_Codeid = 2 then 'Premium Membership'
when mh.MembershipType_Codeid = 3 then 'Pro Plus Membership'
when mh.MembershipType_Codeid = 4 then 'Pro Plus Trial'
when mh.MembershipType_Codeid = 5 then 'Premium Free Trial'
when mh.MembershipType_Codeid = 6 then 'Pro'
when mh.MembershipType_Codeid = 7 then 'Pro Plus Comp - Trial'
else null end
, mh.Description
from amp.dbo.tbl_User u (nolock)
cross apply (
select top 1
MembershipType_Codeid
, Description
from amp.dbo.tbl_MembershipHistoryNew i (nolock)
where u.UniversalUserId = i.AgentUniversalUserId
order by i.ExpirationDate desc /* order by to get the latest first here */
) mh
where u.IsAgent = 1
and mh.MembershipType_Codeid <> 1
答案 1 :(得分:0)
目前还不清楚是否要包含或排除代码为null的成员。 如果你想要空值然后使用: 和(mh.MembershipType_Codeid&lt;&gt; 1或mh.MembershipType_Codeid为null) 否则使用: 和(mh.MembershipType_Codeid&lt;&gt; 1或mh.MembershipType_Codeid not null)