February 8, 2018 at 2:20 pm
[Store] [Planned Sales] [Planned Sales Year] [Actual Sales]
South - 50 - 50 - 22
How can I adapt my select statement so my [Planned Sales Year] = 120 (which is correct) and not 50. Removing the SaleMonth clause would result in my [Planned Sales] also increasing to 120 which I don't want as I want to see the year to date position for planned and actual sales.
Thanks
BO
create table #Sales
(
SaleMonth int null,
Store varchar(25) null,
PlannedSales float null,
PlannedSalesYear float null,
ActualSales float null)
February 8, 2018 at 3:14 pm
Use a CASE expression instead of a WHERE clause.
select
Store,
sum(CASE WHEN SaleMonth <= 5 THEN PlannedSales END) as 'Planned Sales',
sum(PlannedSalesYear) as 'Planned Sales Year',
sum(ActualSales) as 'Actual Sales'
from #Sales
group by Store
drop table #Sales
Drew
J. Drew Allen
Business Intelligence Analyst
Philadelphia, PA
February 8, 2018 at 10:00 pm
Including the case statement for the "Actual Sales" column would ensure you get the sales for the first 5 months.
Currently the "ActualSales" for the rest of the months is null for SaleMonth>5 so it doesnt matter
select
Store,
sum(CASE WHEN SaleMonth <= 5 THEN PlannedSales END) as 'Planned Sales',
sum(PlannedSalesYear) as 'Planned Sales Year',
sum(CASE WHEN SaleMonth <= 5 THEN ActualSales END) as 'Actual Sales' /*Added the statement from Drews Solution*/
from #Sales
group by Store
February 12, 2018 at 2:54 pm
Thanks for the responses guys.
Just what I needed.
BO
Viewing 4 posts - 1 through 3 (of 3 total)
You must be logged in to reply to this topic. Login to reply