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 2008 Forums
 Transact-SQL (2008)
 Join Question

Author  Topic 

murrayb3024
Yak Posting Veteran

79 Posts

Posted - 2013-06-19 : 11:26:40
I am having a complete brain fart on this one. I have 2 tables and there is an ID to link them. One of the fields in table B is a calculated value. Table B is also an archive table so data just gets appended to it and there is a CalculatedDate. I was thinking I would use Max function on calculated date but then I found out that a person's calculated value may not be done each month.

Example

TableA
ID
1
2
3


TableB
ID CalcValue CalcDate
1 1234 4/30/2013
1 1211 5/31/2013
2 9999 5/31/2013
3 7777 3/31/2013
3 6666 4/30/2013


So for my results I would want:

ID CalcValue
1 1211
2 9999
3 6666

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-06-19 : 11:31:38
[code]
SELECT ID,CalcValue
FROM
(
SELECT ROW_NUMBER() OVER (PARTITION BY ID ORDER BY CalcDate DESC) AS Seq, *
FROM TableB
)t
WHERE Seq=1
[/code]

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

murrayb3024
Yak Posting Veteran

79 Posts

Posted - 2013-06-19 : 12:34:14
Thank you for the help
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-06-20 : 01:02:26
welcome

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page
   

- Advertisement -