Solved

tsql, multiplying two columns to create a new column

Posted on 2012-12-20
6
539 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
Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Familiarize people with the process of utilizing SQL Server stored procedures 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 Micr…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…

920 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

12 Experts available now in Live!

Get 1:1 Help Now