Hi All,
I have two tables. Status and Fourhistory tables.
Status table contains a status column with effectivestart and end dates as history. This column is having history at month level.
Fourhistory table maintains 4 columns as part of history with the use of effectivestart and end dates. Here history capturing is at day level.
Desired Result: I want to merge the status column into FourHistory table.
Below i have given some possible sample scenarios which i face and the third table contains the expected ouput.
Please help me, how to achieve this in T-SQL query.
Thanks in advance.
create table dbo.#Status( ID varchar(50), Status varchar(50), EffectiveStartDate datetime, EffectiveEndDate datetime, Is_Current bit ) Insert into #status values('1','Same','2009-07-01 00:00:00.000', '2013-07-31 23:59:59.000', 0 ) Insert into #status values('1','Lost 13','2013-08-01 00:00:00.000', NULL , 1 ) Insert into #status values('2','New 13','2013-02-01 00:00:00.000', '2014-12-31 23:59:59.000', 0 ) Insert into #status values('2','Same','2015-01-01 00:00:00.000', NULL , 1 ) Insert into #status values('3','Same','2009-07-01 00:00:00.000', '2009-09-30 23:59:59.000', 0 ) Insert into #status values('3','Lost 09','2009-10-01 00:00:00.000', '2013-12-31 23:59:59.000', 0 ) Insert into #status values('3','Inter','2014-01-01 00:00:00.000', NULL , 1 ) CREATE TABLE dbo.#FourHistory( ID varchar(50), Fee varchar(100), Bill varchar(50), A_Date date, B_Date date, EffectiveStartDate datetime, EffectiveEndDate datetime ) Insert into #FourHistory values('1','Variable','Not Applicable','2010-09-02','2010-09-01','2009-07-18 00:34:15.000',NULL) Insert into #FourHistory values('2','Activity','Not Applicable','1900-01-01','1900-01-01','2009-07-18 01:38:34.000','2011-08-22 15:35:24.000') Insert into #FourHistory values('2','Monthly','Not Applicable','2012-05-02','2012-04-27','2011-08-22 15:35:25.000','2014-10-22 13:24:57.000') Insert into #FourHistory values('2','Monthly','Wholesale','2014-10-01','2014-10-01','2014-10-22 13:24:58.000',NULL) Insert into #FourHistory values('3','Variable','Not Applicable','2006-05-08','2006-05-19','2009-07-17 23:22:07.000','2009-07-18 01:11:26.000') Insert into #FourHistory values('3','Activity','Not Applicable','2001-01-01','2001-01-01','2009-07-18 01:11:27.000','2010-02-11 12:48:51.000') Insert into #FourHistory values('3','Variable','Not Applicable','2009-09-18','2009-10-22','2010-02-11 12:48:52.000','2014-01-17 00:05:29.000') Insert into #FourHistory values('3','Daily','Wholesale','2014-01-15','2014-01-20','2014-01-17 00:05:30.000',NULL) CREATE TABLE dbo.#Result( ID varchar(50), Fee varchar(100), Bill varchar(50), A_Date date, B_Date date, Status VARCHAR(50), EffectiveStartDate datetime, EffectiveEndDate datetime ) Insert into #Result values('1','Variable','Not Applicable','2010-09-02','2010-09-01','Same','2009-07-18 00:34:15.000','2013-07-31 23:59:59.000') Insert into #Result values('1','Variable','Not Applicable','2010-09-02','2010-09-01','Lost 13','2013-08-01 00:00:00.000',NULL) Insert into #Result values('2','Activity','Not Applicable','1900-01-01','1900-01-01',NULL,'2009-07-18 01:38:34.000','2011-08-22 15:35:24.000') Insert into #Result values('2','Monthly','Not Applicable','2012-05-02','2012-04-27','New 13','2011-08-22 15:35:25.000','2014-10-22 13:24:57.000') Insert into #Result values('2','Monthly','Wholesale','2014-10-01','2014-10-01','New 13','2014-10-22 13:24:58.000','2014-12-31 23:59:59.000') Insert into #Result values('2','Monthly','Wholesale','2014-10-01','2014-10-01','Same','2015-01-01 00:00:00.000',NULL) Insert into #Result values('3','Variable','Not Applicable','2006-05-08','2006-05-19','Same','2009-07-17 23:22:07.000','2009-07-18 01:11:26.000') Insert into #Result values('3','Activity','Not Applicable','2001-01-01','2001-01-01','Same','2009-07-18 01:11:27.000','2009-09-30 23:59:59.000') Insert into #Result values('3','Activity','Not Applicable','2001-01-01','2001-01-01','Lost','2009-10-01 00:00:00.000','2010-02-11 12:48:51.000') Insert into #Result values('3','Variable','Not Applicable','2009-09-18','2009-10-22','Lost','2010-02-11 12:48:52.000','2013-12-31 23:59:59.000') Insert into #Result values('3','Variable','Not Applicable','2009-09-18','2009-10-22','Inter','2014-01-01 00:00:00.000','2014-01-17 00:05:29.000') Insert into #Result values('3','Daily','Wholesale','2014-01-15','2014-01-20','Inter','2014-01-17 00:05:30.000',NULL) SELECT * FROM #status select * From #FourHistory select * From #result