Solved

SELECT TOP 3 and DISTINCT

Posted on 2014-12-07
6
172 Views
Last Modified: 2014-12-07
I have a table "records": id (identity), date (datetime), activityType (integer) with several entries.
I want the three latest activityType, records sorted by date:

Something like:
SELECT TOP 3 DISTINCT activityType FROM records ORDER BY date DESC
0
Comment
Question by:fmsol
  • 3
  • 2
6 Comments
 
LVL 34

Expert Comment

by:Mike Eghtebas
ID: 40485711
SELECT TOP 3 id, activityType FROM records Grouped By Id, activityType ORDER BY date DESC
0
 

Author Comment

by:fmsol
ID: 40485728
SELECT TOP 3 id, activityType FROM records Group By Id, activityType ORDER BY date DESC
returns:
Column "date" is invalid in the ORDER BY clause because it is not contained in either an aggregate function or the GROUP BY clause
0
 
LVL 34

Expert Comment

by:Mike Eghtebas
ID: 40485730
SELECT TOP 3 id, activityType, [date] FROM records Grouped By Id, activityType,  [date] ORDER BY [date] DESC
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

Author Comment

by:fmsol
ID: 40485732
Not there yet; since date is different, it doesn't return distinct activityType
0
 
LVL 34

Expert Comment

by:Mike Eghtebas
ID: 40485739
re:> since date is different

Could you give more info on this statement? Why and how different?
0
 
LVL 18

Accepted Solution

by:
Simon earned 500 total points
ID: 40485744
select top 3 * from (select activityType,max(date)  as LatestDate from records group by activityType)
order by LatestDate DESC
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
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 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.
Viewers will learn how the fundamental information of how to create a table.

828 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