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 |
selva.v.kumar
Starting Member
1 Post |
Posted - 2014-07-22 : 23:54:08
|
Hi,There are two tables. One is the source table and the other is lookup(crosswalk) table. We need to do the join between source table column1 with lookup table column1 and display/take the column2 of lookup table.My concern is, there is some complex logic involved to do the join(lookup) between these 2 columns.Both are date columns. Source column has some 40 date values but lookup table has some 4 date values only. We need to join 40 date columns with 4 date columns based on few conditions.Here is the table structure and data in source table (#Employee).Create table #Employee(EligibleDate date)insert into #EmployeeSelect '2011/07/01' UNIONSelect '2011/08/01' UNIONSelect '2011/08/17' UNIONSelect '2011/09/01' UNIONSelect '2011/10/01' UNIONSelect '2011/11/01' UNIONSelect '2011/12/01' UNIONSelect '2012/01/01' UNIONSelect '2012/02/01' UNIONSelect '2012/03/01' UNIONSelect '2012/03/19' UNIONSelect '2012/04/01' UNIONSelect '2012/05/01' UNIONSelect '2012/05/25' UNIONSelect '2012/06/26' UNIONSelect '2012/07/01' UNIONSelect '2012/08/01' UNIONSelect '2012/09/01' UNIONSelect '2012/09/30' UNIONSelect '2012/10/01' UNIONSelect '2012/11/01' UNIONSelect '2012/12/01' UNIONSelect '2013/01/01' UNIONSelect '2013/02/01' UNIONSelect '2013/03/01' UNIONSelect '2013/04/01' UNIONSelect '2013/05/01' UNIONSelect '2013/06/01' UNIONSelect '2013/07/01' UNIONSelect '2013/08/01' UNIONSelect '2013/09/01' UNIONSelect '2013/10/01' UNIONSelect '2013/11/01' UNIONSelect '2013/12/01' UNIONSelect '2014/01/01' UNIONSelect '2014/02/01' UNIONSelect '2014/03/01' UNIONSelect '2014/04/01' UNIONSelect '2014/05/01' UNIONSelect '2014/06/01' Here is the table structure and data in lookup table (#lookupEmployee)Create table #lookupEmployee(Periodstartdate date,Plandesc varchar(3))insert into #lookupEmployeeselect'2011-07-01','ABC' unionselect'2012-05-25','DEF' unionselect'2012-06-26','GHI' unionselect'2012-09-30','JKL' Basically what it means is column 'Plandesc' value starts from that particular date to till the next date value. Eg:'ABC' value start from 2011-07-01 to till '2012-05-24' because from '2012-05-25', new plan DEF' starts. Similarly this DEF value starts from 2012-05-25' to '2012-06-25' as from '2012-06-26', the new plan 'GHI' starts. This goes till 2012-09-29'. From 2012-09-30, plan 'JKL' starts and any date after this date has 'JKL' only.Here is the output we want after the lookup or joining is doneSELECT * FROM #Finaloutput---DROP TABLE #FinaloutputCreate table #Finaloutput(EligibleDate date,Plandesc varchar(3))insert into #FinaloutputSelect '2011/07/01' ,'ABC' unionSelect '2011/08/01' ,'ABC' unionSelect '2011/08/17' ,'ABC' unionSelect '2011/09/01' ,'ABC' unionSelect '2011/10/01' ,'ABC' unionSelect '2011/11/01' ,'ABC' unionSelect '2011/12/01' ,'ABC' unionSelect '2012/01/01' ,'ABC' unionSelect '2012/02/01' ,'ABC' unionSelect '2012/03/01' ,'ABC' unionSelect '2012/03/19' ,'ABC' unionSelect '2012/04/01' ,'ABC' unionSelect '2012/05/01' ,'ABC' unionSelect '2012/05/25' ,'DEF' unionSelect '2012/06/26' ,'GHI' unionSelect '2012/07/01' ,'GHI' unionSelect '2012/08/01' ,'GHI' unionSelect '2012/09/01' ,'GHI' unionSelect '2012/09/30' ,'JKL' unionSelect '2012/10/01' ,'JKL' unionSelect '2012/11/01' ,'JKL' unionSelect '2012/12/01' ,'JKL' unionSelect '2013/01/01' ,'JKL' unionSelect '2013/02/01' ,'JKL' unionSelect '2013/03/01' ,'JKL' unionSelect '2013/04/01' ,'JKL' unionSelect '2013/05/01' ,'JKL' unionSelect '2013/06/01' ,'JKL' unionSelect '2013/07/01' ,'JKL' unionSelect '2013/08/01' ,'JKL' unionSelect '2013/09/01' ,'JKL' unionSelect '2013/10/01' ,'JKL' unionSelect '2013/11/01' ,'JKL' unionSelect '2013/12/01' ,'JKL' unionSelect '2014/01/01' ,'JKL' unionSelect '2014/02/01' ,'JKL' unionSelect '2014/03/01' ,'JKL' unionSelect '2014/04/01' ,'JKL' unionSelect '2014/05/01' ,'JKL' unionSelect '2014/06/01' ,'JKL' Can you please help me to get the code for getting the output like this ? |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2014-07-23 : 03:40:48
|
[code]SELECT e.EligibleDate, f.PlanDescFROM #Employee AS eOUTER APPLY ( SELECT TOP(1) l.PlanDesc FROM #LookupEmployee AS l WHERE l.PeriodStartDate <= e.EligibleDate ORDER BY l.PeriodStartDate DESC ) AS f;[/code] Microsoft SQL Server MVP, MCT, MCSE, MCSA, MCP, MCITP, MCTS, MCDBA |
|
|
|
|
|
|
|