tommy Posted December 17, 2013 Report Posted December 17, 2013 Bhayya ee query run chesthe baga time padthundhi..so time koncham taginchali ante ela... with cntMissEvents as ( select pt_pgm_id, test_yr_num, analyte_id, max(case when evnt = '1' THEN evntcnt END) AS miss_event1, max(case when evnt = '2' THEN evntcnt END) AS miss_event2, max(case when evnt = '3' THEN evntcnt END) AS miss_event3 from ( select pt_pgm_id, test_yr_num, analyte_id, '1' as evnt, count(*) as evntcnt FROM clia_pt_enrlmnt ce where pt_scre_submsn_1_dt is not null and not exists ( select clia_intrnl_id, pt_pgm_id, test_yr_num, analyte_id, pt_scre_submsn_dt FROM clia_pt_scre cs where ce.prvdr_num = cs.prvdr_num AND ce.pt_pgm_id = cs.pt_pgm_id AND ce.test_yr_num =cs.test_yr_num AND ce.analyte_id = cs.analyte_id and evnt_num = 1 and ce.pt_scre_submsn_1_dt = cs.pt_scre_submsn_dt) group by pt_pgm_id, test_yr_num, analyte_id UNION select pt_pgm_id, test_yr_num, analyte_id, '2' as evnt, count(*) as evntcnt FROM clia_pt_enrlmnt ce where pt_scre_submsn_2_dt is not null and not exists ( select clia_intrnl_id, pt_pgm_id, test_yr_num, analyte_id, pt_scre_submsn_dt FROM clia_pt_scre cs where ce.prvdr_num = cs.prvdr_num AND ce.pt_pgm_id = cs.pt_pgm_id AND ce.test_yr_num =cs.test_yr_num AND ce.analyte_id = cs.analyte_id and evnt_num = 2 and ce.pt_scre_submsn_2_dt = cs.pt_scre_submsn_dt) group by pt_pgm_id, test_yr_num, analyte_id UNION select pt_pgm_id, test_yr_num, analyte_id, '3' as evnt, count(*) as evntcnt FROM clia_pt_enrlmnt ce where pt_scre_submsn_3_dt is not null and not exists ( select clia_intrnl_id, pt_pgm_id, test_yr_num, analyte_id, pt_scre_submsn_dt FROM clia_pt_scre cs where ce.prvdr_num = cs.prvdr_num AND ce.pt_pgm_id = cs.pt_pgm_id AND ce.test_yr_num =cs.test_yr_num AND ce.analyte_id = cs.analyte_id and evnt_num = 3 and ce.pt_scre_submsn_3_dt = cs.pt_scre_submsn_dt) group by pt_pgm_id, test_yr_num, analyte_id ) group by pt_pgm_id, test_yr_num, analyte_id ), cntTotEvents as ( select pt_pgm_id, test_yr_num, analyte_id, max(case when evnt = '1' THEN evntcnt END) AS tot_event1, max(case when evnt = '2' THEN evntcnt END) AS tot_event2, max(case when evnt = '3' THEN evntcnt END) AS tot_event3 from ( select pt_pgm_id, test_yr_num, analyte_id, '1' as evnt, count(*) as evntcnt from clia_pt_enrlmnt where pt_scre_submsn_1_dt is not null group by pt_pgm_id, test_yr_num, analyte_id union select pt_pgm_id, test_yr_num, analyte_id, '2' as evnt, count(*) as evntcnt from clia_pt_enrlmnt where pt_scre_submsn_2_dt is not null group by pt_pgm_id, test_yr_num, analyte_id union select pt_pgm_id, test_yr_num, analyte_id, '3' as evnt, count(*) as evntcnt from clia_pt_enrlmnt WHERE pt_scre_submsn_3_dt IS NOT NULL GROUP BY pt_pgm_id, test_yr_num, analyte_id) GROUP BY pt_pgm_id, test_yr_num, analyte_id) select cte.pt_pgm_id,cte.test_yr_num ,cte.analyte_id, cme.miss_event1, cme.miss_event2,cme.miss_event3, cte.total_event1, cte.total_event2,cte.total_event3 from cntTotEvents cte left join cntMissEvents cme on cte.pt_pgm_id = cme.pt_pgm_id AND cte.test_yr_num = cme.test_yr_num AND cte.analyte_id = cme.analyte_id) group by pt_pgm_id, test_yr_num, analyte_id ...
SwamyRaRa Posted December 17, 2013 Report Posted December 17, 2013 CITI#H@ CITI#H@ CITI#H@ ee code evaru rasinaru baaaa..........
cherlapalli_jailer Posted December 17, 2013 Report Posted December 17, 2013 functions vunte kasta time padutundi more over chaala pedda SQL why don't you go for STored procedure?
cherlapalli_jailer Posted December 17, 2013 Report Posted December 17, 2013 i dno't think this is efficient way to write a SQL chaala clumsy ga vunnadi go for SP and use temp table or hash table
dallascowboys Posted December 17, 2013 Report Posted December 17, 2013 better to remove those sub queries and only use CTEs
dallascowboys Posted December 17, 2013 Report Posted December 17, 2013 i dno't think this is efficient way to write a SQL chaala clumsy ga vunnadi go for SP and use temp table or hash table @gr33d use temp tables following the logic and wrap all of them into a SP or if its only a query in a view use only ctes remove all the sub queries..
pandugadu999 Posted December 17, 2013 Report Posted December 17, 2013 use stored proc, instead of CTE's
tommy Posted December 17, 2013 Author Report Posted December 17, 2013 bane cheparu anadru....but idhi naa modati job ...stored procedures naaku raavu......yaa tellodu raasadu..
ghazni Posted December 17, 2013 Report Posted December 17, 2013 when CTE is not fast look for table variables and temp tables....
SUbba LIngam Posted December 17, 2013 Report Posted December 17, 2013 videsi ayyuntadu..ltt.. ante enti artham, baaga raasinatta leka chandalanga na
tommy Posted December 17, 2013 Author Report Posted December 17, 2013 evaraina koncham marchi eyandi bhayya..
Recommended Posts