If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > DB2 > Required help in re-writing query

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 07-11-11, 03:42
sharma03 sharma03 is offline
Registered User
 
Join Date: May 2011
Posts: 4
Required help in re-writing query

Hi,

I need help i rewriting belwo query.

In the below query every time when vehicle is in ('DOWN','REPAIR') status it will pick strtmil from bc.billhst else it will pick abmil from ab.billmilg.


I want to join with ab.billmilg with bc.billhst only when vehicle is in ( 'DOWN','REPAIR') not all the time.

Can any one please help me in rewriting below query.

Query
------


select abcorpid,abcust,abunitid, abmil,
case when vehicle in ('DOWN','REPAIR') then strtmil else abmil end km, anstrdt
from (


select abcorpid, abcust,abunitid, abmil, min(bcstrtmil) strtmil,abstrdt
from bc.billhist right outer join
(select abcorpid, abunitid,abcust max(abmil) abmil, max(abstrdt) abstrdt
from ab.billmilg
where abcust IN ('629874','654897')
and abuse = 'K'
and abclass = 'M'
group by abcorpid, abunitid, abcust
) billmilg
on abcorpid = bccorpid
and abunitid = bcunitid
and bcuse = 'K'
and bcclass = 'M'
and (substr(sbdt,1,5) = substr(cdbdte,1,5)
or substr(char(date(qgpl.cyy2date(sbdt))),1,7) =
substr(char(date(qgpl.cyy2date(cdbdte))- 1 months),1,7))
group by abcorpid, abcust,abunitid, abmil,abstrdt
) X,

de.vehicle
where abcorpid = decorpid
and abunitid = deunitid
order by abcorpid,abcust,abunitid
Reply With Quote
  #2 (permalink)  
Old 07-11-11, 07:43
tonkuma tonkuma is offline
Registered User
 
Join Date: Feb 2008
Location: Japan
Posts: 2,193
Add a predicate "vehicle in ( 'DOWN','REPAIR')" into the ON condition of outer join.

You need not care wheather column vehicle is in table ab.billmilg or table bc.billhst, if not both.
Only if column vehicle were in both of table ab.billmilg and table bc.billhst,
you should qualify it to specify which vehicle.
Reply With Quote
  #3 (permalink)  
Old 07-11-11, 08:50
tonkuma tonkuma is offline
Registered User
 
Join Date: Feb 2008
Location: Japan
Posts: 2,193
Two notes:

Note 1): You might know this, but I want to confirm it.
if you join ab.billmilg with bc.billhst only when vehicle is in ('DOWN','REPAIR'),
then
case when vehicle in ('DOWN','REPAIR') then strtmil else abmil end
can be replaced by
COALESCE(strtmil , abmil)

Note 2):
Apart from your question,
it might be not a good practice to mix join by traditional way(comma and where conditions) and join by join keyword,
even if one was inside a subquery and another was in outer select.
Reply With Quote
  #4 (permalink)  
Old 07-11-11, 09:18
tonkuma tonkuma is offline
Registered User
 
Join Date: Feb 2008
Location: Japan
Posts: 2,193
What are your DB2 version/release and platform?
Reply With Quote
  #5 (permalink)  
Old 07-11-11, 12:10
tonkuma tonkuma is offline
Registered User
 
Join Date: Feb 2008
Location: Japan
Posts: 2,193
1) I thought that nested table expressions were not necessary, like...
Code:
select abcorpid
     , abcust
     , abunitid
     , max(abmil) abmil
     , COALESCE(
          min(bcstrtmil)
        , max(abmil)
       ) km
     , max(abstrdt) abstrdt
 from
       ab.billmilg
 LEFT  outer join
       bc.billhist
   on  abcorpid = bccorpid
   and abunitid = bcunitid
   and bcuse    = 'K'
   and bcclass  = 'M'
   and
   (   substr(sbdt,1,5) = substr(cdbdte,1,5)
    or substr(char(date(qgpl.cyy2date(sbdt))),1,7) = substr(char(date(qgpl.cyy2date(cdbdte)) - 1 months),1,7)
   )
   AND vehicle in ('DOWN','REPAIR')
 INNER JOIN
       de.vehicle
   ON  abcorpid = decorpid
   and abunitid = deunitid
 group by
       abcorpid
     , abcust
     , abunitid
 order by
       abcorpid
     , abcust
     , abunitid
2) Although I don't know the format of sbdt and cdbdte and function of qgpl.cyy2date,
I guessed that
( substr(sbdt,1,5) = substr(cdbdte,1,5)
or substr(char(date(qgpl.cyy2date(sbdt))),1,7) = substr(char(date(qgpl.cyy2date(cdbdte)) - 1 months),1,7)
)
might be replaced by
substr(sbdt,1,5) IN
( substr(cdbdte,1,5)
, substr(char(date(qgpl.cyy2date(cdbdte)) - 1 months),3,5)
)
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On