Мне нужно найти список клиентов с доходом за текущий месяц, где за последние 6 месяцев доход был нулевым.
DECLARE @cm AS DATE = (
SELECT cm
FROM dbo.datebreak
)
SELECT cmt_ACCOUNTS.[GA CUSTOMER ID]
,SUM(REVENUE) AS REV
FROM cmt_ACCOUNTS
INNER JOIN MDATA ON cmt_ACCOUNTS.[ACCOUNT NUMBER] = MDATA.[AccNumber]
WHERE (MDATA.TobyBill = @cm)
GROUP BY cmt_ACCOUNTS.[GA CUSTOMER ID]
,cmt_ACCOUNTS.[CUSTOMER NAME]
,MDATA.TERR
,MDATA.CHANNEL
,MDATA.ASM
,MDATA.EMPNAME
,MDATA.[Group Product]
,MDATA.TobyBill
HAVING (MDATA.[Group Product] = N'TDI')
AND MDATA.CHANNEL = 'TSR'
AND (
MDATA.TobyBill BETWEEN DATEADD(month, - 6, @cm)
AND DATEADD(month, - 1, @cm)
AND sum(REVENUE) <= 0
)