Avatar of javierpdx
javierpdxFlag for United States of America

asked on 

tsql, multiplying two columns to create a new column

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.
Microsoft AccessC#SQL

Avatar of undefined
Last Comment
javierpdx
Avatar of mbizup
mbizup
Flag of Kazakhstan image

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
Avatar of Qlemo
Qlemo
Flag of Germany image

You will have to transform the formula to use no aggregates in aggregates. Sum(count()) is the issue.
Avatar of Qlemo
Qlemo
Flag of Germany image

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.
Avatar of javierpdx
javierpdx
Flag of United States of America image

ASKER

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.
ASKER CERTIFIED SOLUTION
Avatar of mbizup
mbizup
Flag of Kazakhstan image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of javierpdx
javierpdx
Flag of United States of America image

ASKER

Thanks so much!  Worked great.

I will post a follow up question in a new thread.
Microsoft Access
Microsoft Access

Microsoft Access is a rapid application development (RAD) relational database tool. Access can be used for both desktop and web-based applications, and uses VBA (Visual Basic for Applications) as its coding language.

226K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo