SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Capturing Historical/past data from Current data in tables


Capturing Historical/past data from Current data in tables

Author
Message
thomasrichardson2000
thomasrichardson2000
SSC-Enthusiastic
SSC-Enthusiastic (170 reputation)SSC-Enthusiastic (170 reputation)SSC-Enthusiastic (170 reputation)SSC-Enthusiastic (170 reputation)SSC-Enthusiastic (170 reputation)SSC-Enthusiastic (170 reputation)SSC-Enthusiastic (170 reputation)SSC-Enthusiastic (170 reputation)

Group: General Forum Members
Points: 170 Visits: 154
Below is some sample ddl and data. Each row represents an item that is on rent between a period of time. If I run the query as of now it gives me all the rentals in there current state as of today ,I want to be able to see what rentals were actually active/on rent on a particular day in the past e.g yesterday, 2 days ago, 3 days ago etc. I am finding it difficult to do this as I am looking at the data as of today, the rental could be closed(off-rent) today but active yesterday, How do I capture historical data? There is more columns in the table, I posted the below as I thought date columns would be most relevant, All ideas are welcome and any help is greatly appreciated.


IF OBJECT_ID('TempDB..#mytable','U') IS NOT NULL DROP TABLE #mytable
CREATE TABLE #mytable
( NUMBER INT PRIMARY KEY CLUSTERED,
CLOSE_DATE datetime,
Date_out datetime,
Date_in datetime
)
INSERT INTO #mytable (Number, CLOSE_DATE, Date_out, Date_in)

SELECT '1004','15/10/2012','28/09/2012','15/10/2012' UNION ALL
SELECT '1005','15/10/2012','12/09/2012','15/10/2012' UNION ALL
SELECT '1006','14/10/2012','17/06/2012','14/10/2012' UNION ALL
SELECT '1007','14/10/2012','17/08/2012','12/10/2012' UNION ALL
SELECT '1008','14/10/2012','17/05/2012','11/10/2012' UNION ALL
SELECT '1009','14/10/2012','01/06/2012','14/10/2012' UNION ALL
SELECT '1010','13/10/2012','17/03/2012','10/10/2012' UNION ALL
SELECT '1011','14/07/2012','01/07/2012','13/07/2012' UNION ALL
SELECT '1012','21/09/2012','17/09/2012','24/09/2012' UNION ALL
SELECT '1013','03/08/2012','21/06/2012','03/08/2012' UNION ALL
SELECT '1014','04/09/2012','17/08/2012','04/09/2012'

SELECT Number,Close_Date,Date_Out , Date_In
FROM #mytable
Sean Lange
Sean Lange
SSC Guru
SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)

Group: General Forum Members
Points: 62065 Visits: 17954
Thanks for the ddl and sample data.

Something like this?


declare @TargetDate datetime = dateadd(dd, datediff(dd, 0, getdate()) - 2, 0) --this will get a date 2 days ago
select @TargetDate

select * from #mytable
where Date_out <= @TargetDate and Date_in >= @TargetDate



_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Modens splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Sean Lange
Sean Lange
SSC Guru
SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)

Group: General Forum Members
Points: 62065 Visits: 17954
Actually that probably would not get items that are not yet returned. You probably need something like this to accommodate a NULL in Date_in.


select * from #mytable
where Date_out <= @TargetDate and (Date_in >= @TargetDate or Date_in is null)



_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Modens splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
thomasrichardson2000
thomasrichardson2000
SSC-Enthusiastic
SSC-Enthusiastic (170 reputation)SSC-Enthusiastic (170 reputation)SSC-Enthusiastic (170 reputation)SSC-Enthusiastic (170 reputation)SSC-Enthusiastic (170 reputation)SSC-Enthusiastic (170 reputation)SSC-Enthusiastic (170 reputation)SSC-Enthusiastic (170 reputation)

Group: General Forum Members
Points: 170 Visits: 154
I was off the radar yesterday, thanks for the help sean , its got most of the way
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search