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.
| 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 PERCENT08/08/2005 505/23/2005 608/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 PERCENTELSE PERCENT END AS PERCENTAGE |
|
|
DonAtWork
Master Smack Fu Yak Hacker
2167 Posts |
Posted - 2005-09-02 : 12:04:14
|
| Try something like thisselect @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) |
 |
|
|
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 PERCENTAGEFROM SALESWHERE DATE IN (SELECT MAX(DATE) FROM SALES) |
 |
|
|
SamC
White Water Yakist
3467 Posts |
Posted - 2005-09-02 : 12:44:15
|
| [code]SELECT PERCENTAGEFROM SALESWHERE DATE = (SELECT MAX(DATE)FROM SALES)[/code]Could there be more than 1 row that meets the condition of DATE = MAX(Date)? |
 |
|
|
chriskhan2000
Aged Yak Warrior
544 Posts |
Posted - 2005-09-02 : 13:43:00
|
| SELECT PERCENTAGEFROM SALESWHERE 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. |
 |
|
|
VladRUS.ca
Starting Member
23 Posts |
Posted - 2005-09-02 : 13:53:56
|
May be you can use simple:SELECT TOP 1 PERCENTAGEFROM SALESORDER BY DATE DESC --, rowid DESC ... |
 |
|
|
|
|
|