Solved

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

Posted on 2013-05-16
8
267 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Nowadays, some of developer are too much worried about data. Who is using data, who is updating it etc. etc. Because, data is more costlier in term of money and information. So security of data is focusing concern in days. Lets' understand the Au…
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

770 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