SQL Server 2005
Please help.
……
SELECT
case when year(FJ.RDate)=@year and month(FJ.RDate)=1
then sum(JA.Sales) else 0 end SumJanSaleses,
case when year(FJ.RDate)=@year and month(FJ.RDate)=2
then sum(JA.Sales) else 0 end SumFebSaleses,
case when year(FJ.RDate)=@year and month(FJ.RDate)=3
then sum(JA.Sales) else 0 end SumMarSaleses,
case when year(FJ.RDate)=@year and month(FJ.RDate)=1
then (sum(JA.Sales)-sum(JA.Costs)) else 0 end JanMarginDiff,
case when year(FJ.RDate)=@year and month(FJ.RDate)=2
then (sum(JA.Sales)-sum(JA.Costs)) else 0 end FebMarginDiff,
case when year(FJ.RDate)=@year and month(FJ.RDate)=1
then (sum(JA.Sales)-sum(JA.Costs))*100/
sum(JA.Sales) else 0 end JanMarginPro
....
FROM FJ,AN
but I have to consider when the sum(JA.Sales)=0 don’t divide by zero.
When I write
… …
case when year(FJ.RDate)=@year and month(FJ.RDate)=1
then
(
case when sum(JA.Sales)=0 then
else
(sum(JA.Sales)-sum(JA.Costs))*100/
sum(JA.Sales)
)
else 0 end JanMarginPro
… …
I get errors.
Please help.
……
SELECT
case when year(FJ.RDate)=@year and month(FJ.RDate)=1
then sum(JA.Sales) else 0 end SumJanSaleses,
case when year(FJ.RDate)=@year and month(FJ.RDate)=2
then sum(JA.Sales) else 0 end SumFebSaleses,
case when year(FJ.RDate)=@year and month(FJ.RDate)=3
then sum(JA.Sales) else 0 end SumMarSaleses,
case when year(FJ.RDate)=@year and month(FJ.RDate)=1
then (sum(JA.Sales)-sum(JA.Costs)) else 0 end JanMarginDiff,
case when year(FJ.RDate)=@year and month(FJ.RDate)=2
then (sum(JA.Sales)-sum(JA.Costs)) else 0 end FebMarginDiff,
case when year(FJ.RDate)=@year and month(FJ.RDate)=1
then (sum(JA.Sales)-sum(JA.Costs))*100/
sum(JA.Sales) else 0 end JanMarginPro
....
FROM FJ,AN
but I have to consider when the sum(JA.Sales)=0 don’t divide by zero.
When I write
… …
case when year(FJ.RDate)=@year and month(FJ.RDate)=1
then
(
case when sum(JA.Sales)=0 then
else
(sum(JA.Sales)-sum(JA.Costs))*100/
sum(JA.Sales)
)
else 0 end JanMarginPro
… …
I get errors.