Home How to select first 2 rows using group's
Reply: 0

How to select first 2 rows using group's

user718
1#
user718 Published in July 23, 2018, 10:00 am

I have:

Table1

ID  date       amt
-------------------    
001 21/01/2012 1200
001 25/02/2012 1400
001 24/03/2012 1500
001 21/04/2012 1000
002 21/03/2012 1200
002 01/01/2012 0500
002 08/09/2012 1000
.....

I want to select the first two rows from each group of ID ordered by date DESC from Table1.

Query looks like this:

SELECT TOP 2 DATE, ID, AMT FROM TABLE1 GROUP BY ID, AMT --(NOT WORKING)

Expected output:

ID  date       amt
-------------------
001 21/01/2012 1200
001 25/02/2012 1400
002 21/03/2012 1200
002 01/01/2012 0500

.....
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.315566 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO