Question:for 2014-02-01 , you have Groups "B" .It is correct or typo ?DECLARE @Transactions TABLE ( EmpID CHAR(5), TransDate DATE)DECLARE @Master TABLE ( EmpID CHAR(5), EffectiveDateFr DATE, Groups CHAR(1) )INSERT INTO @Transactions(EmpID,TransDate)VALUES ('00001','1/1/2014') ,('00001', '1/2/2014') ,('00001', '1/3/2014') ,('00001', '1/4/2014') ,('00001', '1/5/2014') ,('00001', '1/6/2014') ,('00001', '1/15/2014') ,('00001', '2/1/2014') ,('00001', '2/2/2014') ,('00001', '2/20/2014')INSERT INTO @Master(EmpID, EffectiveDateFr, Groups)VALUES('00001', '1/1/2014', 'A'), ('00001', '1/5/2014' ,'B'), ('00001', '1/9/2014' ,'C'), ('00001', '2/1/2014' ,'B'), ('00001', '2/20/2014' ,'A')SELECT EmpID, TransDate, GroupsFROM ( SELECT tr.EmpID, tr.TransDate, ms.Groups , RN = Row_Number() Over (Partition By tr.EmpID, tr.TransDate ORDER BY tr.EmpID, tr.TransDate ,ms.Groups DESC) FROM @Transactions tr INNER JOIN @Master ms ON tr.EmpID=ms.EmpID AND tr.TransDate>= ms.EffectiveDateFr )AWHERE A.RN = 1ORDER BY EmpID, TransDate, Groups
output:EmpID TransDate Groups00001 2014-01-01 A00001 2014-01-02 A00001 2014-01-03 A00001 2014-01-04 A00001 2014-01-05 B00001 2014-01-06 B00001 2014-01-15 C00001 2014-02-01 C00001 2014-02-02 C00001 2014-02-20 C
sabinWeb MCP