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


Sum function where field =


Sum function where field =

Author
Message
masarzdenka
masarzdenka
Forum Newbie
Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)

Group: General Forum Members
Points: 5 Visits: 3
Hi,

I am trying to use sum function where a field falls into specific category, but so far no luck.

I have:

Wait_Status column: this can be "missing", "above target" and "within target"
Total: it is counted =Sum(Fields!Surgical_Case_Count.Value)

This Total gives me a number of cases per each wait_status.

What I need is to use the Sum function to get surgical_case_count where wait_status is "within target"...

Any ideas?

Thank you.
SQL Guy 1
SQL Guy  1
Ten Centuries
Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)

Group: General Forum Members
Points: 1340 Visits: 2506
You have just written what you need:

masarzdenka (5/7/2014)

Sum ... surgical_case_count where wait_status is "within target"...



select Sum(surgical_case_count)
where wait_status = 'within target'
masarzdenka
masarzdenka
Forum Newbie
Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)

Group: General Forum Members
Points: 5 Visits: 3
I am building the report using SQL report builder and "where" is not an option I thought. It doesn't work when I use: =Sum(Fields!Surgical_Case_Count.Value) where (Fields!Wait_Status.Value="Within Target")
masarzdenka
masarzdenka
Forum Newbie
Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)

Group: General Forum Members
Points: 5 Visits: 3
I think it needs to be done through lookup, so it looks where wait_status="within target" and takes a value from "surgical_case_count" (which is another column) and sums it up.

I just don't know how to write the expression...
david.bruckwicki
david.bruckwicki
SSC-Enthusiastic
SSC-Enthusiastic (113 reputation)SSC-Enthusiastic (113 reputation)SSC-Enthusiastic (113 reputation)SSC-Enthusiastic (113 reputation)SSC-Enthusiastic (113 reputation)SSC-Enthusiastic (113 reputation)SSC-Enthusiastic (113 reputation)SSC-Enthusiastic (113 reputation)

Group: General Forum Members
Points: 113 Visits: 675
Try using a case statement:

select
Total_within_target = sum(case when Wait_Status.Value="Within Target"
then Fields!Surgical_Case_Count.Value
else 0
end),
Total_missing = sum(case when Wait_Status.Value="Missing"
then Fields!Surgical_Case_Count.Value
else 0
end),
Total_above_target = sum(case when Wait_Status.Value="above_target"
then Fields!Surgical_Case_Count.Value
else 0
end)
from ...
David Burrows
David Burrows
SSCrazy Eights
SSCrazy Eights (9.5K reputation)SSCrazy Eights (9.5K reputation)SSCrazy Eights (9.5K reputation)SSCrazy Eights (9.5K reputation)SSCrazy Eights (9.5K reputation)SSCrazy Eights (9.5K reputation)SSCrazy Eights (9.5K reputation)SSCrazy Eights (9.5K reputation)

Group: General Forum Members
Points: 9517 Visits: 9740
Try
=Sum(IIf(Fields!Wait_Status.Value="Within Target",Fields!Surgical_Case_Count.Value,0))


Far away is close at hand in the images of elsewhere.

Anon.


masarzdenka
masarzdenka
Forum Newbie
Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)

Group: General Forum Members
Points: 5 Visits: 3
This one gives me an error:

=Sum(IIf(Fields!Wait_Status.Value="Within Target",Fields!Surgical_Case_Count.Value,0))

I think I've tried it before.
david.bruckwicki
david.bruckwicki
SSC-Enthusiastic
SSC-Enthusiastic (113 reputation)SSC-Enthusiastic (113 reputation)SSC-Enthusiastic (113 reputation)SSC-Enthusiastic (113 reputation)SSC-Enthusiastic (113 reputation)SSC-Enthusiastic (113 reputation)SSC-Enthusiastic (113 reputation)SSC-Enthusiastic (113 reputation)

Group: General Forum Members
Points: 113 Visits: 675
The IIf function is new to SQL Server 2012.
masarzdenka
masarzdenka
Forum Newbie
Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)

Group: General Forum Members
Points: 5 Visits: 3
David, I am using 2008
david.bruckwicki
david.bruckwicki
SSC-Enthusiastic
SSC-Enthusiastic (113 reputation)SSC-Enthusiastic (113 reputation)SSC-Enthusiastic (113 reputation)SSC-Enthusiastic (113 reputation)SSC-Enthusiastic (113 reputation)SSC-Enthusiastic (113 reputation)SSC-Enthusiastic (113 reputation)SSC-Enthusiastic (113 reputation)

Group: General Forum Members
Points: 113 Visits: 675
Then the case code I gave you should work. I've used similar code many times in 2008 R2. You may have to tweak on the syntax a little.
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