Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

tsql, multiplying two columns to create a new column

Posted on 2012-12-20
6
541 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 69

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 69

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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 

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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

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

The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Phishing attempts can come in all forms, shapes and sizes. No matter how familiar you think you are with them, always remember to take extra precaution when opening an email with attachments or links.
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

790 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