SELECT
SUM(CASE WHEN month(soldDate) = @month AND year(soldDate) = @year THEN CostPrice ELSE 0 END) AS CostCurrentMonth,
SUM(CASE WHEN month(soldDate) = @month AND year(soldDate) = @year THEN SoldQuantity ELSE 0 END) AS QtyCurrentMonth,
SUM(CASE WHEN @month = 1 AND month(soldDate) = 12 AND
year(soldDate) = @year THEN CostPrice ELSE CASE WHEN month(soldDate) = @month - 1 AND year(soldDate) = @year THEN CostPrice ELSE 0 END END) AS CostLastMonth,
SUM(CASE WHEN @month = 1 AND month(soldDate) = 12 AND
year(soldDate) = @year THEN SoldQuantity ELSE CASE WHEN month(soldDate) = @month - 1 AND year(soldDate) = @year THEN SoldQuantity ELSE 0 END END) AS QtyLastMonth,
SUM(CASE WHEN month(soldDate) = @month AND year(soldDate) = @year - 1 THEN CostPrice ELSE 0 END) AS CostSameMonthLastYear,
SUM(CASE WHEN month(soldDate) = @month AND year(soldDate) = @year - 1 THEN SoldQuantity ELSE 0 END) AS QtySameMonthLastYear
FROM table1......
|