SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Select top X for each multiple item in column


Select top X for each multiple item in column

Author
Message
domleg
domleg
Grasshopper
Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)

Group: General Forum Members
Points: 11 Visits: 22
I have the following script:

SELECT dbo.HemAnimal.AniUserNumber, dbo.HemAnimal.AniResponderNumber, dbo.PrmDeviceVisit.DviStartTime, dbo.PrmDeviceVisit.DviEndTime,
dbo.PrmDeviceVisit.DviMilkVisit, dbo.PrmDeviceVisit.DviFailure, dbo.PrmMilkVisit.MviLFTeatWidth, dbo.PrmMilkVisit.MviLRTeatWidth,
dbo.PrmMilkVisit.MviRFTeatWidth, dbo.PrmMilkVisit.MviRRTeatWidth
FROM dbo.HemAnimal INNER JOIN
dbo.PrmDeviceVisit ON dbo.HemAnimal.AniId = dbo.PrmDeviceVisit.DviAniId INNER JOIN
dbo.PrmMilkVisit ON dbo.PrmDeviceVisit.DviId = dbo.PrmMilkVisit.MviDviId
ORDER BY dbo.HemAnimal.AniUserNumber, dbo.PrmDeviceVisit.DviEndTime DESC

Part of results:
92 NULL 2011-11-17 19:26:57.000 2011-11-17 19:32:15.000 1 0 18 15 17 16
92 NULL 2011-11-16 18:57:49.000 2011-11-16 19:01:23.000 1 0 18 15 17 16
92 NULL 2011-11-16 08:09:15.000 2011-11-16 08:17:35.000 1 0 18 15 17 16
92 NULL 2011-11-15 17:21:14.000 2011-11-15 17:30:00.000 1 0 18 15 17 16
92 NULL 2011-11-14 22:46:03.000 2011-11-14 22:50:56.000 1 0 18 15 17 16
92 NULL 2011-11-14 01:17:59.000 2011-11-14 01:23:18.000 1 0 18 15 17 16
92 NULL 2011-11-13 11:14:47.000 2011-11-13 11:20:42.000 1 0 18 15 17 16
92 NULL 2011-11-12 21:37:20.000 2011-11-12 21:51:02.000 1 0 18 15 17 16
107 3027325 2013-08-10 19:21:35.000 2013-08-10 19:27:29.000 1 0 18 15 17 16
107 3027325 2013-08-10 06:38:22.000 2013-08-10 06:44:21.000 1 0 18 15 17 16
107 3027325 2013-08-09 10:32:19.000 2013-08-09 10:38:25.000 1 0 18 15 17 16
107 3027325 2013-08-08 22:50:41.000 2013-08-08 22:57:50.000 1 0 18 15 17 16
107 3027325 2013-08-07 10:43:20.000 2013-08-07 10:52:55.000 1 0 18 15 17 16
107 3027325 2013-08-06 09:22:50.000 2013-08-06 09:30:16.000 1 0 18 15 17 16
107 3027325 2013-08-05 11:33:43.000 2013-08-05 11:43:21.000 1 0 18 15 17 16
107 3027325 2013-08-04 05:53:26.000 2013-08-04 05:59:18.000 1 1 18 15 17 16


However this gives me all the items in the database for AniUserNumber. I want to be able to get only the top 3 records for each AniUserNumber in the database table PrmMilkVisit, AniUserNumber is already a unique entry in the HemAnimal table but when Inner Joined with PrmDeviceVisit and PrmMilkVisit there are many visits for each animal, I only want the top 3 visits.
Keith Tate
Keith Tate
Ten Centuries
Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)

Group: General Forum Members
Points: 1028 Visits: 979
Welcome! Please take a look at the article in my signature about posting questions to the forums. Table definitions and sample data is always useful. What I would recommend is
with cte as
(SELECT ROW_NUMBER() over (partition by dbo.HemAnimal.AniUserNumber order by dbo.PrmDeviceVisit.DviEndTime DESC) as rowNum, dbo.HemAnimal.AniUserNumber, dbo.HemAnimal.AniResponderNumber, dbo.PrmDeviceVisit.DviStartTime, dbo.PrmDeviceVisit.DviEndTime,
dbo.PrmDeviceVisit.DviMilkVisit, dbo.PrmDeviceVisit.DviFailure, dbo.PrmMilkVisit.MviLFTeatWidth, dbo.PrmMilkVisit.MviLRTeatWidth,
dbo.PrmMilkVisit.MviRFTeatWidth, dbo.PrmMilkVisit.MviRRTeatWidth
FROM dbo.HemAnimal INNER JOIN
dbo.PrmDeviceVisit ON dbo.HemAnimal.AniId = dbo.PrmDeviceVisit.DviAniId INNER JOIN
dbo.PrmMilkVisit ON dbo.PrmDeviceVisit.DviId = dbo.PrmMilkVisit.MviDviId
)
select *
from CTE
where rowNum <= 3


This should give you what you want.



Microsoft Certified Master - SQL Server 2008
Follow me on twitter: @keith_tate

Forum Etiquette: How to post data/code on a forum to get the best help
CKX
CKX
SSC-Enthusiastic
SSC-Enthusiastic (189 reputation)SSC-Enthusiastic (189 reputation)SSC-Enthusiastic (189 reputation)SSC-Enthusiastic (189 reputation)SSC-Enthusiastic (189 reputation)SSC-Enthusiastic (189 reputation)SSC-Enthusiastic (189 reputation)SSC-Enthusiastic (189 reputation)

Group: General Forum Members
Points: 189 Visits: 1014
domleg (12/5/2013)
I have the following script:

SELECT dbo.HemAnimal.AniUserNumber, dbo.HemAnimal.AniResponderNumber, dbo.PrmDeviceVisit.DviStartTime, dbo.PrmDeviceVisit.DviEndTime,
dbo.PrmDeviceVisit.DviMilkVisit, dbo.PrmDeviceVisit.DviFailure, dbo.PrmMilkVisit.MviLFTeatWidth, dbo.PrmMilkVisit.MviLRTeatWidth,
dbo.PrmMilkVisit.MviRFTeatWidth, dbo.PrmMilkVisit.MviRRTeatWidth
FROM dbo.HemAnimal INNER JOIN
dbo.PrmDeviceVisit ON dbo.HemAnimal.AniId = dbo.PrmDeviceVisit.DviAniId INNER JOIN
dbo.PrmMilkVisit ON dbo.PrmDeviceVisit.DviId = dbo.PrmMilkVisit.MviDviId
ORDER BY dbo.HemAnimal.AniUserNumber, dbo.PrmDeviceVisit.DviEndTime DESC

Part of results:
92 NULL 2011-11-17 19:26:57.000 2011-11-17 19:32:15.000 1 0 18 15 17 16
92 NULL 2011-11-16 18:57:49.000 2011-11-16 19:01:23.000 1 0 18 15 17 16
92 NULL 2011-11-16 08:09:15.000 2011-11-16 08:17:35.000 1 0 18 15 17 16
92 NULL 2011-11-15 17:21:14.000 2011-11-15 17:30:00.000 1 0 18 15 17 16
92 NULL 2011-11-14 22:46:03.000 2011-11-14 22:50:56.000 1 0 18 15 17 16
92 NULL 2011-11-14 01:17:59.000 2011-11-14 01:23:18.000 1 0 18 15 17 16
92 NULL 2011-11-13 11:14:47.000 2011-11-13 11:20:42.000 1 0 18 15 17 16
92 NULL 2011-11-12 21:37:20.000 2011-11-12 21:51:02.000 1 0 18 15 17 16
107 3027325 2013-08-10 19:21:35.000 2013-08-10 19:27:29.000 1 0 18 15 17 16
107 3027325 2013-08-10 06:38:22.000 2013-08-10 06:44:21.000 1 0 18 15 17 16
107 3027325 2013-08-09 10:32:19.000 2013-08-09 10:38:25.000 1 0 18 15 17 16
107 3027325 2013-08-08 22:50:41.000 2013-08-08 22:57:50.000 1 0 18 15 17 16
107 3027325 2013-08-07 10:43:20.000 2013-08-07 10:52:55.000 1 0 18 15 17 16
107 3027325 2013-08-06 09:22:50.000 2013-08-06 09:30:16.000 1 0 18 15 17 16
107 3027325 2013-08-05 11:33:43.000 2013-08-05 11:43:21.000 1 0 18 15 17 16
107 3027325 2013-08-04 05:53:26.000 2013-08-04 05:59:18.000 1 1 18 15 17 16


However this gives me all the items in the database for AniUserNumber. I want to be able to get only the top 3 records for each AniUserNumber in the database table PrmMilkVisit, AniUserNumber is already a unique entry in the HemAnimal table but when Inner Joined with PrmDeviceVisit and PrmMilkVisit there are many visits for each animal, I only want the top 3 visits.



Posting DDL and expected results almost always means someone gives you a coded response to your issue.

I think what you need here is the use of ROW_NUMBER partitioning on the relevant column and then just grabbing the rows you need based on the Row_Number Column value.
No DDL or expected results posted so can't show you to see if its whats you require.
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search