Solved

1-Many join with duplicate id's in one table

Posted on 2013-05-16
8
260 Views
Last Modified: 2013-05-16
Have a table (from our accounting software so I can't change the way it works)

inItems
101-00002      AP Flour      
101-00003      Salt

I have another table that lists the possible Units Of Measure for each item

inUOM
101-00002      BG22
101-00002      GR
101-00002      KG
101-00003      BX25
101-00003      GR
101-00003      KG

I need to get the item details & the UOM's that are not GR or KG

101-00002      AP Flour      BG22
101-00003      Salt               BX25

Tried a bunch of different Join combinations but all return everything

ideas?
0
Comment
Question by:SidFishes
  • 2
  • 2
  • 2
  • +2
8 Comments
 
LVL 69

Accepted Solution

by:
Scott Pletcher earned 500 total points
ID: 39172148
SELECT
    i.item, u.UOM
FROM dbo.inItems i
INNER JOIN dbo.inUOM u ON
    u.item = i.item AND
    u.uom NOT IN ('GR', 'KG')
ORDER BY
    i.item, u.UOM


If you want to list an item even if it has no (other) uom, use a LEFT OUTER JOIN instead of an INNER JOIN.
0
 
LVL 16

Expert Comment

by:Surendra Nath
ID: 39172154
first thing you have given us the table names but not the column names,

SO I assuming the below

100-0001 will be under the column itemID
BG22,KG,GR.... will be under the column UOM

if that is not correct, change them respectively in the below query

ok try this

select i.*,U.UOM 
from inItems i
JOIN inUOM U
ON i.itemID = U.itemID
AND u.UOM not in( 'KG','GR')

Open in new window

0
 

Expert Comment

by:VuNguyen84
ID: 39172167
Hi,

If the first column named "id", and second column named "detail" of every table. You can try this:

Select * from inUOM
Left join inItems on inUOM.id = inItems.id
Where inUOM.detail <> 'GR' and inUOM.detail <> 'KG'
0
 
LVL 36

Author Closing Comment

by:SidFishes
ID: 39172171
Quick & exactly correct. Thanks
0
Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

 
LVL 8

Expert Comment

by:didnthaveaname
ID: 39172189
I'm going to propose one slight variation to Scott's answer:

SELECT
    i.item, u.UOM
FROM dbo.inItems i
INNER JOIN dbo.inUOM u ON
    u.item = i.item AND
    u.uom <> 'GR' AND
    u.uom <> 'KG'
ORDER BY
    i.item, u.UOM

He may want to weigh in on this, but it's my understanding that the IN will evaluate as logical ORs, which will expand the execution plans and could result in a sub-optimal execution plan being used.

Edit: just a proposed potential optimization to his answer which was, as you already said, spot on =)
0
 
LVL 16

Expert Comment

by:Surendra Nath
ID: 39172222
@didnthaveaname
do you really think the person asking this question needs the optimization of the query, in most of the cases over here they dont. The need the job done.
I might be wrong though, but this is my understanding for being a memeber from last one month.
0
 
LVL 8

Expert Comment

by:didnthaveaname
ID: 39172229
The question's closed. The points are assigned (correctly).  It was merely suggested to try and be helpful.
0
 
LVL 69

Expert Comment

by:Scott Pletcher
ID: 39172350
SQL should produce exactly the same query plan from either coding.  I think the NOT IN is easier to code, read and maintain, so I prefer it to multiple <>s connected by AND, which I think is much less intuitively understandable.
0

Featured Post

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

When you hear the word proxy, you may become apprehensive. This article will help you to understand Proxy and when it is useful. Let's talk Proxy for SQL Server. (Not in terms of Internet access.) Typically, you'll run into this type of problem w…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

27 Experts available now in Live!

Get 1:1 Help Now