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 > self join query

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 09-29-06, 14:56
hnhegde hnhegde is offline
Registered User
 
Join Date: Aug 2006
Posts: 6
self join query

Hi Friends,

There is a table with two columns containing a long account number and a short account number. like this:
LongAcctNum ShortAcctNum
A00146000 A00146
A00146000 1000146000
Q78886001 Q78886
00351250246 351250246

Each long account number should be having one short account number. But some have two. If I have to pick out such long acct. numbers, what kind of query should I write? Can I use a self-join on this table, something like:
select LongAcctNum, ShortAcctNum from table1 t1 where t1.LongAcctNum = t1.LongAcctNum and t1.ShortAcctNum != t1.ShortAcctNum.

I tried this way; it doesn't work. Can my friends here please correct me?

Thanks!
Reply With Quote
  #2 (permalink)  
Old 09-29-06, 15:42
ARWinner ARWinner is offline
Registered User
 
Join Date: Jan 2003
Posts: 3,575
An easier way is to let DB2 count them for you. Like this:

with temp1 (longAcctnum,quantity) as (
select longacctnum,count(*) from table1 group by longacctnum)
select tp1.longacctnum,t1.shortacctnum from temp1 as tp1 inner join table1 as t1 on (tp1.longacctnum = t1.longacctnum) where tp1.quantity > 1

HTH

Andy
Reply With Quote
  #3 (permalink)  
Old 09-29-06, 16:08
hnhegde hnhegde is offline
Registered User
 
Join Date: Aug 2006
Posts: 6
Hi Andy,

Thanks for the reply. Can this be done without using temporary tables?
Reply With Quote
  #4 (permalink)  
Old 09-29-06, 16:31
ARWinner ARWinner is offline
Registered User
 
Join Date: Jan 2003
Posts: 3,575
That is not a temporary table. It is a common-table-expression. I just named it temp1.

Andy
Reply With Quote
  #5 (permalink)  
Old 09-29-06, 16:40
hnhegde hnhegde is offline
Registered User
 
Join Date: Aug 2006
Posts: 6
Oh, ok thanks. I am new to db2. I have earlier worked with Sybase and hadn't come accross common-table-expressions.
Thank You for your response.
Reply With Quote
  #6 (permalink)  
Old 09-30-06, 18:37
Peter.Vanroose Peter.Vanroose is offline
Registered User
 
Join Date: Sep 2004
Location: Belgium
Posts: 1,079
Quote:
Originally Posted by hnhegde
select LongAcctNum, ShortAcctNum from table1 t1 where t1.LongAcctNum = t1.LongAcctNum and t1.ShortAcctNum != t1.ShortAcctNum
This is no self join: you must specify the table twice in the "from" condition for it to be a self join, e.g.:
Code:
select t1.LongAcctNum, t1.ShortAcctNum, t2.ShortAcctNum
from   table1 AS t1 LEFT OUTER JOIN table1 AS t2
       ON t1.LongAcctNum = t2.LongAcctNum and t1.ShortAcctNum != t2.ShortAcctNum
__________________
--_Peter Vanroose,
__IBM Certified Database Administrator, DB2 9 for z/OS
__IBM Certified Application Developer
__ABIS Training and Consulting
__http://www.abis.be/
Reply With Quote
  #7 (permalink)  
Old 09-30-06, 19:03
Peter.Vanroose Peter.Vanroose is offline
Registered User
 
Join Date: Sep 2004
Location: Belgium
Posts: 1,079
Quote:
Originally Posted by ARWinner
with temp1 (longAcctnum,quantity) as ...
This is a CTE (common table expression). Not available in DB2 prior to version 8.
The same result can be obtained with
Code:
select longAcctnum, count(*) AS quantity
from   table1
group by longAcctnum
having  count(*) > 1
__________________
--_Peter Vanroose,
__IBM Certified Database Administrator, DB2 9 for z/OS
__IBM Certified Application Developer
__ABIS Training and Consulting
__http://www.abis.be/
Reply With Quote
  #8 (permalink)  
Old 10-04-06, 20:49
sathyaram_s sathyaram_s is offline
Super Moderator
 
Join Date: Aug 2001
Location: UK
Posts: 4,534
On LUW, it has been available atleast from V7 ... On ZOS, CTEs were not introduced till V8

Sathyaram
__________________
Visit the new-look IDUG Website , register to gain access to the excellent content.
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