Hi,I'm having some trouble finding a solution to a problem, its been a few hours now and I think I just completely loose track of what I'm doing and ended up a few times bashing and mix matching keywords around the lines of 'OUTER', 'MERGE', 'RIGHT', EXCEPT' and 'CROSS APPLY'. I need some extra help with this one.I've got a table that holds batches of 52-53 records (one per week of the year) per every PersonId. Problem was that many of them had duplicated records for X year (eg. 2 or 3 year 2015 batches), deleting the duplicates was more or less a pain but now the task is to complete the ones that are missing weeks, so some of them have 20 records for year 2015 (52 week year) then I need to add the missing 32 weeks.Payment table is a bit like:[code="sql"]CREATE TABLE #Payment ( PaymentId int, PersonId int, PaymentYear int, PaymentWeek int, EePaid decimal(18,2), ErPaid decimal (18,2))DECLARE @i INT = 1WHILE (@i <= 52) BEGIN INSERT INTO #Payment ( PaymentId, PersonId, PaymentYear, PaymentWeek, EePaid, ErPaid) VALUES (999 + @i, 1, 2015, @i, 0, 0) SET @i = @i + 1ENDINSERT INTO #Payment ( PaymentId, PersonId, PaymentYear, PaymentWeek, EePaid, ErPaid) VALUES (1256, 2, 2015, 26, 0, 0), (1257, 2, 2015, 27, 0, 0), (1258, 2, 2015, 28, 0, 0), (1259, 2, 2015, 29, 0, 0), (1260, 2, 2015, 30, 0, 0), (1261, 2, 2015, 31, 0, 0), (1262, 2, 2015, 32, 0, 0)SELECT * FROM #Payment[/code]As you can see PersonId 1 have a full year 2015 but PersonId 2 only has week 26 'till 32I'm getting the corresponding Weeks of the year with the following CTE:[code="sql"]WITH E(n) AS(SELECT 0 AS n UNION ALLSELECT 0 AS n UNION ALLSELECT 0 AS n UNION ALLSELECT 0 AS n UNION ALLSELECT 0 AS n UNION ALLSELECT 0 AS n UNION ALLSELECT 0 AS n UNION ALLSELECT 0 AS n UNION ALLSELECT 0 AS n UNION ALLSELECT 0 AS n ),cteDates(Monday) AS( SELECT TOP(DATEDIFF(dd, '2005-01-01', DATEADD(yy, DATEDIFF(yy,0,getdate()) + 1, -1))/7) DATEADD(ww, (DATEDIFF(dd, 0, '2005-01-01')/7) + ROW_NUMBER() OVER(ORDER BY (SELECT NULL)), 0) Monday FROM E a, E b, E c, E d)SELECT Monday AS WkStDt ,YEAR(Monday) AS WkYear ,ROW_NUMBER() OVER(PARTITION BY YEAR(Monday) ORDER BY Monday) AS WkNoFROM cteDates [/code]So I figured I could Insert from a join but my theory came up short and I ended up with all sorts, here's my last take and its a general mess, but will illustrate perfectly the insanity I reached trying to solve this problem. (* WeekList in the query is the CTE)[code="sql"]insert into Payment ( PersonId, PaymentYear, WeekNumber, WeekStartDate)select t1.PersonId, t1.WkYear, t1.WkNo, t1.WkStDtfrom( SELECT Person.PersonId, CA.WkYear, CA.WkNo, CA.WkStDt FROM Person INNER JOIN ( SELECT Person.PayeeNo, WeekList.WkYear, WeekList.WkNo, WeekList.WkStDt FROM Person CROSS APPLY WeekList ) CA ON Person.PayeeNo = CA.PayeeNo WHERE Person.PayeeNo IN ( select sd.PayeeNo from ( select PayeeNo, PaymentYear from Payment group by PensionNo, PaymentYear having count(*) < 52 ) sd ) INTERSECT SELECT Payment.PersonId, Payment.PayeeNo, Payment.PaymentYear, Payment.WeekNumber, Payment.WeekStartDate FROM Payment WHERE Payment.PayeeNo IN ( select sd.PensionNo from ( select PayeeNo, PaymentYear from Payment group by PayeeNo, PaymentYear having count(*) < 52 ) sd ) ) t1[/code]Any help would be appretiated :hehe:
↧