Solved

How do I select the first row from each subset of data within a query result?

Posted on 2010-08-27
5
389 Views
Last Modified: 2013-11-05
Hi!

I've got a MS SQL 2000 query that produces the following result:

Schedule    Date         Class          Period               Count       
----------- ------------ -------------- -------------------- ----------- 
441         2008/07/28   DEVL 101       A3201                205 
441         2008/07/28   DEVL 101       ZR019                102
 ...
441         2008/07/28   DEVL 101       BF012                1
554         2009/02/14   DEVL 154       X5002                384 
554         2009/02/14   DEVL 154       W8098                98
 ...
554         2009/02/14   DEVL 154       H2309                5

Open in new window


From this query, I want to select the Schedule, Date, Class, and Period with the highest Count from the rows that contain the same values for Schedule, Date, and Class.

The result should look like this:

Schedule    Date         Class          Period               Count       
----------- ------------ -------------- -------------------- ----------- 
441         2008/07/28   DEVL 101       A3201                205 
554         2009/02/14   DEVL 154       X5002                384
 ...

Open in new window


0
Comment
Question by:PaulELS
5 Comments
 
LVL 2

Expert Comment

by:SkydiverFL
ID: 33544206
SELEC TOP 1 FROM...
0
 
LVL 92

Accepted Solution

by:
Patrick Matthews earned 500 total points
ID: 33544233
SELECT t1.Schedule, t1.Date, t1.Class, t1.Period, t1.[Count]FROM SomeTable t1 INNER JOIN    (SELECT t2.Schedule, t2.Date, t2.Class, MAX(t2.[Count]) AS MaxCount    FROM SomeTable    GROUP BY t2.Schedule, t2.Date, t2.Class) x ON t1.Schedule = x.Schedule AND t1.Date = x.Date AND t1.Class = x.Class AND t1.[Count] = x.MaxCount
0
 
LVL 25

Expert Comment

by:jrb1
ID: 33544249
one way to do it (my preferred method)

select Schedule, Date, Class, Period, Count
from yourtable A
where count = (select max(count) from yourtable
                 where schedule = a.schedule
                 and date = a.date
                 and class = a.class
                and period = a.period)
0
 
LVL 2

Author Comment

by:PaulELS
ID: 33544345
Thanks, matthewspatrick!
0
 
LVL 8

Expert Comment

by:Mohit Vijay
ID: 33544502
In Below query, you need to change Table1 with your table name.

SELECT tbl.Schedule, tbl.Date, tbl.Class, tbl.Period, t1.[Count]
FROM (SELECT Schedule, Date, Class, MAX(Count) AS MaxCount
   FROM Table1
   GROUP BY Schedule, Date, Class) AS tbl2 LEFT OUTER JOIN Table1 tbl 
  ON tbl2.Schedule = tbl.Schedule AND tbl2.Date = tbl.Date AND tbl2.Class = tbl.Class AND tbl.Count= tbl2.MaxCount

Open in new window

0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
PL/SQL query 14 51
How toselect unique values 3 10
select over clause 1 10
Minus first query 1 0
In this article I will describe the Copy Database Wizard 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.
How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

747 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

12 Experts available now in Live!

Get 1:1 Help Now