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)
 Max(Date)?

Author  Topic 

chriskhan2000
Aged Yak Warrior

544 Posts

Posted - 2005-09-02 : 11:58:50
I have some records that I'm using a Case statement to pull the most current date. But using Max doesn't work. Any ideas?

Example:


DATE PERCENT
08/08/2005 5
05/23/2005 6
08/12/2005 9



From the example since 08/12/2005 is the most current it will pick that one in my example above. Otherwise it will keep whatever percentage that is available in that record.


CASE WHEN MAX(DATE) THEN PERCENT
ELSE PERCENT END AS PERCENTAGE

DonAtWork
Master Smack Fu Yak Hacker

2167 Posts

Posted - 2005-09-02 : 12:04:14
Try something like this
select @MyPercent = Percent FROM YourTable WHERE DATE = MAX(DATE)

Otherwise, follow the link in the sig.


Help us help YOU!
Read this blog entry for more details: http://weblogs.sqlteam.com/brettk/archive/2005/05/25.aspx

*need more coffee*
SELECT * FROM Users WHERE CLUE > 0
(0 row(s) affected)
Go to Top of Page

chriskhan2000
Aged Yak Warrior

544 Posts

Posted - 2005-09-02 : 12:27:02
Thanks for the example. GOt it to work now. HEre's what I did in case someone runs into this.


SELECT PERCENTAGE
FROM SALES
WHERE DATE IN
(SELECT MAX(DATE)
FROM SALES)

Go to Top of Page

SamC
White Water Yakist

3467 Posts

Posted - 2005-09-02 : 12:44:15
[code]
SELECT PERCENTAGE
FROM SALES
WHERE DATE = (SELECT MAX(DATE)FROM SALES)[/code]

Could there be more than 1 row that meets the condition of DATE = MAX(Date)?
Go to Top of Page

chriskhan2000
Aged Yak Warrior

544 Posts

Posted - 2005-09-02 : 13:43:00
SELECT PERCENTAGE
FROM SALES
WHERE DATE = (SELECT MAX(DATE)FROM SALES)

The date will always be different, because the percentage is a discount that is given to the customer. If a discount percentage is adjusted twice in the same day then yes I'm screwed. Will need to check for rowid or something else too.
Go to Top of Page

VladRUS.ca
Starting Member

23 Posts

Posted - 2005-09-02 : 13:53:56
May be you can use simple:
SELECT TOP 1 PERCENTAGE
FROM SALES
ORDER BY DATE DESC --, rowid DESC ...
Go to Top of Page
   

- Advertisement -