Solved

SQL-SubQuery to remove duplicates

Posted on 2014-11-13
7
146 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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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 Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Data architecture is an important aspect in Software as a Service (SaaS) delivery model. This article is a study on the database of a single-tenant application that could be extended to support multiple tenants. The application is web-based develope…
Naughty Me. While I was changing the database name from DB1 to DB_PROD1 (yep it's not real database name ^v^), I changed the database name and notified my application fellows that I did it. They turn on the application, and everything is working. A …
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

757 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

19 Experts available now in Live!

Get 1:1 Help Now