Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2013-05-16
8
Medium Priority
?
276 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 70

Accepted Solution

by:
Scott Pletcher earned 2000 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 36

Author Closing Comment

by:SidFishes
ID: 39172171
Quick & exactly correct. Thanks
0
 
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 70

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

NEW Veeam Backup for Microsoft Office 365 1.5

With Office 365, it’s your data and your responsibility to protect it. NEW Veeam Backup for Microsoft Office 365 eliminates the risk of losing access to your Office 365 data.

Question has a verified solution.

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

Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

824 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