Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Group by with an order field
Message
 
 
To
15/09/2010 12:51:51
General information
Forum:
Microsoft SQL Server
Category:
SQL syntax
Environment versions
SQL Server:
SQL Server 2008
Application:
Web
Miscellaneous
Thread ID:
01481294
Message ID:
01481296
Views:
28
>The following SQL allows me to group by RO_No and collect a date time field with it:
>
>
>SELECT Event.RO_No,max(Event.DateTime) as LastDateTime FROM Event GROUP BY Event.RO_No
>
>
>However, I need to adjust that so the group by will only applies to the first 10 records of an order to Event.DateTime. Basically, if I wouldn't have a group by, the SQL would be this:
>
>
>SELECT TOP 10 Event.RO_No,Event.DateTime FROM Event ORDER by Event.DateTime
>
>
>But, on that result, I need to only get the unique occurence of Event.RO_No.

Are you working with SQL Server?

If yes, then
;with cte as (select top (10) Event.Ro_No, Event.[DateTime] from Event ORDER BY Event.DateTime)

select max(Event.DateTime) as LastDate, Event.Ro_No from cte
If it's not broken, fix it until it is.


My Blog
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform