SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 max problem
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

yohan_w
Starting Member

Indonesia
2 Posts

Posted - 01/11/2013 :  02:19:29  Show Profile  Reply with Quote
hi,I'm having trouble with max functions

here are the situations
I have table like below

---------------------------------------
| ID | Salary | Date |
---------------------------------------
| 1001 | 300 | 2012-01-01 |
---------------------------------------
| 1001 | 350 | 2012-12-01 |
---------------------------------------

Now I have to get the salary from ID 1001
which max(date) <= 2013-01-01

I tried this:
select distinct ID, Salary from Salary_Table where ID='1001
group by ID, Salar
having Cast(convert(varchar(8),max([Date]), 112) as datetime)<='20130101'

the correct result I want is
1001 350 but it gives me
1001 30
1001 350

please help me
thx u,...

visakh16
Very Important crosS Applying yaK Herder

India
52249 Posts

Posted - 01/11/2013 :  02:59:38  Show Profile  Reply with Quote

SELECT ID,Salary,[Date]
FROM
(
SELECT ROW_NUMBER() OVER (PARTITION BY ID ORDER BY [Date] DESC) AS Seq,*
FROM table
WHERE [date] <='20130101'
)t
WHERE Seq=1


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

yohan_w
Starting Member

Indonesia
2 Posts

Posted - 01/11/2013 :  03:20:32  Show Profile  Reply with Quote
Thank u for your quick reply!

Your code works like a charm and SOLVED my problem :)

thanks!
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52249 Posts

Posted - 01/11/2013 :  03:21:57  Show Profile  Reply with Quote
welcome

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.06 seconds. Powered By: Snitz Forums 2000