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.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 Select with max and min date

Author  Topic 

wardlejew
Starting Member

1 Post

Posted - 2009-12-01 : 11:38:58
I have two tables

route_tbl: route_id
route_point_tbl: route_id, number, date, time, city, state, zip

route_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
Go to Top of Page
   

- Advertisement -