Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Picking unique records


Picking unique records

Author
Message
savycara
savycara
Grasshopper
Grasshopper (24 reputation)Grasshopper (24 reputation)Grasshopper (24 reputation)Grasshopper (24 reputation)Grasshopper (24 reputation)Grasshopper (24 reputation)Grasshopper (24 reputation)Grasshopper (24 reputation)

Group: General Forum Members
Points: 24 Visits: 13
ID,FID,HC,vc,sN


I have to send the records with unique combination HC and VC.
when they are same then I have to pick the record based on maximum updatedate
when updatedated is same the I have to pick the record based on maximum SN
when SN is same the I have to pick the record based on maximum FID.
please suggest way to do this


ID FID HC VC SN updateddate

104 1 100562 5000151 2974814 2012-03-13
104 1 100865 5000151 2980508 2012-08-27
104 1 102406 5000151 3042830 2013-04-05
104 1 102406 5000151 3042832 2013-04-05
104 1 102407 5000151 3021692 2013-04-10
104 1 102408 5000151 3021063 2013-04-10
104 1 102408 5000151 3021070 2013-04-10
104 1 102409 5000151 3021709 2013-01-25
104 1 102979 5000151 3020070 2013-01-04
104 1 103921 5000151 3034203 2012-10-23
104 1 104659 5000151 3050322 2013-03-14
104 1 105309 5000151 3062808 2012-03-13
Sean Lange
Sean Lange
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16580 Visits: 17023
Pretty sparse on details but I am pretty sure something like this will work.


select top 1 max(ID), max(FID), HC, vc, Max(sN), MAX(updateddate)
from SomeTable
group by hc, vc



If that doesn't work, please take a few minutes and read the first article in my signature for best practices when posting questions.

_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
dwain.c
dwain.c
SSCarpal Tunnel
SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)

Group: General Forum Members
Points: 4255 Visits: 6431
Sounds to me like you need something similar to this:


WITH SampleData (ID, FID, HC, VC, SN, updateddate) AS
(
SELECT 104,1,100562,5000151,2974814,'2012-03-13'
UNION ALL SELECT 104,1,100865,5000151,2980508,'2012-08-27'
UNION ALL SELECT 104,1,102406,5000151,3042830,'2013-04-05'
UNION ALL SELECT 104,1,102406,5000151,3042832,'2013-04-05'
UNION ALL SELECT 104,1,102407,5000151,3021692,'2013-04-10'
UNION ALL SELECT 104,1,102408,5000151,3021063,'2013-04-10'
UNION ALL SELECT 104,1,102408,5000151,3021070,'2013-04-10'
UNION ALL SELECT 104,1,102409,5000151,3021709,'2013-01-25'
UNION ALL SELECT 104,1,102979,5000151,3020070,'2013-01-04'
UNION ALL SELECT 104,1,103921,5000151,3034203,'2012-10-23'
UNION ALL SELECT 104,1,104659,5000151,3050322,'2013-03-14'
UNION ALL SELECT 104,1,105309,5000151,3062808,'2012-03-13'
)
SELECT ID, FID, HC, VC, SN, updateddate
FROM
(
SELECT ID, FID, HC, VC, SN, updateddate
,rn=ROW_NUMBER() OVER (PARTITION BY HC, VC ORDER BY updateddate DESC, SN DESC, FID DESC)
FROM SampleData
) a
WHERE rn=1;





My mantra: No loops! No CURSORs! No RBAR! Hoo-uh!

My thought question: Have you ever been told that your query runs too fast?

My advice:
INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.


Need to UNPIVOT? Why not CROSS APPLY VALUES instead?
Since random numbers are too important to be left to chance, let's generate some!
Learn to understand recursive CTEs by example.
Splitting strings based on patterns can be fast!
My temporal SQL musings: Calendar Tables, an Easter SQL, Time Slots and Self-maintaining, Contiguous Effective Dates in Temporal Tables
savycara
savycara
Grasshopper
Grasshopper (24 reputation)Grasshopper (24 reputation)Grasshopper (24 reputation)Grasshopper (24 reputation)Grasshopper (24 reputation)Grasshopper (24 reputation)Grasshopper (24 reputation)Grasshopper (24 reputation)

Group: General Forum Members
Points: 24 Visits: 13
@ Sean Lange

Thank you...
savycara
savycara
Grasshopper
Grasshopper (24 reputation)Grasshopper (24 reputation)Grasshopper (24 reputation)Grasshopper (24 reputation)Grasshopper (24 reputation)Grasshopper (24 reputation)Grasshopper (24 reputation)Grasshopper (24 reputation)

Group: General Forum Members
Points: 24 Visits: 13
@dwain.c

Thank you...it worked...Smile


SELECT ID, FID, HC, VC, SN, updateddate
FROM
(
SELECT ID, FID, HC, VC, SN, updateddate
,rn=ROW_NUMBER() OVER (PARTITION BY HC, VC ORDER BY updateddate DESC, SN DESC, FID DESC)
FROM SampleData
) a
WHERE rn=1;
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