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

Posted on 2010-08-27
Last Modified: 2013-11-05

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

Question by:PaulELS
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

Expert Comment

ID: 33544206
LVL 92

Accepted Solution

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
LVL 25

Expert Comment

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 =
                 and class = a.class
                and period = a.period)

Author Comment

ID: 33544345
Thanks, matthewspatrick!

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


Featured Post

Free Webinar: AWS Backup & DR

Join our upcoming webinar with experts from AWS, CloudBerry Lab, and the Town of Edgartown IT to discuss best practices for simplifying online backup management and cutting costs.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
sql query help 15 51
unable to reinstall SCVMM 2012 R2 - SQL errors. 5 36
Show RTF format in an SSRS report 3 39
help converting varchar to date 14 25
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

740 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