473,408 Members | 1,746 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

How can I sum the Case Statement?

working in VS 2005 Version 8
sql server 2005


Here is my select statement:

Expand|Select|Wrap|Line Numbers
  1. SELECT     Agency, Type, CompanyNumber, AgencyDirect, PolicyNumber, Date, KeyTranCode, Time, UpdateTime, InstTime, CustomerNumber, PolicySeq, 
  2.                       TranCode, UpdateDate, EffDate, LobCode, GrossPrem, SplitPrem, CurrComm, PrevComm, SplitComm, ProdSplit, PayableSelected, PayableFromInstall, 
  3.                       BusinessType, AcctType, CommMarked, CommReconciled, BrokerNumber, CommSW, 
  4.                       CASE AcctType WHEN 'C' THEN CASE AgencyDirect WHEN 'D' THEN CommMarked ELSE CurrComm END WHEN 'A' THEN (CurrComm - PrevComm) 
  5.                       END AS CommTotals
  6. FROM         Payables
  7. WHERE     (PayableSelected = 1) AND (Type = 'C')
  8. GROUP BY Agency, Type, CompanyNumber, AgencyDirect, PolicyNumber, Date, KeyTranCode, Time, UpdateTime, InstTime, CustomerNumber, PolicySeq, 
  9.                       TranCode, UpdateDate, EffDate, LobCode, GrossPrem, SplitPrem, CurrComm, PrevComm, SplitComm, ProdSplit, PayableSelected, PayableFromInstall, 
  10.                       BusinessType, AcctType, CommMarked, CommReconciled, BrokerNumber, CommSW
I would like to get a sum of the Case statement into another column, don't really care if it shows up multiple times.
Is it even possible to get the sum that I want through sql?
Jan 18 '07 #1
6 16785
iburyak
1,017 Expert 512MB
Not sure if I understood your problem correctly but it should be as simple as

[PHP]SELECT Agency, Type, CompanyNumber, AgencyDirect, PolicyNumber, Date, KeyTranCode, Time,
UpdateTime, InstTime, CustomerNumber, PolicySeq,
TranCode, UpdateDate, EffDate, LobCode, GrossPrem, SplitPrem,
CurrComm, PrevComm, SplitComm, ProdSplit, PayableSelected, PayableFromInstall,
BusinessType, AcctType, CommMarked, CommReconciled, BrokerNumber, CommSW,
sum(CASE AcctType WHEN 'C' THEN
CASE AgencyDirect WHEN 'D' THEN CommMarked ELSE CurrComm END
WHEN 'A' THEN (CurrComm - PrevComm)
END) AS CommTotals
FROM Payables
WHERE (PayableSelected = 1) AND (Type = 'C')
GROUP BY Agency, Type, CompanyNumber, AgencyDirect, PolicyNumber, Date, KeyTranCode, Time,
UpdateTime, InstTime, CustomerNumber, PolicySeq,
TranCode, UpdateDate, EffDate, LobCode, GrossPrem, SplitPrem, CurrComm, PrevComm, SplitComm,
ProdSplit, PayableSelected, PayableFromInstall,
BusinessType, AcctType, CommMarked, CommReconciled, BrokerNumber, CommSW [/PHP]
Jan 18 '07 #2
I may not have been clear I would like to get a sum of CommTotals column and put that into another column. I have tried summing the case statement the way
- iburyak - suggested but that will give me a total on the returned row. I basically would like to have SUM(CommTotals) but I get an invalid column error.

hope this helps for anyone else trying to figure it out
and thanks for any help
Jan 18 '07 #3
almaz
168 Expert 100+
Your GROUP BY clause includes all fields, so the result of SUM() function will be the same as its argument. If you want to sum up CommTotals values based on some particular grouping, please specify it. Or maybe you want the select statement to have a grand total of CommTotals specified for each row? Please provide sample values, actual results and expected results.
Jan 19 '07 #4
In my original post I had included the Group By from when I had tried to run the statement with a SUM() of the Case statement as had been previously suggested this is the original Select statement that I was trying to run:

Expand|Select|Wrap|Line Numbers
  1. SELECT     Agency, Type, CompanyNumber, AgencyDirect, PolicyNumber, Date, KeyTranCode, Time, UpdateTime, InstTime, CustomerNumber, PolicySeq, 
  2.                       TranCode, UpdateDate, EffDate, LobCode, GrossPrem, SplitPrem, CurrComm, PrevComm, SplitComm, ProdSplit, PayableSelected, PayableFromInstall, 
  3.                       BusinessType, AcctType, CommMarked, CommReconciled, BrokerNumber, CommSW, 
  4.                       CASE AcctType WHEN 'C' THEN CASE AgencyDirect WHEN 'D' THEN CommMarked ELSE CurrComm END WHEN 'A' THEN (CurrComm - PrevComm) 
  5.                       END AS CommTotals
  6. FROM         Payables
  7. WHERE     (PayableSelected = 1) AND (Type = 'C')
  8.  
What i want to accomplish is a grand total of the CommTotals column.
ie:
CommTotals GrandTotals
40.00
34.00
-25.00
20.00
-100.00 -31.00
Jan 19 '07 #5
almaz
168 Expert 100+
Still not sure if I understood you well. Can you try this one?
Expand|Select|Wrap|Line Numbers
  1. ;WITH Source (Agency, Type, CompanyNumber, AgencyDirect, PolicyNumber, Date, KeyTranCode, Time, UpdateTime, InstTime, CustomerNumber, PolicySeq, 
  2.                       TranCode, UpdateDate, EffDate, LobCode, GrossPrem, SplitPrem, CurrComm, PrevComm, SplitComm, ProdSplit, PayableSelected, PayableFromInstall, 
  3.                       BusinessType, AcctType, CommMarked, CommReconciled, BrokerNumber, CommSW, CommTotals)
  4. AS
  5. (SELECT     Agency, Type, CompanyNumber, AgencyDirect, PolicyNumber, Date, KeyTranCode, Time, UpdateTime, InstTime, CustomerNumber, PolicySeq, 
  6.                       TranCode, UpdateDate, EffDate, LobCode, GrossPrem, SplitPrem, CurrComm, PrevComm, SplitComm, ProdSplit, PayableSelected, PayableFromInstall, 
  7.                       BusinessType, AcctType, CommMarked, CommReconciled, BrokerNumber, CommSW, 
  8.                       CASE AcctType WHEN 'C' THEN CASE AgencyDirect WHEN 'D' THEN CommMarked ELSE CurrComm END WHEN 'A' THEN (CurrComm - PrevComm) 
  9.                       END AS CommTotals
  10. FROM         Payables
  11. WHERE     (PayableSelected = 1) AND (Type = 'C')
  12. )
  13. select *, GrandTotal = (select sum(CommTotals) from Source) from Source
Jan 19 '07 #6
Awesome works perfectly! thanks almaz
we learn something new everyday
Jan 19 '07 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Tom | last post by:
I would like to know if an .asp case statement can contain HTML elements. I am building an application that I would like to have dynamic choices. The dynamic part would be built in the a case...
1
by: mirth | last post by:
I would like to update a decimal column in a temporary table based on a set of Glcodes from another table. I search for a set of codes and then want to sum the value for each row matching the...
6
by: ryan.mclean | last post by:
Hi all, first, let me preface this by saying that I am very new to sql server, coming from oracle. Here is my problem: I would like to have a case statement (similar to decode in oracle) that...
21
by: Andy | last post by:
Can someone tell me if the following Switch...Case construct is valid? I'm wanting to check for multiple values in the Case statement without explicitly listing each values. So for example, will...
6
by: deanfamily11 | last post by:
I've set up a case statement to have my program determine where on the Cartesian plane a point the user enters is located. I keep getting the C2051 error when I compile. Any help? #include...
2
by: Mark Mullins | last post by:
have code below: Function ClassColl(strClass As Variant, strColl As Variant) As String ' Comments : ' Parameters : strClass ' strColl ' Returns : String Description '...
12
by: rAinDeEr | last post by:
Hi, I have a table with 2 columns ** CREATE TABLE test (emp_num DECIMAL(7) NOT NULL,emp_name CHAR(10) NOT NULL) and i have inserted a number of records. ** Now, I want to insert a new...
1
by: microsoft.public.dotnet.languages.vb | last post by:
Hi All, I wanted to know whether this is possible to use multiple variables to use in the select case statement such as follows: select case dWarrExpDateMonth, dRetailDateMonth case...
7
by: sam_cit | last post by:
Hi Everyone, In the following code, i have a common action for three switch cases, is there any other better way to write the three values in a single case? Thanks in advance #include...
13
by: Satya | last post by:
Hi everyone, This is the first time iam posting excuse me if iam making any mistake. My question is iam using a switch case statement in which i have around 100 case statements to compare. so...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.