We help IT Professionals succeed at work.
Get Started

optimizing oracle query

anumoses
anumoses asked
on
379 Views
Last Modified: 2015-04-23
I know many experts have helped me getting to this point. I am posting a query with sample data. Can someone help me to optimize it? Its taking time to execute.

select ym,customer_id,patient_name,

       nullif(substr(month_string,1,instr(month_string,',') - 1),'~') sun_1,

       nullif(substr(month_string,instr(month_string,',',1,1) + 1,instr(month_string,',',1,2) - instr(month_string,',',1,1) - 1),'~') mon_1,

       nullif(substr(month_string,instr(month_string,',',1,2) + 1,instr(month_string,',',1,3) - instr(month_string,',',1,2) - 1),'~') tue_1,

       nullif(substr(month_string,instr(month_string,',',1,3) + 1,instr(month_string,',',1,4) - instr(month_string,',',1,3) - 1),'~') wed_1,

       nullif(substr(month_string,instr(month_string,',',1,4) + 1,instr(month_string,',',1,5) - instr(month_string,',',1,4) - 1),'~') thu_1,

       nullif(substr(month_string,instr(month_string,',',1,5) + 1,instr(month_string,',',1,6) - instr(month_string,',',1,5) - 1),'~') fri_1,

       nullif(substr(month_string,instr(month_string,',',1,6) + 1,instr(month_string,',',1,7) - instr(month_string,',',1,6) - 1),'~') sat_1,

       substr(month_string,instr(month_string,',',1,7) + 1,instr(month_string,',',1,8) - instr(month_string,',',1,7) - 1) sun_2,

       substr(month_string,instr(month_string,',',1,8) + 1,instr(month_string,',',1,9) - instr(month_string,',',1,8) - 1) mon_2,

       substr(month_string,instr(month_string,',',1,9) + 1,instr(month_string,',',1,10) - instr(month_string,',',1,9) - 1) tue_2,

       substr(month_string,instr(month_string,',',1,10) + 1,instr(month_string,',',1,11) - instr(month_string,',',1,10) - 1) wed_2,

       substr(month_string,instr(month_string,',',1,11) + 1,instr(month_string,',',1,12) - instr(month_string,',',1,11) - 1) thu_2,

       substr(month_string,instr(month_string,',',1,12) + 1,instr(month_string,',',1,13) - instr(month_string,',',1,12) - 1) fri_2,

       substr(month_string,instr(month_string,',',1,13) + 1,instr(month_string,',',1,14) - instr(month_string,',',1,13) - 1) sat_2,

       substr(month_string,instr(month_string,',',1,14) + 1,instr(month_string,',',1,15) - instr(month_string,',',1,14) - 1) sun_3,

       substr(month_string,instr(month_string,',',1,15) + 1,instr(month_string,',',1,16) - instr(month_string,',',1,15) - 1) mon_3,

       substr(month_string,instr(month_string,',',1,16) + 1,instr(month_string,',',1,17) - instr(month_string,',',1,16) - 1) tue_3,

       substr(month_string,instr(month_string,',',1,17) + 1,instr(month_string,',',1,18) - instr(month_string,',',1,17) - 1) wed_3,

       substr(month_string,instr(month_string,',',1,18) + 1,instr(month_string,',',1,19) - instr(month_string,',',1,18) - 1) thu_3,

       substr(month_string,instr(month_string,',',1,19) + 1,instr(month_string,',',1,20) - instr(month_string,',',1,19) - 1) fri_3,

       substr(month_string,instr(month_string,',',1,20) + 1,instr(month_string,',',1,21) - instr(month_string,',',1,20) - 1) sat_3,

       substr(month_string,instr(month_string,',',1,21) + 1,instr(month_string,',',1,22) - instr(month_string,',',1,21) - 1) sun_4,

       substr(month_string,instr(month_string,',',1,22) + 1,instr(month_string,',',1,23) - instr(month_string,',',1,22) - 1) mon_4,

       substr(month_string,instr(month_string,',',1,23) + 1,instr(month_string,',',1,24) - instr(month_string,',',1,23) - 1) tue_4,

       substr(month_string,instr(month_string,',',1,24) + 1,instr(month_string,',',1,25) - instr(month_string,',',1,24) - 1) wed_4,

       substr(month_string,instr(month_string,',',1,25) + 1,instr(month_string,',',1,26) - instr(month_string,',',1,25) - 1) thu_4,

       substr(month_string,instr(month_string,',',1,26) + 1,instr(month_string,',',1,27) - instr(month_string,',',1,26) - 1) fri_4,

       substr(month_string,instr(month_string,',',1,27) + 1,instr(month_string,',',1,28) - instr(month_string,',',1,27) - 1) sat_4,

       nullif(substr(month_string,instr(month_string,',',1,28) + 1,instr(month_string,',',1,29) - instr(month_string,',',1,28) - 1),'~') sun_5,

       nullif(substr(month_string,instr(month_string,',',1,29) + 1,instr(month_string,',',1,30) - instr(month_string,',',1,29) - 1),'~') mon_5,

       nullif(substr(month_string,instr(month_string,',',1,30) + 1,instr(month_string,',',1,31) - instr(month_string,',',1,30) - 1),'~') tue_5,

       nullif(substr(month_string,instr(month_string,',',1,31) + 1,instr(month_string,',',1,32) - instr(month_string,',',1,31) - 1),'~') wed_5,

       nullif(substr(month_string,instr(month_string,',',1,32) + 1,instr(month_string,',',1,33) - instr(month_string,',',1,32) - 1),'~') thu_5,

       nullif(substr(month_string,instr(month_string,',',1,33) + 1,instr(month_string,',',1,34) - instr(month_string,',',1,33) - 1),'~') fri_5,

       nullif(substr(month_string,instr(month_string,',',1,34) + 1,instr(month_string,',',1,35) - instr(month_string,',',1,34) - 1),'~') sat_5,

       nullif(substr(month_string,instr(month_string,',',1,35) + 1,instr(month_string,',',1,36) - instr(month_string,',',1,35) - 1),'~') sun_6,

       nullif(substr(month_string,instr(month_string,',',1,36) + 1,instr(month_string,',',1,37) - instr(month_string,',',1,36) - 1),'~') mon_6,

       nullif(substr(month_string,instr(month_string,',',1,37) + 1,instr(month_string,',',1,38) - instr(month_string,',',1,37) - 1),'~') tue_6

  from (select ym,customer_id,patient_name,

               case when to_date(ym,'yyyymm') - trunc(to_date(ym,'yyyymm'),'iw') + 1 < 7

                    then rpad('~,',2 * (to_date(ym,'yyyymm') - trunc(to_date(ym,'yyyymm'),'iw') + 1),'~,')

               end||

               days_in_month||

               rpad('~,',

                    2 * (length(replace(case when to_date(ym,'yyyymm') - trunc(to_date(ym,'yyyymm'),'iw') + 1 < 7

                                             then rpad('~,',2 * (to_date(ym,'yyyymm') - trunc(to_date(ym,'yyyymm'),'iw') + 1),'~,')

                                        end||days_in_month,

                                        ','

                                       )

                               ) -

                         length(case when to_date(ym,'yyyymm') - trunc(to_date(ym,'yyyymm'),'iw') + 1 < 7

                                     then rpad('~,',2 * (to_date(ym,'yyyymm') - trunc(to_date(ym,'yyyymm'),'iw') + 1),'~,')

                                end||days_in_month

                               ) +

                         38

                   ),

                   '~,'

                   ) month_string

          from (select to_char(trunc(ih.invoice_date,'mm'),'yyyymm') ym,

                       ih.customer_id,

                       patient_name,

                       to_char(count(case when to_char(ih.invoice_date,'dd') = '01' then 1 end))||','||

                       to_char(count(case when to_char(ih.invoice_date,'dd') = '02' then 1 end))||','||

                       to_char(count(case when to_char(ih.invoice_date,'dd') = '03' then 1 end))||','||

                       to_char(count(case when to_char(ih.invoice_date,'dd') = '04' then 1 end))||','||

                       to_char(count(case when to_char(ih.invoice_date,'dd') = '05' then 1 end))||','||

                       to_char(count(case when to_char(ih.invoice_date,'dd') = '06' then 1 end))||','||

                       to_char(count(case when to_char(ih.invoice_date,'dd') = '07' then 1 end))||','||

                       to_char(count(case when to_char(ih.invoice_date,'dd') = '08' then 1 end))||','||

                       to_char(count(case when to_char(ih.invoice_date,'dd') = '09' then 1 end))||','||

                       to_char(count(case when to_char(ih.invoice_date,'dd') = '10' then 1 end))||','||

                       to_char(count(case when to_char(ih.invoice_date,'dd') = '11' then 1 end))||','||

                       to_char(count(case when to_char(ih.invoice_date,'dd') = '12' then 1 end))||','||

                       to_char(count(case when to_char(ih.invoice_date,'dd') = '13' then 1 end))||','||

                       to_char(count(case when to_char(ih.invoice_date,'dd') = '14' then 1 end))||','||

                       to_char(count(case when to_char(ih.invoice_date,'dd') = '15' then 1 end))||','||

                       to_char(count(case when to_char(ih.invoice_date,'dd') = '16' then 1 end))||','||

                       to_char(count(case when to_char(ih.invoice_date,'dd') = '17' then 1 end))||','||

                       to_char(count(case when to_char(ih.invoice_date,'dd') = '18' then 1 end))||','||

                       to_char(count(case when to_char(ih.invoice_date,'dd') = '19' then 1 end))||','||

                       to_char(count(case when to_char(ih.invoice_date,'dd') = '20' then 1 end))||','||

                       to_char(count(case when to_char(ih.invoice_date,'dd') = '21' then 1 end))||','||

                       to_char(count(case when to_char(ih.invoice_date,'dd') = '22' then 1 end))||','||

                       to_char(count(case when to_char(ih.invoice_date,'dd') = '23' then 1 end))||','||

                       to_char(count(case when to_char(ih.invoice_date,'dd') = '24' then 1 end))||','||

                       to_char(count(case when to_char(ih.invoice_date,'dd') = '24' then 1 end))||','||

                       to_char(count(case when to_char(ih.invoice_date,'dd') = '25' then 1 end))||','||

                       to_char(count(case when to_char(ih.invoice_date,'dd') = '27' then 1 end))||','||

                       to_char(count(case when to_char(ih.invoice_date,'dd') = '28' then 1 end))||','||

                       case when to_char(last_day(trunc(ih.invoice_date,'mm')),'dd') >= '29'

                            then to_char(count(case when to_char(ih.invoice_date,'dd') = '29' then 1 end))

                            else '~'

                       end||','||

                       case when to_char(last_day(trunc(ih.invoice_date,'mm')),'dd') >= '30'

                            then to_char(count(case when to_char(ih.invoice_date,'dd') = '30' then 1 end))

                            else '~'

                       end||','||

                       case when to_char(last_day(trunc(ih.invoice_date,'mm')),'dd') >= '31'

                            then to_char(count(case when to_char(ih.invoice_date,'dd') = '31' then 1 end))

                            else '~'

                       end||',' days_in_month

                  from tab1 ih,

                       tab2 id,

                       tab4 ip,

                       tab3 vp

                 where id.invoice_number = ih.invoice_number

                   and id.item_id = vp.product_code

                   and id.item_id = ip.item_id

                   and ip.item_type in ('P')

                   and ih.customer_id = 'WAD-EX0128'

                   and ih.invoice_date between to_date(:date_from,'DD-MON-YYYY') and to_date(:date_to,'DD-MON-YYYY')

                   and vp.inv_product_type in ('RBC','LRBC','LPHER','PHER','FFP','FP24','CRYO')

                 group by ih.customer_id,patient_name,trunc(ih.invoice_date,'mm')

               )

       )

order by ym,customer_id,patient_name
							
 

Open in new window

sample-date.txt
Comment
Watch Question
Database Developer & Administrator
CERTIFIED EXPERT
Most Valuable Expert 2011
Top Expert 2012
Commented:
This problem has been solved!
Unlock 2 Answers and 12 Comments.
See Answers
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE