Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2013-05-16
8
Medium Priority
?
274 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

 
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

Technology Partners: 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!

Question has a verified solution.

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

The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

715 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