SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Summing Sales Data


Summing Sales Data

Author
Message
ByronOne
ByronOne
Hall of Fame
Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)

Group: General Forum Members
Points: 3298 Visits: 580
Hi
If I run the script below I get the following output.

[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)
insert into #Sales (SaleMonth, Store, PlannedSales,PlannedSalesYear, ActualSales)
values (1, 'South', 10, 10, 5),
(2, 'South', 10, 10, 3),
(3, 'South', 10, 10, 4),
(4, 'South', 10, 10, 2),
(5, 'South', 10, 10, 8),
(6, 'South', 10, 10, null),
(7, 'South', 10, 10, null),
(8, 'South', 10, 10, null),
(9, 'South', 10, 10, null),
(10, 'South', 10, 10, null),
(11, 'South', 10, 10, null),
(12, 'South', 10, 10, null);
select
Store,
sum(PlannedSales) as 'Planned Sales',
sum(PlannedSalesYear) as 'Planned Sales Year',
sum(ActualSales) as 'Actual Sales'
from #Sales
where SaleMonth <= 5
group by Store
drop table #Sales

drew.allen
drew.allen
SSC-Forever
SSC-Forever (49K reputation)SSC-Forever (49K reputation)SSC-Forever (49K reputation)SSC-Forever (49K reputation)SSC-Forever (49K reputation)SSC-Forever (49K reputation)SSC-Forever (49K reputation)SSC-Forever (49K reputation)

Group: General Forum Members
Points: 49071 Visits: 14720
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
How to post data/code on a forum to get the best help.
How to Post Performance Problems
Make sure that you include code in the appropriate IFCode tags, e.g. [code=sql]<your code here>[/code]. You can find the IFCode tags under the INSERT options when you are writing a post.
george_at_sql
george_at_sql
SSC-Addicted
SSC-Addicted (441 reputation)SSC-Addicted (441 reputation)SSC-Addicted (441 reputation)SSC-Addicted (441 reputation)SSC-Addicted (441 reputation)SSC-Addicted (441 reputation)SSC-Addicted (441 reputation)SSC-Addicted (441 reputation)

Group: General Forum Members
Points: 441 Visits: 1156
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

ByronOne
ByronOne
Hall of Fame
Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)

Group: General Forum Members
Points: 3298 Visits: 580
Thanks for the responses guys.

Just what I needed.

BO
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum








































































































































































SQLServerCentral


Search