HighTechTalks DotNet Forums  

Question about SUM query

Dotnet Framework (ADO.net) microsoft.public.dotnet.framework.adonet


Discuss Question about SUM query in the Dotnet Framework (ADO.net) forum.



Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old   
Cirene
 
Posts: n/a

Default Question about SUM query - 10-22-2007 , 12:55 PM






I have a "transaction" table with 2 columns, TransTypeId and TransAmt.
Example:

TransTypeId TransAmt
A 100
A 200
B 100
C 50
B 100
A 100

I am trying to do a SUM of all the TransTypeId A and B. The result should
be 600. I WANT TO GET ONLY THIS 1 VALUE BACK.

But, the result of the query is returning...
A 400
B 200

It's not summing it as a "group", but per "TransTypeId". Any ideas why?

Here's the raw query... (
SELECT SUM(Transactions.TransAmt) AS Expr1, Enroll.ProgramId,
Enroll.EnrollId
FROM Transactions INNER JOIN
Enroll ON Transactions.EnrollId = Enroll.EnrollId
GROUP BY Enroll.ProgramId, Enroll.EnrollId, Transactions.TransTypeId
HAVING (Enroll.ProgramId = @ProgramId) AND (Enroll.EnrollId =
@EnrollId) AND (Transactions.TransTypeId = 1 OR
Transactions.TransTypeId = 2 OR
Transactions.TransTypeId = 3)

Thanks!



Reply With Quote
  #2  
Old   
Miha Markic
 
Posts: n/a

Default Re: Question about SUM query - 10-22-2007 , 01:48 PM






I think this select would do the trick:
Select SUM(TransAmt) FROM Transactions where TransTypeId IN ('A', 'B')

--
Miha Markic [MVP C#, INETA Country Leader for Slovenia]
RightHand .NET consulting & development www.rthand.com
Blog: http://cs.rthand.com/blogs/blog_with_righthand/

"Cirene" <invalid_email (AT) zzz (DOT) com> wrote

Quote:
I have a "transaction" table with 2 columns, TransTypeId and TransAmt.
Example:

TransTypeId TransAmt
A 100
A 200
B 100
C 50
B 100
A 100

I am trying to do a SUM of all the TransTypeId A and B. The result should
be 600. I WANT TO GET ONLY THIS 1 VALUE BACK.

But, the result of the query is returning...
A 400
B 200

It's not summing it as a "group", but per "TransTypeId". Any ideas why?

Here's the raw query... (
SELECT SUM(Transactions.TransAmt) AS Expr1, Enroll.ProgramId,
Enroll.EnrollId
FROM Transactions INNER JOIN
Enroll ON Transactions.EnrollId = Enroll.EnrollId
GROUP BY Enroll.ProgramId, Enroll.EnrollId, Transactions.TransTypeId
HAVING (Enroll.ProgramId = @ProgramId) AND (Enroll.EnrollId =
@EnrollId) AND (Transactions.TransTypeId = 1 OR
Transactions.TransTypeId = 2 OR
Transactions.TransTypeId = 3)

Thanks!



Reply With Quote
  #3  
Old   
Cirene
 
Posts: n/a

Default Re: Question about SUM query - 10-22-2007 , 03:11 PM



Thanks Miha...

"Miha Markic" <miha at rthand com> wrote

Quote:
I think this select would do the trick:
Select SUM(TransAmt) FROM Transactions where TransTypeId IN ('A', 'B')

--
Miha Markic [MVP C#, INETA Country Leader for Slovenia]
RightHand .NET consulting & development www.rthand.com
Blog: http://cs.rthand.com/blogs/blog_with_righthand/

"Cirene" <invalid_email (AT) zzz (DOT) com> wrote in message
news:Ojr9YxMFIHA.1184 (AT) TK2MSFTNGP04 (DOT) phx.gbl...
I have a "transaction" table with 2 columns, TransTypeId and TransAmt.
Example:

TransTypeId TransAmt
A 100
A 200
B 100
C 50
B 100
A 100

I am trying to do a SUM of all the TransTypeId A and B. The result
should be 600. I WANT TO GET ONLY THIS 1 VALUE BACK.

But, the result of the query is returning...
A 400
B 200

It's not summing it as a "group", but per "TransTypeId". Any ideas why?

Here's the raw query... (
SELECT SUM(Transactions.TransAmt) AS Expr1, Enroll.ProgramId,
Enroll.EnrollId
FROM Transactions INNER JOIN
Enroll ON Transactions.EnrollId = Enroll.EnrollId
GROUP BY Enroll.ProgramId, Enroll.EnrollId, Transactions.TransTypeId
HAVING (Enroll.ProgramId = @ProgramId) AND (Enroll.EnrollId =
@EnrollId) AND (Transactions.TransTypeId = 1 OR
Transactions.TransTypeId = 2 OR
Transactions.TransTypeId = 3)

Thanks!





Reply With Quote
Reply




Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.