我有一个非常庞大的查询,其最简单的形式如下所示:
select r.rep_id, u.user_id, u.signup_date, pi.application_date, pi.management_date, aum
from table1 r
left join table2 u on r.user_id=u.user_id
left join table3 pi on u.user_id=pi.user_id
我需要再添加一个条件,为每个代表提供非空应用程序日期的用户数(例如:代表1有3个用户填写申请日期),并将其分配到类别中(因为3个用户,代表是一个某种状态类别)。这看起来像这样:
case when sum(case when application_date is not null then 1 else 0 end) >=10 then 'status1'
when sum(case when application_date is not null then 1 else 0 end) >=5 then 'status2'
when sum(case when application_date is not null then 1 else 0 end) >=1 then 'status3'
else 'no_status' end as category
但是,如果我只是将其添加到select语句中,则所有代表将变为status1,因为sum()是在所有填充了应用程序日期的顾问程序上完成的:
select r.rep_id, u.user_id, u.signup_date, pi.application_date, pi.management_date, aum,
(
select case when sum(case when application_date is not null then 1 else 0 end) >=10 then 'status1'
when sum(case when application_date is not null then 1 else 0 end) >=5 then 'status2'
when sum(case when application_date is not null then 1 else 0 end) >=1 then 'status3'
else 'no_status' end as category
from table3
) as category
from table1 r
left join table2 u on r.user_id=u.user_id
left join table3 pi on u.user_id=pi.user_id
你可以协助我的查询添加到跨代表而不是整体吗?非常感激!
答案 0 :(得分:3)
根据您的描述,我认为您需要一个窗口功能:
select r.rep_id, u.user_id, u.signup_date, pi.application_date, pi.management_date, aum,
count(pi.application_date) over (partition by r.rep_id) as newcol
from table1 r left join
table2 u
on r.user_id = u.user_id left join
table3 pi
on u.user_id = pi.user_id;
如果您愿意,可以使用count()
中的case
来获取范围。