sql中连接多个结果集查询符合要求的字段

1.sql中将查询到的结果集作为一个表去连接多个结果集查询想要的字段,适用于查询复杂逻辑的字段;

举例:

select dd.base_grade_value,count(distinct dd.userid) from 


(select cc.school_id,bb.userid,cc.base_grade_value from 

(select a.create_time,a.student_id,a.school_id,c.base_grade_value,b.clzss_id from 
jk_ods_fz_ssp_school.v_ods_fz_ssp_school_org_school_student a,
jk_ods_fz_ssp_school.v_ods_fz_ssp_school_org_clzss_student_rel b,
jk_ods_fz_ssp_school.v_ods_fz_ssp_school_org_school_grade c 
where a.id=b.school_student_id and a.school_id=b.school_id and b.grade_id=c.id and b.school_id=c.school_id) cc
join


(select aa.userid from (
select a.userid,a.createtime from ods_log_realtime.ods_log_raw_kafka_input  a
where substring(a.createtime,1,7) ='2018-06' )aa
join  
jk_ods_fz_ssp_account.v_ods_fz_ssp_account_ac_student c on aa.userid=c.id 
and  c.status=1 and  substring(c.create_time,1,7)= substring(aa.createtime,1,7) and substring(c.activate_at,1,7)= substring(aa.createtime,1,7) )bb 
on bb.userid=cc.student_id)dd  join

jk_ods_fz_ssp_school.ods_fz_ssp_school_org_school d  on  d.id=dd.school_id

and d.school_name  not regexp '【.*'   
and  d.status in(1,3) group by dd.base_grade_value;

猜你喜欢

转载自blog.csdn.net/qq_35958094/article/details/82994649