DLTaylor
Posting Yak Master
136 Posts |
Posted - 2008-09-29 : 07:51:37
|
Hello,Im doing some work where I have a 2 tables that im having real problems joining together. I have simplified the problem (and written some Test SQL) and it seems I need to join using dates and ranges? I have 2 tables #table1 and #table2;Table1 contains Activity Start and End dates.Table2 contains Activity Dates – (like a daily log)I have pasted some T-SQL to try explain my problem and simplified the fields etc…Can anyone tell me how its best to join the data so I result in the #FinalTable example?Any advice will be much appreciatedThanks---------------------------------------------/*Please paste T-SQL into query window*/---------------------------------------------CREATE TABLE #table1(CustID nvarchar(6),ActivityStartDate [datetime] NULL,ActivityEndDate [datetime] NULL,DataAssociated char(2))GOSET ANSI_PADDING OFFInsert into #table1select '012345','2008-01-09 00:00:00.000','2008-01-10 00:00:00.000','23' UNION allselect '012345','2008-01-10 00:00:00.000','2008-01-14 00:00:00.000','19' UNION allselect '012345','2008-01-20 00:00:00.000','2008-01-23 00:00:00.000','08'select * from #table1CREATE TABLE #table2(CustID nvarchar(6),ActivityDate [datetime] NULL)Insert into #table2select '012345','2008-01-09 00:00:00.000' UNION allselect '012345','2008-01-10 00:00:00.000' UNION allselect '012345','2008-01-11 00:00:00.000' UNION allselect '012345','2008-01-12 00:00:00.000' UNION allselect '012345','2008-01-13 00:00:00.000' UNION allselect '012345','2008-01-14 00:00:00.000' UNION allselect '012345','2008-01-20 00:00:00.000' UNION allselect '012345','2008-01-21 00:00:00.000' UNION allselect '012345','2008-01-22 00:00:00.000' UNION allselect '012345','2008-01-23 00:00:00.000' select * from #table2--Table I would like to make using a join on #table1 and #table2CREATE TABLE #FinalTable(CustID nvarchar(6),ActivityStartDate [datetime] NULL,ActivityEndDate [datetime] NULL,DataAssociated char(2),ActivityDate [datetime] NULL)GOSET ANSI_PADDING OFFInsert into #FinalTableselect '012345','2008-01-09 00:00:00.000','2008-01-10 00:00:00.000','23','2008-01-09 00:00:00.000' UNION allselect '012345','2008-01-10 00:00:00.000','2008-01-14 00:00:00.000','19','2008-01-10 00:00:00.000' UNION allselect '012345','2008-01-10 00:00:00.000','2008-01-14 00:00:00.000','19','2008-01-11 00:00:00.000' UNION allselect '012345','2008-01-10 00:00:00.000','2008-01-14 00:00:00.000','19','2008-01-12 00:00:00.000' UNION allselect '012345','2008-01-10 00:00:00.000','2008-01-14 00:00:00.000','19','2008-01-13 00:00:00.000' UNION allselect '012345','2008-01-10 00:00:00.000','2008-01-14 00:00:00.000','19','2008-01-14 00:00:00.000' UNION allselect '012345','2008-01-20 00:00:00.000','2008-01-23 00:00:00.000','08','2008-01-20 00:00:00.000' UNION allselect '012345','2008-01-20 00:00:00.000','2008-01-23 00:00:00.000','08','2008-01-21 00:00:00.000' UNION allselect '012345','2008-01-20 00:00:00.000','2008-01-23 00:00:00.000','08','2008-01-22 00:00:00.000' UNION allselect '012345','2008-01-20 00:00:00.000','2008-01-23 00:00:00.000','08','2008-01-23 00:00:00.000' select * from #FinalTabledrop table #table1drop table #table2drop table #FinalTable |
|