Solved

How to pick any one of duplicate records in a table

Posted on 2011-09-28
12
205 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
 

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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

by Mark Wills Attending one of Rob Farley's seminars the other day, I heard the phrase "The Accidental DBA" and fell in love with it. It got me thinking about the plight of the newcomer to SQL Server...  So if you are the accidental DBA, or, simp…
In this article I will describe the Detach & Attach method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
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…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

705 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

21 Experts available now in Live!

Get 1:1 Help Now