Site Sponsored By: SQLDSC - SQL Server Desired State Configuration
Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
I have two tablesroute_tbl: route_idroute_point_tbl: route_id, number, date, time, city, state, ziproute_point_tbl.route_id has foreign key to route_tbl.route_id.I need a select that gives me results:route_id, StartDate(route_point_tbl.date where route_point_tbl.number = 1 for the route_id),EndDate(route_point_tbl.date where route_point_tbl.numbers is max for the route_id)I will also need to be able to sort the results by some combination of number, Start Date, or EndDate, based on what a user selects. I'm rusty on my SQL, so any help would be appreciated.
vijayisonly
Master Smack Fu Yak Hacker
1836 Posts
Posted - 2009-12-01 : 16:24:45
May be there is an easier way...but try this..
select a.route_id, max(case when a.number = b.minnum then a.date else 0 end) as startdate, max(case when a.number = b.maxnum then a.date else 0 end) as enddate from route_point_tbl a inner join (select route_id, max(number) as maxnum, min(number) as minnum from route_point_tbl group by route_id) b on a.route_id = b.route_id group by a.route_id