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
 General SQL Server Forums
 New to SQL Server Programming
 Query help for history

Author  Topic 

Ranjit.ileni
Posting Yak Master

183 Posts

Posted - 2011-05-03 : 01:05:41
at first ATP_FA_FLIGHTS table having following data
FLIGHT_ID FLIGHT_NAME FLIGHT_EXTRANAME
151 old flight old extra name

in the above table user can update by using front end application
when ever user trying to update records the new value, old value of a column will store in ATP_TRANSACTION_HISTORY table

consider ATP_TRANSACTION_HISTORY table having following data

TABLE_NAME HISTORY_TABLE_KEY_ID HISTORY_FIELD_NAME HISTORY_OLD_VALUE HISTORY_CURRENT_VALUE
ATP_FA_FLIGHTS 151 FLIGHT_NAME rewnow new flight
ATP_FA_FLIGHTS 151 FLIGHT_EXTRANAME ewrew new extra name

when ever updation completed data will update in ATP_FA_FLIGHTS
now ATP_FA_FLIGHTS table having following data

FLIGHT_ID FLIGHT_NAME FLIGHT_EXTRANAME
151 new flight new extra name


like wise user can update any record from any table
data base having 50+ table , 2000+ columns

OUTPUT:
when i run a Query I need get following results

STATUS FLIGHT_ID FLIGHT_NAME FLIGHT_EXTRANAME
old 151 old flight old extra name
new 151 new flight new extra name


Please help .

Thanks in Advance

--Ranjit

vaibhavktiwari83
Aged Yak Warrior

843 Posts

Posted - 2011-05-03 : 03:18:08
You must have date column in history table.

Vaibhav T

If I cant go back, I want to go fast...
Go to Top of Page

Ranjit.ileni
Posting Yak Master

183 Posts

Posted - 2011-05-03 : 03:54:44
yes,
ATP_TRANSACTION_HISTORY table have HISTORY_TRANSACTION_DATE column


--Ranjit
Go to Top of Page

Seventhnight
Master Smack Fu Yak Hacker

2878 Posts

Posted - 2011-05-03 : 06:12:46
I think this will do it...



Declare @FlightId
Set @FlightId = 151

;With cte As (
Select
FLIGHT_NAME = HISTORY_OLD_VALUE,
RankNum = Row_Number()Over(Partition By FLIGHT_ID, HISTORY_FIELD_NAME Order By HISTORY_TRANSACTION_DATE Desc)
From ATP_TRANSACTION_HISTORY
)

Select
STATUS = 'new'
FLIGHT_ID,
FLIGHT_NAME,
FLIGHT_EXTRANAME
From ATP_FA_FLIGHTS
Where FLIGHT_ID = @FlightId

Union

Select
STATUS = 'old'
A.FLIGHT_ID,
FLIGHT_NAME = isnull(B.FLIGHT_NAME, A.FLIGHT_NAME),
FLIGHT_EXTRANAME = isnull(C.FLIGHT_EXTRANAME, A.FLIGHT_EXTRANAME)
From ATP_FA_FLIGHTS A
Left Join cte B
On A.FLIGHT_ID = B.HISTORY_TABLE_KEY_ID
and 'FLIGHT_NAME' = B.HISTORY_FIELD_NAME
and 1 = B.RankNum
Left Join cte C
On A.FLIGHT_ID = C.HISTORY_TABLE_KEY_ID
and 'FLIGHT_EXTRANAME' = C.HISTORY_FIELD_NAME
and 1 = C.RankNum
Where A.FLIGHT_ID = @FlightId



Corey

I Has Returned!!
Go to Top of Page
   

- Advertisement -