Solved

SQL-SubQuery to remove duplicates

Posted on 2014-11-13
7
152 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
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.

 
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

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.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to use odbc in vb to connect to ms sql 14 52
Dimension table indexes 8 26
Help  needed 3 23
SQL Server - executing an agent job from a stored proc 2 18
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…
If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

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