Solved

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

Posted on 2013-05-16
8
258 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:
ScottPletcher 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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
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:ScottPletcher
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Having an SQL database can be a big investment for a small company. Hardware, setup and of course, the price of software all add up to a big bill that some companies may not be able to absorb.  Luckily, there is a free version SQL Express, but does …
Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
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.

747 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

16 Experts available now in Live!

Get 1:1 Help Now