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
 Getting the latest date on another table
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

karrojo
Starting Member

26 Posts

Posted - 05/30/2012 :  02:50:36  Show Profile  Reply with Quote

Good Day!

I have two tables. Master table (contains the main info of a person) and Updates Table (all updates done in a record). From the Master Table, i need to get the latest Update in the UPdates table.

i have this query but gives me null result:

select a.record_no, a.remarks, a.d_updated,
b.record_no, b.remarks, b.d_updated
from master_record a
left join (select top 1 record_no, reason, d_updated
from checks_table
order by d_updated desc) b
on a.record_no = b.record_no

Result:
Record NO Remarks Date_Updated Record_no Remarks Date_updated
1 xxxx 01/01/2012 null null null
2 xxxx 03/01/2012 null null null

i really need all your help. thank you very much!

tobee

nigelrivett
Flowing Fount of Yak Knowledge

United Kingdom
3328 Posts

Posted - 05/30/2012 :  03:35:24  Show Profile  Visit nigelrivett's Homepage  Reply with Quote
Someting is odd.
b.remarks doesn't exist in the derived table so this should give an error.
A bigger proble uis that there is only one row from the derived table so you will only ever get one record_id row.

I suspect you might want something like

select a.record_no, a.remarks, a.d_updated,
b.record_no, b.remarks, b.d_updated
from master_record a
left join (select record_no, reason, d_updated, remarks
from checks_table t
join (select record_no, d_updated = max(d_updated) from checks_table group by record_no) t2
on t.record_no = t2.record_no
and t.d_updated = t2.d_updated) b
on a.record_no = b.record_no

==========================================
Cursors are useful if you don't know sql.
SSIS can be used in a similar way.
Beer is not cold and it isn't fizzy.

Edited by - nigelrivett on 05/30/2012 03:36:31
Go to Top of Page

karrojo
Starting Member

26 Posts

Posted - 05/30/2012 :  04:13:33  Show Profile  Reply with Quote

thanks very much NIgelRivett!... I got the result correctly!... more power to YOu and SQLTeam!

thanks again! ^_^
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.05 seconds. Powered By: Snitz Forums 2000