Jump to content

Sql Query Help..p.t


Recommended Posts

Posted

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
 

...

  • Replies 33
  • Created
  • Last Reply

Top Posters In This Topic

  • tommy

    10

  • loveindia

    5

  • SwamyRaRa

    3

  • siritptpras

    2

Popular Days

Top Posters In This Topic

Posted

CITI#H@  CITI#H@  CITI#H@

 

ee code evaru rasinaru baaaa..........

Posted

functions vunte kasta time padutundi more over chaala pedda SQL

 

why don't you go for STored procedure?

Posted

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

Posted

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..

Posted

bane cheparu anadru....but idhi naa modati job ...stored procedures naaku raavu......yaa tellodu raasadu..

Posted

videsi ayyuntadu..ltt..

 

ante enti artham, baaga raasinatta leka chandalanga na fdfgdfd-o.gif

×
×
  • Create New...