Ok I've been able to build a query that gets me 'Deactivated' (Nothing in next week) and 'Moved Out' (Client remains client but product change) working.
I will still need to do 'Acquired' (First time this product is received), 'Moved In' (What the new product is that wasn't in previous week). And 'Reactivated' (Meaning the client was inactive, but the last time they were active, they had the same product that they were reactivated into).
The criteria are [customer #] and [Group Description] (Which is the product).
--DEACTIVATED and MOVED OUT
SELECT
[Fiscal Year],
[Fiscal Week],
[Customer #],
[Customer Full Name],
[DG Name],
[Group Description],
[Customer Type],
[Sales ORG],
[Sales Person],
[Sales Manager],
[Sales Director],
[Movement],
[Fiscal Week1],
[Fiscal Week2]
INTO #DEACTnOUT
FROM (
SELECT
a.[Fiscal Year],
--a.[Week Num] + 1 as [Week Num],
a.[Fiscal Week] + 1 as [Fiscal Week],
a.[Customer #],
BaseTb.[Customer Full Name],
IsNull(BaseTb.[DG Name],'') as [DG Name],
a.[Group Description],
a.[Customer Type],
a.[Sales ORG],
BaseTb.[Sales Person],
BaseTb.[Sales Manager],
BaseTb.[Sales Director],
CASE
WHEN a.[Week Num]+1 = c.[Week Num] THEN 'MOVED OUT' ELSE 'Deactivated' END
as [Movement],
a.[Fiscal Week] as [Fiscal Week1],
c.[Fiscal Week] as [Fiscal Week2]
FROM #Viewoa as a
LEFT JOIN #CustomerMovementCountBaseTable as BaseTb
ON a.[Customer #] = BaseTb.[Customer #] AND a.[Week Num] = BaseTb.[Week Num]
LEFT JOIN #Viewoa as c
ON a.[Customer #] = c.[Customer #] AND a.[Week Num]+1 = c.[Week Num]
WHERE NOT EXISTS(
SELECT *
FROM #Viewoa as b
WHERE b.[Week Num] = a.[Week Num] + 1
AND a.[Group Description] = b.[Group Description]
AND a.[Customer #] = b.[Customer #])
AND a.[Fiscal Week] < 45 )aaa
--AND a.[Customer #]='07488701' ) a
GROUP BY
[Fiscal Year],
[Fiscal Week],
[Customer #],
[Customer Full Name],
[DG Name],
[Group Description],
[Customer Type],
[Sales ORG],
[Sales Person],
[Sales Manager],
[Sales Director],
[Movement],
[Fiscal Week1],
[Fiscal Week2]
HAVING [Customer #] = '07490551' AND [Fiscal Week] = @RunWeek
ORDER BY [Fiscal Week], [Customer #], [Group Description], [Movement]
I need to exclude anything that is in the previous week from the main table with the same group description but give it the value of MOVED IN the current week in the movement field.