Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win


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

Posted on 2010-08-27
Medium Priority
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 93

Accepted Solution

Patrick Matthews earned 2000 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 = a.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 Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

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…
What if you have to shut down the entire Citrix infrastructure for hardware maintenance, software upgrades or "the unknown"? I developed this plan for "the unknown" and hope that it helps you as well. This article explains how to properly shut down …
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
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

596 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