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 select min(m.date),max(m.date) from m where type='x' and p='test'how can I join this so that i can get the id of the min date and the max date instead of just returning the date of the min or the max
sunitabeck
Master Smack Fu Yak Hacker
5155 Posts
Posted - 2009-03-08 : 11:57:58
something like this...
with A as( select row_number() over (order by date asc) as asc_rownum, row_number() over (order by date desc) as desc_rownum, idcolumn, date from yourtable where type = 'x' and p='test')select idcolumn, date from Awhere asc_rownum = 1 or desc_rownum = 1
visakh16
Very Important crosS Applying yaK Herder
52326 Posts
Posted - 2009-03-08 : 12:45:52
also
SELECT idcolumn, dateFROM(SELECT MIN(date) OVER () AS MinDate,MAX(date) OVER () AS MaxDate,idcolumn, date from yourtable where type = 'x' and p='test')tWHERE date=MinDate OR date=MaxDate