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
 IndID Last Updated in Several Tables
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

SQLNOVICE999
Yak Posting Veteran

57 Posts

Posted - 02/11/2013 :  16:12:37  Show Profile  Reply with Quote
Hi Guys,

There are IndID and UpdateDate columns in 18 of the 200 plus tables in our database. What I need to find out is how many IndID was updated in the last 5 years. Latest UpdateDate for each IndID from those 18 tables need to be used as the last update.

I was thinking I should make a temp table with all IndID and then run 18 Update Queries to find the last update date for each IndID. Anyone who has better solution please suggest?

Thanks,
Laura

James K
Flowing Fount of Yak Knowledge

3744 Posts

Posted - 02/11/2013 :  18:54:32  Show Profile  Reply with Quote
For a given IndID, are you looking for the latest updateDate value in each table, or the the latest across all tables? If it is the latter, what you are thinking seems to be pretty much the only solution.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52325 Posts

Posted - 02/11/2013 :  23:15:27  Show Profile  Reply with Quote
As per what I understood you're trying to identify IndID that has latest UpdateDate faling within last 5 years. if that being case, you can use something like

SELECT IndID
FROM (SELECT IndID,MAX(UpdateDate) AS MaxUpdate
      FROM view
      GROUP BY IndID
     )t
WHERE MaxUpdate >=DATEADD(yy,DATEDIFF(yy,0,GETDATE())-5,0)
AND MaxUpdate <DATEADD(yy,DATEDIFF(yy,0,GETDATE()),0)


the view would be like

SELECT IndID,UpdateDate
FROM table1
UNION ALL
SELECT IndID,UpdateDate
FROM table2
....


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

Go to Top of Page

SQLNOVICE999
Yak Posting Veteran

57 Posts

Posted - 02/12/2013 :  08:56:44  Show Profile  Reply with Quote
Thanks guys. Yes James it the the latest update across all 18 tables.

Thanks,
Laura
Go to Top of Page

SQLNOVICE999
Yak Posting Veteran

57 Posts

Posted - 02/12/2013 :  08:57:44  Show Profile  Reply with Quote
quote:
Originally posted by SQLNOVICE999

Thanks guys. Yes James it the the latest update across all 18 tables.

Thanks,
Laura



Thanks Visakh that is a better solution than what I had thought.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52325 Posts

Posted - 02/12/2013 :  09:50:53  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