Home > Ask the SQL Server Experts > Questions & Answers > Derived table in DB2
Ask The SQL Server Expert: Questions & Answers
EMAIL THIS

Derived table in DB2

Rudy Limeback EXPERT RESPONSE FROM: Rudy Limeback

Pose a Question
Other SQL Server Categories
Meet all SQL Server Experts
Become an Expert for this site


Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   


>
QUESTION POSED ON: 27 October 2003

Can you code a subquery as part of a select statement in DB2? The query will work in SQL Server but gets an error in DB2.

select B.CUSNO, B.Delays, 
B.ShpTons,                              
 ( SELECT Sum(CVIEW1.AC_WT/2000.0)
   from WAREHOUS.CORD_ORDER_VIEW  CVIEW1                
   where CVIEW1.CUS_NO = B.CUSNO 
   and CVIEW1.PROM_ACK_DT >= '2003-08-01' 
   and CVIEW1.PROM_ACK_DT  < '2003-09-01'
   and CVIEW1.ORD_STS_CD NOT IN ('C','I') )  as 
OrdTons                                            
FROM ... 

>

If you've tried this query in DB2, and it failed, then presumably the answer is no (I don't actually have a copy of DB2 on which to test it). But do not despair; there is often more than one way to remove a feline's outer integument.

Based on the from-clause specification in the DB2 SQL Reference for Cross-Platform Development v1.1 manual, you can rewite your query using a derived table (which DB2 calls a "nested table expression").

In your original query, you have a correlation within the subquery between the CUS_NO column and the outer query's B.CUSNO column. This correlation can be rewritten as the JOIN condition.

select B.CUSNO, B.Delays, B.ShpTons
     , DT.AC_tons   as OrdTons
  from ( select CUS_NO
              , Sum(AC_WT/2000.0)    as AC_tons
           from WAREHOUS.CORD_ORDER_VIEW              
          where PROM_ACK_DT >= '2003-08-01' 
            and PROM_ACK_DT  < '2003-09-01'
            and ORD_STS_CD NOT IN ('C','I')
         group
             by CUS_NO ) as DT
inner
  join B
    on DT.CUS_NO = B.CUSNO


Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   



RELATED RESOURCES
2020software.com, trial software downloads for accounting software, ERP software, CRM software and business software systems
Search Bitpipe.com for the latest white papers and business webcasts
Whatis.com, the online computer dictionary



Search and Browse the Expert Answer Center
Search and browse more than 25,000 question and answer pairs from more than 250 TechTarget industry experts.
Browse our Expert Advice



SQL Solutions - SQL Database Design
HomeNewsTopicsITKnowledge ExchangeTipsAsk the ExpertsMultimediaWhite PapersIT Downloads
About Us  |  Contact Us  |  For Advertisers  |  For Business Partners  |  Site Index  |  RSS
SEARCH 
TechTarget provides technology professionals with the information they need to perform their jobs - from developing strategy, to making cost-effective purchase decisions and managing their organizations' technology projects - with its network of technology-specific websites, events and online magazines.

TechTarget Corporate Web Site  |  Media Kits  |  Site Map




All Rights Reserved, Copyright 2005 - 2009, TechTarget | Read our Privacy Policy
  TechTarget - The IT Media ROI Experts