Solved

How to pick any one of duplicate records in a table

Posted on 2011-09-28
12
230 Views
Last Modified: 2012-06-21
Hi Experts,

I have a Part master tab1 with unique partno which is the key, and another tab2 being loosely organized with reference info and most likely with deuplicate partno records. The two tables look like:
Tab1 (Note that the partno is the key)
Partno     Field1
111
222
333
444

Tab2  (Note the partno most likely are duplicated)
Partno          Refno
111               2000
222               4000
111               9000

My mission is to set relation using partno and pick the Refno from tab2. If duplicated records in Tab2, then just pick any one -- only one record from the duplicated records from Tab2.
The result table will look like:
Partno         Refno
111              2000   (or 9000, doesn't matter)
222              4000
333              NULL
444              NULL

In old VFP command, I can use
USE tab1
SET RELATION TO partno INTO tab2
REPLACE tab1.field1 WITH  tab2.refno

How to accomplish in SQL?
0
Comment
Question by:Castlewood
  • 8
  • 3
12 Comments
 
LVL 33

Expert Comment

by:knightEknight
ID: 36717986
If it truly doesn't matter which Refno is used, then do this:

select Partno, MAX(Refno)
into NewTable
from tab2

select * from NewTable
0
 
LVL 33

Accepted Solution

by:
knightEknight earned 250 total points
ID: 36717992
oops, forgot the group by clause:

select Partno, MAX(Refno)
into NewTable
from tab2
GROUP BY Partno
0
 
LVL 18

Assisted Solution

by:lludden
lludden earned 250 total points
ID: 36718002
SELECT Tab1.PartNo, MIN(Tab2.RefNo) AS RefNo
FROM Tab1 INNER JOIN Tab2 ON  Tab1.PartNo = Tab2.PartNo
GROUP BY Tab1.PartNo
0
Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

 

Author Comment

by:Castlewood
ID: 36718554
Thanks guys. Now I know MAX() or MIN() can help in this matter. BUT I need to SELECT more fields than just Partno and Refno. For instance, I want to include one more column Field1 as below:

SELECT Tab1.PartNo, MIN(Tab2.RefNo) AS RefNo, Tab1.Field1
FROM Tab1 LEFT JOIN Tab2 ON  Tab1.PartNo = Tab2.PartNo
GROUP BY Tab1.PartNo

I will get an error saying "Column Tab1.Field1 is invalid because it is not contained in either an aggregate function or the GROUP BY clause."

What can I do?
0
 

Author Comment

by:Castlewood
ID: 36719027
0
 

Author Closing Comment

by:Castlewood
ID: 36719047
Inorder to get the Tab1's columns, a join is needed.
Inorder to keep Tab1's records intact, a LEFT JOIN is needed.

Thank you all.
0
 
LVL 33

Expert Comment

by:knightEknight
ID: 36719051
SELECT Tab1.PartNo, Tab2.RefN, Tab1.Field1
FROM Tab1
LEFT JOIN Tab2
  ON  Tab1.PartNo = Tab2.PartNo
where Tab2.RefN in (select MAX(RefN) from Tab2 GROUP BY PartNo)
0
 

Author Comment

by:Castlewood
ID: 36719075
knightEknight:
your answer is exactly what I look for. I should have given you all the points.
0
 

Author Comment

by:Castlewood
ID: 36719605
knightEknight:
I overlooked one critical point -- the WHERE condition needs to be part of ON condition. Otherwise the join will be just like an INNER JOIN and partno 333 and 444 will be left out, which is not what I want. Rather I want all tab1 records to be kept. So here is it:

SELECT Tab1.PartNo, Tab2.RefN, Tab1.Field1
FROM Tab1
LEFT JOIN Tab2
  ON  Tab1.PartNo = Tab2.PartNo AND Tab2.RefN in (select MAX(RefN) from Tab2 GROUP BY PartNo)
0
 

Author Comment

by:Castlewood
ID: 36720661
or using derived table:

SELECT Tab1.PartNo, Tab2.RefN, Tab1.Field1
FROM Tab1
LEFT JOIN (select PartNo, MAX(RefN) as RefN from Tab2 GROUP BY PartNo) AS Tab2
  WHERE Tab1.PartNo = Tab2.PartNo
0
 

Author Comment

by:Castlewood
ID: 36750454
Also notice the applied order is:

. the required tables are joined
. the composite dataset is filtered through the WHERE clause
. the remaining rows are chopped into groups by the GROUP BY clause, and aggregated
. they are then filtered again, through the HAVING clause
. finally operated on, by SELECT / ORDER BY, UPDATE or DELETE.
(WHERE is applied before GROUP BY while HAVING is applied after GROUP BY)

0
 

Author Comment

by:Castlewood
ID: 36759162
(WHERE is applied before GROUP BY while HAVING is applied after GROUP BY)

SELECT partno, SUM(refno) AS ref
  FROM tab2
  GROUP BY partno HAVING SUM(refno) > 10000
returns:
111    11000

SELECT partno, SUM(refno) AS ref
  FROM tab2
  WHERE refno > 2000
  GROUP BY partno HAVING SUM(refno) > 10000
returns: no records

(Note: HAVING can have aggregate functions while WHERE can not.)

0

Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

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…
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
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…

829 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