Max Date

Max Date

Post by Pauline Ohania » Sat, 13 May 2000 04:00:00



Hello,
I've got a table like this:
OrderID     DateSent
111            1/1/2000
111            1/2/2000
111            1/3/2000
112            1/1/2000
112            1/5/2000

I want to select the Maximum Date for Orders that would return from the
above table:
OrderID     DateSent
111            1/3/2000
112            1/5/2000

So, what's the SQL statement that can return this?

Thanks,
Pauline

 
 
 

Max Date

Post by Tibor Karasz » Sat, 13 May 2000 04:00:00


SELECT MAX(DateSent)
 FROM tbl
 GROUP BY OrderId

--
Tibor Karaszi, Cornerstone Sweden AB
MCDBA, MCSE, MCSD, MCT, SQL Server MVP
Please reply to the newsgroup only, not by email.


Quote:> Hello,
> I've got a table like this:
> OrderID     DateSent
> 111            1/1/2000
> 111            1/2/2000
> 111            1/3/2000
> 112            1/1/2000
> 112            1/5/2000

> I want to select the Maximum Date for Orders that would return from the
> above table:
> OrderID     DateSent
> 111            1/3/2000
> 112            1/5/2000

> So, what's the SQL statement that can return this?

> Thanks,
> Pauline


 
 
 

Max Date

Post by BlueG » Sat, 13 May 2000 04:00:00


MAX will return the maximum value based on your where clause, but if you
want to return the N maximum values, then you have to do something else
like:

select top 2 * from MyTable order by DateSent desc

where 2 is the number of rows you want returned.  Notice the 'desc' in the
order by clause to order the dates from highest to lowest first.


Quote:> Hello,
> I've got a table like this:
> OrderID     DateSent
> 111            1/1/2000
> 111            1/2/2000
> 111            1/3/2000
> 112            1/1/2000
> 112            1/5/2000

> I want to select the Maximum Date for Orders that would return from the
> above table:
> OrderID     DateSent
> 111            1/3/2000
> 112            1/5/2000

> So, what's the SQL statement that can return this?

> Thanks,
> Pauline