SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 sql query using group by
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

rowter
Yak Posting Veteran

75 Posts

Posted - 07/11/2013 :  16:21:11  Show Profile  Reply with Quote
Hi,

I have a table that has picktime, route_id, stlat and stlong columns.
There might be multiple records having same picktime and routeid.


Select S.pickup_time, S.route_id from STARTTBL as S where Start_Dt= '7/11/2013' group by S.Route_id

(pktime) route_id stlat stlat
08:50 17 -32.01 102.98
09:50 17 -33.01 102.98
14:25 25
16:30 25
16:00 29

1. Is it possible to get the all the 4 columns where pickup_time is MAX for each Route_id

For the above data , the resulting data would be

09:50 17 -33.01 102.98
16:30 25
16:00 29

Thanks

MuMu88
Aged Yak Warrior

547 Posts

Posted - 07/11/2013 :  16:25:30  Show Profile  Reply with Quote
quote:
Originally posted by rowter

Hi,

I have a table that has picktime, route_id, stlat and stlong columns.
There might be multiple records having same picktime and routeid.


Select MAX(S.pickup_time), S.route_id from STARTTBL as S where Start_Dt= '7/11/2013' group by S.Route_id
(pktime) route_id stlat stlat
08:50 17 -32.01 102.98
09:50 17 -33.01 102.98
14:25 25
16:30 25
16:00 29

1. Is it possible to get the all the 4 columns where pickup_time is MAX for each Route_id

For the above data , the resulting data would be

09:50 17 -33.01 102.98
16:30 25
16:00 29

Thanks





WITH CTE AS
(Select MAX(S.pickup_time) as pickup_time, S.route_id from STARTTBL as S where Start_Dt= '7/11/2013' group by S.Route_id)

SELECT * from CTE T1 LEFT JOIN STARTTBL T2 WHERE T1.route_id = T2.route_id and T1.pickup_time = T2.pickup_time;


Edited by - MuMu88 on 07/11/2013 16:30:32
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52325 Posts

Posted - 07/12/2013 :  01:56:06  Show Profile  Reply with Quote
Assuming picktime is of datatype time you can use


SELECT *
FROM 
(
SELECT ROW_NUMBER() OVER (PARTITION BY route_id ORDER BY pktime DESC) AS Seq,*
FROM table
)t
WHERE Seq=1


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52325 Posts

Posted - 07/12/2013 :  01:58:46  Show Profile  Reply with Quote
if its varchar you need this small modification

SELECT *
FROM 
(
SELECT ROW_NUMBER() OVER (PARTITION BY route_id ORDER BY DATEDIFF(minute,0,pktime) DESC) AS Seq,*
FROM table
)t
WHERE Seq=1


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.05 seconds. Powered By: Snitz Forums 2000