?
Solved

tsql, multiplying two columns to create a new column

Posted on 2012-12-20
6
Medium Priority
?
544 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
[X]
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
  • 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 70

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 70

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
Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

 

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 2000 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

Get proactive database performance tuning online

At Percona’s web store you can order full Percona Database Performance Audit in minutes. Find out the health of your database, and how to improve it. Pay online with a credit card. Improve your database performance now!

Question has a verified solution.

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

AutoNumbers should increment automatically, without duplicates.  But sometimes something goes wrong, and the next AutoNumber value is a duplicate.  This article shows how to recover from this problem.
It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.
Suggested Courses

770 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