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.
Author |
Topic |
stockcer
Starting Member
1 Post |
Posted - 2013-07-30 : 11:06:18
|
Let me explain.table 1David 05/10/2013Peter 05/16/2013table 2Period_Num Begin_Period End_PeriodPeriod 1 01/05/2013 05/15/2013Period 2 05/16/2013 12/31/2013I want the final to readDavid Period 1Peter Period 2Is there a way to do this in sql particularly in a viewThanks |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2013-07-30 : 12:36:59
|
SELECT *FROM dbo.Table2 AS t2INNER JOIN dbo.Table1 AS t1 ON t2.Col2 BETWEEN t1.Begin_Period AND t1.End_Period Microsoft SQL Server MVP, MCT, MCSE, MCSA, MCP, MCITP, MCTS, MCDBA |
|
|
MuMu88
Aged Yak Warrior
549 Posts |
Posted - 2013-07-30 : 12:37:03
|
[CODE]DECLARE @Temp1 TABLE (NAME VARCHAR(16), Period DATE);DECLARE @Temp2 TABLE (Period VARCHAR(16), Begin_Period DATE, End_Period DATE);INSERT INTO @Temp1 VALUES('David', '2013-05-10'),('Peter', '2013-05-16');INSERT INTO @Temp2 VALUES('Period1', '2013-01-05', '2013-05-15'),('Period2', '2013-05-16', '2013-12-31');SELECT Name, (SELECT Period from @Temp2 where T1.period between Begin_Period and End_Period) as Period from @Temp1 T1;[/CODE] |
|
|
|
|
|
|
|