Solved

SQL-SubQuery to remove duplicates

Posted on 2014-11-13
7
156 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
Guide to Performance: Optimization & Monitoring

Nowadays, monitoring is a mixture of tools, systems, and codes—making it a very complex process. And with this complexity, comes variables for failure. Get DZone’s new Guide to Performance to learn how to proactively find these variables and solve them before a disruption occurs.

 
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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Suggested Solutions

SQL Server engine let you use a Windows account or a SQL Server account to connect to a SQL Server instance. This can be configured immediatly during the SQL Server installation or after in the Server Authentication section in the Server properties …
How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

734 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