Solved

tsql, multiplying two columns to create a new column

Posted on 2012-12-20
6
538 Views
Last Modified: 2012-12-20
I struggled through making this query.  Now I'm trying to create a new field by multiplying two fields but am not able to make it work.
This works:
SELECT Guest.RegistrationID2, Registration.RegistrationID2, StudentID, FirstName, LastName, PreferredName, StudentEmail, MailAddress1, MailAddress2, MailCity, MailState, MailZip, CommunicationPreference, IsActive, ApplicationIDPrimary, Sum(CPSTransaction.AuthorizationAmount) as Total_Paid, COUNT(Guest.GuestRegID) as Guests
FROM Registration Left Join CPSTransaction ON StudentID= ApplicationIDPrimary LEFT JOIN Guest ON Registration.RegistrationID2 = Guest.RegistrationID2
WHERE IsActive = 'True' AND (CommunicationPreference = 'Email') 
Group by Guest.GuestRegID, Guest.RegistrationID2, Registration.RegistrationID2, Registration.StudentID, CPSTransaction.AuthorizationAmount, FirstName, LastName, PreferredName, StudentEmail, MailAddress1, MailAddress2, MailCity, MailState, MailZip, CommunicationPreference, IsActive, ApplicationIDPrimary
Order by SUM(AuthorizationAmount), COUNT(Guest.GuestRegID)

Open in new window


When I add this line to the select statement, I break it.
SUM(CPSTransaction.AuthorizationAmount - ((55 * COUNT(Guest.GuestRegID) + 100))) as Total_Due

Error:
Cannot perform an aggregate function on an expression containing an aggregate or a subquery.


I would appreciate any help with this.
Thanks.
0
Comment
Question by:javierpdx
  • 2
  • 2
  • 2
6 Comments
 
LVL 61

Expert Comment

by:mbizup
ID: 38711411
What exactly are you trying to do?

Your question title mentions multiplying two columns, but the expression that you are having trouble with is not multiplying them.

>>> SUM(CPSTransaction.AuthorizationAmount - ((COUNT(Guest.GuestRegID) * 50) + 100)

Should the minus sign be a multiplication sign?


SUM(CPSTransaction.AuthorizationAmount)  * COUNT(Guest.GuestRegID) * 50 + 100
0
 
LVL 68

Expert Comment

by:Qlemo
ID: 38711420
You will have to transform the formula to use no aggregates in aggregates. Sum(count()) is the issue.
0
 
LVL 68

Expert Comment

by:Qlemo
ID: 38711438
The formula does not make sense to me. Arithemetic transformation leads to
  sum(CPSTransaction.AuthorizationAmount)
 + count(*)*100
 + count(Guest.GuestRegID)*count(*)*55
which doesn't look reasonable.
0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 

Author Comment

by:javierpdx
ID: 38711574
Sorry for the confusion.

I'm trying to multiple COUNT(Guest.GuestRegID) by 55, then add 100 to it.
Then subtract this value from Sum(CPSTransaction.AuthorizationAmount) to get the total still due.

Thanks.
0
 
LVL 61

Accepted Solution

by:
mbizup earned 500 total points
ID: 38711587
Try this:

Sum(CPSTransaction.AuthorizationAmount) - (COUNT(Guest.GuestRegID) * 55 + 100)

Open in new window


Your parentheses were originally in the wrong place.
0
 

Author Closing Comment

by:javierpdx
ID: 38711671
Thanks so much!  Worked great.

I will post a follow up question in a new thread.
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

In a multiple monitor setup, if you don't want to use AutoCenter to position your popup forms, you have a problem: where will they appear?  Sometimes you may have an additional problem: where the devil did they go?  If you last had a popup form open…
Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now