Solved

Selecting TOP 1 of each record

Posted on 2009-07-06
22
509 Views
Last Modified: 2012-05-07
I'm working with the following data where a simple GROUP BY statement will not work.   The only thing they have in common 100% of the time is a column named "OrderNo".

For Example:

Table 1
============================
CaseName | DateSubmitted | OrderNo | SubjectName

I.E.
============================
"Northwind vs Test" | Jun 1, 2009 | 1000 | John Doe
"Northwind vs. Testing" | Jun 1, 2009 | 1000 | John Doe
"Northwind vs Test" | Jun 2, 2009 | 1000 | Jonathan Doe
"McDonalds vs Burger King" | Aug 1, 2008 | 2000 | Tim Smith
"Mcdonalds vs Burger King" | Aug 2, 2008 | 2000 | Tim Smith

What I would like the query to return is:
"Northwind vs Test" | Jun 1, 2009 | 1000 | John Doe
"McDonalds vs Burger King" | Aug 1, 2008 | 2000 | Tim Smith

As you can tell, the system is like this because of inconsistencies and human error but the rows all say the same thing in a different way.   I don't necessarily care in which order the TOP 1 is selected but I've tried a bunch of stuff and I want to see what ideas you guys have on the issue.

Thanks

0
Comment
Question by:LukeJD
  • 4
  • 4
  • 3
  • +5
22 Comments
 
LVL 17

Expert Comment

by:pssandhu
ID: 24789528
What if you do something like this since you say that then data is basically the same:
Select OrderNo, MAX(DateSubmitted), MAX(SubjectName), MAX(CaseName)
From Table
Group by OrderNo
0
 
LVL 40

Expert Comment

by:mrjoltcola
ID: 24789535
Why will GROUP BY not work?

I would do it with a GROUP BY, unless you can clarify why it wont work.


select casename, min(datesubmitted), orderno, subjectname

  from table1

group by casename, orderno, subjectname

;

Open in new window

0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 24789536
the problem "test" vs "testing" is that it is not a 100% match (leaving case sensitively apart). so "=" won't work.
using "LIKE" won't help neither, as it's impossible to use a % somewhere to make all th eversions match.
SOUNDEX can help. but it might give you false matches, also.

the real solution will be assisted manual data cleaning.
0
 
LVL 40

Expert Comment

by:mrjoltcola
ID: 24789565
Oh, I see now. I did not read close enough.

So I agree with angelIII. You could do some rudimentary replacements in the query such as replacing "vs." with "vs" and grouping by a case insensitive version of the column, but that would still leave plural/singular problems.

Yes, data should be cleaned. This is also a case where the table should be normalized so the casename is not in that table itself, but instead use a foreign key.
0
 
LVL 12

Expert Comment

by:williamcampbell
ID: 24789647
Select a random row

SELECT TOP 1 column FROM table
ORDER BY NEWID()


0
 

Author Comment

by:LukeJD
ID: 24789845
I was thinking of a self join, something along these lines:

SELECT a.* FROM (SELECT DISTINCT(o.OrderNo) FROM orders o) b
JOIN orders a ON a.OrderNo = b.OrderNo

But that seems to still create the duplicates.    Is there a way of drawing TOP 1 information based off of a column from another joined table?
0
 

Author Comment

by:LukeJD
ID: 24789882
Also, I wanted to add that there are over 100,000 rows in the table and doing a manual clean up will be labor intensive.
0
 
LVL 41

Accepted Solution

by:
ralmada earned 250 total points
ID: 24791266
What about this? (SQL 2005+)
select * from (

	select *, row_number() over (partition by OrderNo order by OrderNo) as rn

	from Table1

) a

where rn = 1

Open in new window

0
 
LVL 15

Assisted Solution

by:rob_farley
rob_farley earned 250 total points
ID: 24792899
Ok... I'm going to pitch in now.

The ROW_NUMBER() method suggested is great. But for another alternative, that works really well if you're actually looking at two different tables, how about:


SELECT t_top.*
FROM
(
SELECT DISTINCT Name
FROM TableName
) t
CROSS APPLY
(
SELECT TOP (1) *
FROM TableName t2
WHERE t2.Name = t.Name
ORDER BY SomeCriteria
) t_top


If you imagine something which lists the most expensive Product in each ProductSubcategory (in AdventureWorks)

SELECT s.Name, p.*
FROM Production.ProductSubcategory s
  CROSS APPLY
  (SELECT TOP (1) *
  FROM Production.Product ps
  WHERE ps.ProductSubcategoryID = s.ProductSubcategoryID
  ORDER BY ListPrice DESC) p

CROSS APPLY is like INNER JOIN where the second table is actually correlated to the first. You can use OUTER APPLY if you don't want to eliminate rows in the first table that don't produce any rows in the second.

Rob
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 23

Expert Comment

by:Racim BOUDJAKDJI
ID: 24793500
This should help..

select distinct A.CaseName, A.DateSubmitted, A..OrderNo , A.SubjectName
from table1 A inner join
(select CaseName, SubjectName, min(DateSubmitted) as DateSubmitted from table1 group by CaseName, SubjectName) B on A.OrderNo = B. OrderNo and A.DateSubmitted = B.DateSubmitted
0
 
LVL 23

Expert Comment

by:Racim BOUDJAKDJI
ID: 24793507
Ooops sorry a bad copy/paste

select distinct A.CaseName, A.DateSubmitted, A..OrderNo , A.SubjectName
from table1 A inner join
(select CaseName, SubjectName, min(DateSubmitted) as DateSubmitted from table1 group by CaseName, SubjectName) B on A.OrderNo = B.OrderNo and A.DateSubmitted = B.DateSubmitted
0
 
LVL 41

Expert Comment

by:ralmada
ID: 24793990
@Racimo, I was tempted to follow the same approach, however I feel your query will fail to eliminate the first two rows, which are duplicates, since they both have same OrderNo and DateSubmitted. That's why I've suggested the ROW_NUMBER() approach in my comment 24791266
0
 
LVL 23

Expert Comment

by:Racim BOUDJAKDJI
ID: 24794186
@ralmada

<<, however I feel your query will fail to eliminate the first two rows, which are duplicates>>

 The main problem is the lack of unique identifer for tuples.  As angelIII mentionned, there is a need to do data cleaning first by removing dupplicates, else running any extraction will necessarily make one *assume* what the correct answer might be.  

For instance, by using ROWNUMBER(), one has to assume that the first row that appears *in ROWNUMBER sequence* is necessarily the right row to select.  But since the questionner only mentionned OrderNo as a natural identifier, I did not feel it was necessariy to bring additional assumptions to the equation.

I guess the safest way to do that would be to run a dupplicate remover based on CaseName and SubjectName candidate key than run the followin based on the natural key.

something like...

select distinct A.CaseName, A.DateSubmitted, A..OrderNo , A.SubjectName
from table1 A inner join
(select CaseName, SubjectName, min(DateSubmitted) as DateSubmitted from table1 group by CaseName, SubjectName) B on A.CaseName = B.CaseName and A.SubjectName = B.SubjectName andA.SubjectName = B.SubjectName and A.DateSubmitted = B.DateSubmitted
0
 
LVL 23

Expert Comment

by:Racim BOUDJAKDJI
ID: 24794198
Ooops I mean

select distinct A.CaseName, A.DateSubmitted, A.SubjectName
from table1 A inner join
(select CaseName, SubjectName, min(DateSubmitted) as DateSubmitted from table1 group by CaseName, SubjectName) B on A.CaseName = B.CaseName and A.SubjectName = B.SubjectName and A.DateSubmitted = B.DateSubmitted
0
 
LVL 41

Expert Comment

by:ralmada
ID: 25042600
I still think my comment 24791266 could help the asker.
0
 
LVL 12

Expert Comment

by:williamcampbell
ID: 25043746
Seconded My Comment 24789647 is also useful
0
 
LVL 41

Expert Comment

by:ralmada
ID: 25047607
My recommendation is split points between comment 24791266 and 24792899.
0
 
LVL 12

Expert Comment

by:williamcampbell
ID: 25057084
Thats fine
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

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…
Shadow IT is coming out of the shadows as more businesses are choosing cloud-based applications. It is now a multi-cloud world for most organizations. Simultaneously, most businesses have yet to consolidate with one cloud provider or define an offic…
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

743 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

13 Experts available now in Live!

Get 1:1 Help Now