Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Calculate Sum of an Expression field


Calculate Sum of an Expression field

Author
Message
sharonsql2013
sharonsql2013
SSC-Addicted
SSC-Addicted (406 reputation)SSC-Addicted (406 reputation)SSC-Addicted (406 reputation)SSC-Addicted (406 reputation)SSC-Addicted (406 reputation)SSC-Addicted (406 reputation)SSC-Addicted (406 reputation)SSC-Addicted (406 reputation)

Group: General Forum Members
Points: 406 Visits: 1220
Here is the situation :
Transaction Type Cr Amount Db Amount Crvalue DBValue
C     500         100     400
D            100    300           200


CrValue and DbValue are Calculated Fields so I have written the following expression:

Expression for Cr value
= switch (Field!Transactiontype.value )= “C”,Field!CrAmount.Value-Field!DbAmount.value

Experssion for DBvalue
= switch (Field!Transactiontype.value )= “D”,Field!DbAmount.Value-Field!CrAmount.value

Up until here , I am able to see the above data fine.
But ,now I need to get the total sum the Expression CrValue and DBValue.
How do I calculate the Sum of an Expression field.

Kindly advise
metralgia
metralgia
Grasshopper
Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)

Group: General Forum Members
Points: 22 Visits: 234
Wrapping your expression in a "sum" doesn't work? e.g. Sum(<expression>Wink
LinksUp
LinksUp
Right there with Babe
Right there with Babe (736 reputation)Right there with Babe (736 reputation)Right there with Babe (736 reputation)Right there with Babe (736 reputation)Right there with Babe (736 reputation)Right there with Babe (736 reputation)Right there with Babe (736 reputation)Right there with Babe (736 reputation)

Group: General Forum Members
Points: 736 Visits: 3975
I don't do anything in Reporting Services, so this could be totally off base, but the following works for me:


declare @t1 table
(
Trans_type char,
Credit int,
Debit int,
CR_Value as (case when Trans_Type = 'C' then credit-debit else 0 end),
DB_Value as (case when Trans_Type = 'D' then debit-credit else 0 end)
)

Insert @t1 (Trans_Type, Credit, Debit) values
('C', 500, 100),
('C', 100, 200),
('D', 100, 300),
('D', 500, 100)

select * from @t1

select Trans_type, SUM(CR_Value), SUM(DB_VALUE)
from @t1
group by Trans_type



Give this a shot in SSMS and hopefully the same principles can be applied for Reporting Services.

__________________________________________________________________________________________________________
How to Post to get the most: http://www.sqlservercentral.com/articles/Best+Practices/61537/
pietlinden
pietlinden
SSCrazy
SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)

Group: General Forum Members
Points: 2176 Visits: 12497
you could add a calculated value to your dataset that did the calculation "row-wise" and then use that calculated value in your SUM / aggregate expression
pietlinden
pietlinden
SSCrazy
SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)

Group: General Forum Members
Points: 2176 Visits: 12497
you could add a calculated value to your dataset that did the calculation "row-wise" and then use that calculated value in your SUM / aggregate expression
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