I have the following stored procedure:
SELECT mv_rep as Rep,mv_account,aca_name as Name,sum(mv_cost)as Cost, sum(mv_sell_value)as Value,
sum(mv_sell_value - mv_cost)as profit,
datepart(month,[mv_date])as Month,datepart(year,[mv_date])as Year
From movements m1 INNER JOIN ADDRESSES ON( m1.mv_ACCOUNT = ADDRESSES.ACA_ACCOUNT)
WHERE NOT EXISTS
(Select * from movements m2
where m2.mv_Account = m1.mv_account
and YEAR(mv_Date) = YEAR(getdate())
and Month(mv_Date) = Month(getdate()))
AND mv_date >= DATEADD(mm, -6, getdate())and
and mv_rep in ("01","02","03","04","10" and
mv_type in ("no","eo","op"
What this does is find out who hasen't bought anything this month and show the previous 6 months spending for these customers.
It works fine but I would like to include in this the same previous 6 months data but for the previour year.
So at the end i will have a bunch accounts who havent speant this month, with their spend for the previous 6 months in this year and the previous 6 months for last year.
Many thanks to all the bods that can help me with this one.
SELECT mv_rep as Rep,mv_account,aca_name as Name,sum(mv_cost)as Cost, sum(mv_sell_value)as Value,
sum(mv_sell_value - mv_cost)as profit,
datepart(month,[mv_date])as Month,datepart(year,[mv_date])as Year
From movements m1 INNER JOIN ADDRESSES ON( m1.mv_ACCOUNT = ADDRESSES.ACA_ACCOUNT)
WHERE NOT EXISTS
(Select * from movements m2
where m2.mv_Account = m1.mv_account
and YEAR(mv_Date) = YEAR(getdate())
and Month(mv_Date) = Month(getdate()))
AND mv_date >= DATEADD(mm, -6, getdate())and
and mv_rep in ("01","02","03","04","10" and
mv_type in ("no","eo","op"
What this does is find out who hasen't bought anything this month and show the previous 6 months spending for these customers.
It works fine but I would like to include in this the same previous 6 months data but for the previour year.
So at the end i will have a bunch accounts who havent speant this month, with their spend for the previous 6 months in this year and the previous 6 months for last year.
Many thanks to all the bods that can help me with this one.