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 > Duplicates in Same Column/Table, Different Where-Clauses

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 10-21-11, 14:41
Questro Questro is offline
Registered User
 
Join Date: Oct 2011
Posts: 2
Duplicates in Same Column/Table, Different Where-Clauses

I may have overcomplicated this task, I’ve tried different approaches to filter the results, but my query still gives more than I asked for. I’m looking for duplicate values (vendor) on 2 tables, using different where-clauses. I want to know if the same vendor exists in 2 different departments of the same company.

The query that’s gotten me closest is this:

SELECT distinct a.vendor, c.dept,
COUNT(a.vendor) as times_dupl
FROM VendorTbl a,
DptTbl b,
DescTbl c
where (c.dept = 123 or
c.dept = 456)
and a.vendor = b.vendor
and b.desc = c.desc
GROUP BY a.vendor, c.dept
HAVING ( COUNT(a.vendor) > 1 )
with ur;


I started out with an implict JOIN, then UNIONed 2 different SELECTs with where clauses having the different department numbers (123 and 456), then pared it down to the above single SELECT statement. But it still produces more results than just a single occurrence of the same vendor in both departments 123 and 456. What am I missing? Thanks for looking.
Reply With Quote
  #2 (permalink)  
Old 10-21-11, 14:57
Lenny77 Lenny77 is offline
Registered User
 
Join Date: Jul 2009
Location: NY
Posts: 886
maybe you have to add something like b.dept = c.dept ....

Who knows structure of your tables ?
Reply With Quote
  #3 (permalink)  
Old 10-21-11, 15:41
Questro Questro is offline
Registered User
 
Join Date: Oct 2011
Posts: 2
Quote:
Originally Posted by Lenny77 View Post
maybe you have to add something like b.dept = c.dept
Good suggestion. I'll add more like columns to where-clause to winnow results down and post query that gives correct results. Thanks.
Reply With Quote
Reply

Tags
different where clauses, duplicate columns, same table

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