Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Selecting the Row with the MAX(DateStarted) Expand / Collapse
Author
Message
Posted Wednesday, October 16, 2013 5:39 PM


SSC Eights!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

Group: General Forum Members
Last Login: Yesterday @ 2:38 PM
Points: 958, Visits: 3,266
Hello Everyone
I am working on a query that needs to select the MAX(DateStated). There are other rows with the same ProductID and Different RowID. I am trying to select the Only row if there is only one ProductID, and if there are Multiple of the same ProductID, then select the one with the MAX(DateStarted)

I have used Row_Number(), but I was asked to not use that for this query. Kind of makes things rather difficult. This query will not only need to be ran against a SQL Server database, but possible a (And I hate saying this) DB2 database.

I greatly appreciate any and all assistance. Thank You in advance for all your suggestions, assistance and comments

Andrew SQLDBA

CREATE TABLE #AllData
(
DateStarted Datetime
, RowID int
, ProductID bigint
)

INSERT INTO #AllData

SELECT '2012-06-01 06:03:46.023', 66937, 777002387 UNION ALL
SELECT '2012-06-01 14:03:57.780', 67100, 777002387 UNION ALL
SELECT '2012-06-02 06:03:44.377', 67243, 777002387 UNION ALL
SELECT '2012-06-02 14:03:43.873', 67347, 777002387 UNION ALL
SELECT '2012-09-24 21:09:16.183', 75558, 701327244 UNION ALL
SELECT '2012-06-30 06:02:40.850', 75357, 701298894 UNION ALL
SELECT '2012-06-30 14:02:42.547', 75461, 701298894 UNION ALL
SELECT '2012-06-30 06:02:56.210', 75369, 701298827 UNION ALL
SELECT '2012-06-30 14:03:00.770', 75473, 701298827 UNION ALL
SELECT '2012-06-29 06:02:39.467', 75157, 701298589 UNION ALL
SELECT '2012-06-29 14:02:37.700', 75260, 701298589 UNION ALL
SELECT '2012-06-30 06:03:11.013', 75381, 701298589 UNION ALL
SELECT '2012-06-30 14:03:14.190', 75484, 701298589 UNION ALL
SELECT '2012-06-29 06:02:41.137', 75158, 701298587 UNION ALL
SELECT '2012-06-29 14:02:39.553', 75261, 701298587 UNION ALL
SELECT '2012-06-30 06:03:12.400', 75382, 701298587 UNION ALL
SELECT '2012-06-30 14:03:15.590', 75485, 701298587 UNION ALL
SELECT '2012-06-28 14:02:35.307', 75052, 701298403 UNION ALL
SELECT '2012-06-29 06:03:05.407', 75173, 701298373 UNION ALL
SELECT '2012-06-29 14:03:01.060', 75275, 701298373 UNION ALL
SELECT '2012-06-30 06:03:23.780', 75394, 701298373 UNION ALL
SELECT '2012-06-30 14:03:25.600', 75496, 701298373 UNION ALL
SELECT '2012-06-28 06:02:45.220', 74955, 701298288 UNION ALL
SELECT '2012-06-28 14:02:47.733', 75059, 701298288 UNION ALL
SELECT '2012-06-28 06:03:02.437', 74966, 701298112 UNION ALL
SELECT '2012-06-28 14:03:06.297', 75070, 701298112 UNION ALL
SELECT '2012-06-29 06:03:15.763', 75185, 701298112 UNION ALL
SELECT '2012-06-29 14:03:09.863', 75286, 701298112 UNION ALL
SELECT '2012-06-27 06:02:36.813', 74753, 701297947 UNION ALL
SELECT '2012-06-27 14:02:42.880', 74856, 701297947 UNION ALL
SELECT '2012-06-28 06:03:19.753', 74977, 701297947 UNION ALL
SELECT '2012-06-28 14:03:21.300', 75081, 701297947 UNION ALL
SELECT '2012-06-29 06:03:22.450', 75194, 701297947 UNION ALL
SELECT '2012-06-29 14:03:15.533', 75295, 701297947 UNION ALL
SELECT '2012-06-26 14:02:33.670', 74652, 701297565 UNION ALL
SELECT '2012-06-27 06:03:01.627', 74768, 701297565 UNION ALL
SELECT '2012-06-27 14:03:08.833', 74871, 701297565

SELECT * FROM #AllData
ORDER BY ProductID DESC

DROP TABLE #AllData

ResultSet:

DateStarted			RowID	ProductID
2012-06-01 06:03:46.023 66937 777002387
2012-06-01 14:03:57.780 67100 777002387
2012-06-02 06:03:44.377 67243 777002387
2012-06-02 14:03:43.873 67347 777002387 <-- Need This one
2012-09-24 21:09:16.183 75558 701327244 <-- Need This one
2012-06-30 06:02:40.850 75357 701298894
2012-06-30 14:02:42.547 75461 701298894 <-- Need This one
2012-06-30 06:02:56.210 75369 701298827
2012-06-30 14:03:00.770 75473 701298827 <-- Need This one
2012-06-29 06:02:39.467 75157 701298589
2012-06-29 14:02:37.700 75260 701298589
2012-06-30 06:03:11.013 75381 701298589
2012-06-30 14:03:14.190 75484 701298589 <-- Need This one
2012-06-29 06:02:41.137 75158 701298587
2012-06-29 14:02:39.553 75261 701298587
2012-06-30 06:03:12.400 75382 701298587
2012-06-30 14:03:15.590 75485 701298587 <-- Need This one
2012-06-28 14:02:35.307 75052 701298403
2012-06-29 06:03:05.407 75173 701298373
2012-06-29 14:03:01.060 75275 701298373
2012-06-30 06:03:23.780 75394 701298373
2012-06-30 14:03:25.600 75496 701298373 <-- Need This one
2012-06-28 06:02:45.220 74955 701298288
2012-06-28 14:02:47.733 75059 701298288 <-- Need This one
2012-06-28 06:03:02.437 74966 701298112
2012-06-28 14:03:06.297 75070 701298112
2012-06-29 06:03:15.763 75185 701298112
2012-06-29 14:03:09.863 75286 701298112 <-- Need This one
2012-06-27 06:02:36.813 74753 701297947
2012-06-27 14:02:42.880 74856 701297947
2012-06-28 06:03:19.753 74977 701297947
2012-06-28 14:03:21.300 75081 701297947
2012-06-29 06:03:22.450 75194 701297947
2012-06-29 14:03:15.533 75295 701297947 <-- Need This one
2012-06-26 14:02:33.670 74652 701297565
2012-06-27 06:03:01.627 74768 701297565
2012-06-27 14:03:08.833 74871 701297565 <-- Need This one


Post #1505452
Posted Wednesday, October 16, 2013 6:28 PM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Today @ 2:24 AM
Points: 3,591, Visits: 5,100
As long as you don't have any duplicates in the MAX date, you can do it this way:

WITH GroupedData AS
(
SELECT DateStarted=MAX(DateStarted), ProductID
FROM #AllData
GROUP BY ProductID
)
SELECT a.*
FROM #AllData a
JOIN GroupedData b
ON a.ProductID = b.ProductID AND a.DateStarted = b.DateStarted
ORDER BY a.ProductID DESC;





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!
Post #1505457
Posted Wednesday, October 16, 2013 11:10 PM
UDP Broadcaster

UDP BroadcasterUDP BroadcasterUDP BroadcasterUDP BroadcasterUDP BroadcasterUDP BroadcasterUDP BroadcasterUDP Broadcaster

Group: General Forum Members
Last Login: Wednesday, December 18, 2013 5:03 AM
Points: 1,454, Visits: 135
dwain.c (10/16/2013)
As long as you don't have any duplicates in the MAX date:



SELECT * FROM #AllData WHERE CONVERT(VARCHAR(20),DateStarted) + CONVERT(VARCHAR(30), ProductID) IN(
SELECT CONVERT(VARCHAR(20),max(DateStarted))+ CONVERT(VARCHAR(30), ProductID) FROM #AllData
group BY ProductID)

Post #1505479
Posted Wednesday, October 16, 2013 11:50 PM


SSC Eights!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

Group: General Forum Members
Last Login: Yesterday @ 2:38 PM
Points: 958, Visits: 3,266
Thanks DWain
That is exactly what I was after. Works very fast also. Got to Love that part.

Andrew SQLDBA
Post #1505488
Posted Thursday, October 17, 2013 2:02 AM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Today @ 2:24 AM
Points: 3,591, Visits: 5,100
You're most welcome.


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!
Post #1505520
Posted Thursday, October 17, 2013 3:18 AM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Thursday, April 17, 2014 7:06 AM
Points: 2,404, Visits: 7,311
Don't know if it's any better, but an alternative is: -

SELECT DateStarted, RowID, ProductID
FROM (SELECT DateStarted, RowID, ProductID,
MAX(DateStarted) OVER(PARTITION BY ProductID) AS MaxDateStarted
FROM #AllData
) a
WHERE a.DateStarted = a.MaxDateStarted
ORDER BY a.ProductID DESC;




Not a DBA, just trying to learn

For better, quicker answers on T-SQL questions, click on the following...
http://www.sqlservercentral.com/articles/Best+Practices/61537/

For better, quicker answers on SQL Server performance related questions, click on the following...
http://www.sqlservercentral.com/articles/SQLServerCentral/66909/



If you litter your database queries with nolock query hints, are you aware of the side effects?
Try reading a few of these links...

(*) Missing rows with nolock
(*) Allocation order scans with nolock
(*) Consistency issues with nolock
(*) Transient Corruption Errors in SQL Server error log caused by nolock
(*) Dirty reads, read errors, reading rows twice and missing rows with nolock


LinkedIn | Blog coming soon (for sufficiently large values of "soon" )!
Post #1505548
Posted Thursday, October 17, 2013 7:40 AM


SSC Eights!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

Group: General Forum Members
Last Login: Yesterday @ 2:38 PM
Points: 958, Visits: 3,266
Thanks Cadavre
That one works really efficiently also. I will keep that code handy.

Greatly appreciate it

Andrew SQLDBA
Post #1505705
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse