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 > SQL / UNION / temporary table

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 01-12-04, 10:05
c149187 c149187 is offline
Registered User
 
Join Date: Jul 2003
Posts: 50
SQL / UNION / temporary table

Hi

I have the following query:

Select distinct FK_ADRESSE_UUID from ADU.UTADU_DET_PRAEMIE where UNTERBRANCHE in ( 93001,93006,93009,98020,98021,98027,99020,99021,99 023,99024,99027,99028,99029 ) and STATUS <> 4 and (PLZ='8600')
UNION
Select distinct FK_ADRESSE_UUID from ADU.UTADU_VERTRAG where BRANCHE in ( 94,52010,88000,89000 ) and STATUS <> 4 and (PLZ='8600')

How can I get the number of rows of the result WITHOUT use of a temporary table ?

(UDB 8.1, Unix)

Marc
Reply With Quote
  #2 (permalink)  
Old 01-12-04, 11:16
ARWinner ARWinner is offline
Registered User
 
Join Date: Jan 2003
Posts: 3,575
Marc,

Try:

with temp1 as (Select distinct FK_ADRESSE_UUID from ADU.UTADU_DET_PRAEMIE where UNTERBRANCHE in ( 93001,93006,93009,98020,98021,98027,99020,99021,99
023,99024,99027,99028,99029 ) and STATUS <> 4 and (PLZ='8600')
UNION
Select distinct FK_ADRESSE_UUID from ADU.UTADU_VERTRAG where BRANCHE in ( 94,52010,88000,89000 ) and STATUS <> 4 and (PLZ='8600') ) select count(*) from temp1;

HTH

Andy
Reply With Quote
  #3 (permalink)  
Old 01-13-04, 03:47
grofaty grofaty is offline
Registered User
 
Join Date: Jan 2003
Posts: 1,570
Hi,

Solution of ARwinner also generates temporaly table.

I thing there is no way you can do it without temporaly table.

Hope this helps,
Grofaty
Reply With Quote
  #4 (permalink)  
Old 01-13-04, 07:51
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
tip: remove the DISTINCT from each of the subqueries in the union, it is not necessary because UNION itself removes duplicate rows

does this work? i don't have db2 to test on --
Code:
select count(*)
  from (
select FK_ADRESSE_UUID 
  from ADU.UTADU_DET_PRAEMIE 
 where UNTERBRANCHE 
    in ( 93001,93006,93009,98020,98021,98027
       , 99020,99021,99023,99024,99027,99028,99029 )
   and STATUS <> 4 
   and PLZ='8600'
union 
select FK_ADRESSE_UUID 
  from ADU.UTADU_VERTRAG
 where BRANCHE 
    in ( 94,52010,88000,89000 )
   and STATUS <> 4 
   and PLZ='8600'
       )
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #5 (permalink)  
Old 01-15-04, 08:21
c149187 c149187 is offline
Registered User
 
Join Date: Jul 2003
Posts: 50
Thanks, it works like this:

select count(*) from (select FK_ADRESSE_UUID from ADU.UTADU_DET_PRAEMIE where UNTERBRANCHE in ( 93001,93006,93009,98020,98021,98027 , 99020,99021,99023,99024,99027,99028,99029 ) and STATUS <> 4 and PLZ='8600'union select FK_ADRESSE_UUID from ADU.UTADU_VERTRAG where BRANCHE in ( 94,52010,88000,89000 ) and STATUS <> 4 and PLZ='8600' ) as T

-> I had to put "as T" at the end of your statement

Marc
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