Solved

SQL-SubQuery to remove duplicates

Posted on 2014-11-13
7
157 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
[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
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

Question has a verified solution.

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

Long way back, we had to take help from third party tools in order to encrypt and decrypt data.  Gradually Microsoft understood the need for this feature and started to implement it by building functionality into SQL Server. Finally, with SQL 2008, …
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 …
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

627 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