union - SQL - same tables different where clause -


im new sql , trying following:

my query pulls 2 fields based on clause:

select distinct       count(distinct t1.p_id) "c1",      count(distinct t2.sa_id) "c2"  capd_section t5,       capd_department t6,      capd_person t1,      capd_studentapplication t2,      capd_module t4,      capd_moduleapplication t3  (t3.ma_studentapplication(+)=t2.sa_id) ,        (t3.ma_module=t4.m_id(+)) ,        (t4.m_modulesection=t5.s_id(+)) ,        (t4.m_moduledept=t6.d_id(+)) ,        (t4.m_reference not '%%fta%%') ,        **(t2.sa_reference '212%%')** ,        (t4.m_reference not '%%he%%') ,        (t4.m_reference not '%%pt%%') ,        (t4.m_name not 'nctj%%') ,        (t4.m_reference not 'me%%') ,        (t2.sa_student=t1.p_id)  having (count(distinct t3.ma_id)>0) 

i wanting have same query clause (t2.sa_reference '213%%') pull aswell. (current year & previous year)

so 4 fields in total (c1,c2,c3,c4). if makes sense @ all. possible?

many :)

just use case in count statement:

select distinct       count(distinct t1.p_id) "c1",      count(distinct t2.sa_id) "c2",      count(distinct case when t2.sa_reference '212%%' t1.p_id else null end) "c3",      count(distinct case when t2.sa_reference '212%%' t2.sa_id else null end) "c4"  capd_section t5,       capd_department t6,      capd_person t1,      capd_studentapplication t2,      capd_module t4,      capd_moduleapplication t3  (t3.ma_studentapplication(+)=t2.sa_id) ,        (t3.ma_module=t4.m_id(+)) ,        (t4.m_modulesection=t5.s_id(+)) ,        (t4.m_moduledept=t6.d_id(+)) ,        (t4.m_reference not '%%fta%%') ,        (t4.m_reference not '%%he%%') ,        (t4.m_reference not '%%pt%%') ,        (t4.m_name not 'nctj%%') ,        (t4.m_reference not 'me%%') ,        (t2.sa_student=t1.p_id)  having (count(distinct t3.ma_id)>0) 

Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -