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
Solved

SQL-SubQuery to remove duplicates

Posted on 2014-11-13
7
153 Views
Last Modified: 2014-11-14
Hello,
I have the below query:
select * from expeditetbl1 ex 
left outer join exp_dummytbl expd on ex.program=expd.pgm_codes
 where
 ex.open_qty <>0
and vendor_code='HAL601' and po_num='41309844'
and emp_num='092'

select * from exp_dummytbl where emp_num='092' and pGM_CODES='661'

Open in new window


The second query has duplicates for that combination so when I do the left join in the first query its returning 2 rows.How can I modify the first query to a subquery so that the duplicates are eliminated.
Thanks.
0
Comment
Question by:Star79
7 Comments
 
LVL 8

Accepted Solution

by:
vr6r earned 300 total points
ID: 40440912
Try this for your first query...

select * from expeditetbl1 ex 
left join 
(
    select 
        emp_num, pGM_CODES 
    from  exp_dummytbl
    group by emp_num, pGM_CODES
) Q ON ex.program = Q.pGM_CODES
where
ex.open_qty <>0
and vendor_code='HAL601' and po_num='41309844'
and emp_num='092'

Open in new window


If you need other fields out of the exp_dummytbl in your first query this may not work because it's limiting the result set to just the emp_num & pgm_codes column, but if you just need to prevent duplicate matches on pgm_codes and emp_num it should work for you.

Hope this helps.
0
 
LVL 5

Assisted Solution

by:TONY TAYLOR
TONY TAYLOR earned 100 total points
ID: 40441063
My experience says that at times we want only the first result and it depends upon multiple different factors.  I would suggest you look at ROW_NUMBER.

Reference:
http://msdn.microsoft.com/en-us/library/ms186734.aspx

Example:
SELECT * 
FROM 
	expeditetbl1 ex 
	LEFT JOIN (
		SELECT *, ROW_NUMBER() OVER(PARTITION BY pGM_CODES ORDER BY emp_num) AS ROW_NUM
		FROM exp_dummytbl
	) expd ON ex.program = expd.pGM_CODES AND expd.ROW_NUM = 1 
where ex.open_qty <>0
and vendor_code='HAL601' and po_num='41309844'
and emp_num='092'

Open in new window


The advantage here is that if you want more classifications (pGM_CODES AND emp_num, then you could add "emp_num" to the "PARTITION BY" section.  If you only wanted the pGM_CODES and the FIRST emp_num number that it comes to, then leave it as the example above.

This example provides versatility in how you want the results and potentially more versatility during the building process.
0
 
LVL 23

Expert Comment

by:Racim BOUDJAKDJI
ID: 40441068
Use DISTINCT or specify a scheme by which you consider a row a duplicate of another.

Hope this helps
0
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.

 
LVL 18

Assisted Solution

by:JR2003
JR2003 earned 100 total points
ID: 40441323
OUTER APPLY with TOP(1) is the way to go with this one:

select * 
 from expeditetbl1 ex 
OUTER APPLY (SELECT TOP(1) *
               FROM exp_dummytbl expd 
              WHERE expd.pgm_codes = ex.program) as expd
 where ex.open_qty <>0
   and vendor_code='HAL601' 
   and po_num='41309844'
   and emp_num='092'

Open in new window

0
 
LVL 5

Expert Comment

by:TONY TAYLOR
ID: 40441329
Outer Apply is Awesome!  That is a great example.
0
 
LVL 5

Expert Comment

by:TONY TAYLOR
ID: 40441346
I'm not opposed to the distribution of points, just curious....

Can you post an explanation/what your end solution was?  I think that it MIGHT have depended on what you were actually wanting.
0
 

Author Comment

by:Star79
ID: 40443394
The first answer gave me the right results but I also noticed the others worked as well.
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

There have been several questions about Large Transaction Log Files in SQL Server 2008, and how to get rid of them when disk space has become critical. This article will explain how to disable full recovery and implement simple recovery that carries…
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

856 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