Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win


MSSQL Query With SUM Function To Include Other Fields

Posted on 2013-01-10
Medium Priority
Last Modified: 2013-01-11

I have a Table in MSSQL...call it "CustomerPurchases", that has the following fields:


I need to summarize the data...BUT...I *ALSO* need to display...the "other" (non-summarized) fields in the query's output.

I created a query like the following:

Select CustomerNumber, sum(ProductSales), sum(ServiceSales)
from CustomerPurchases
group by CustomerNumber

That works, certainly, but I need to display:
...on the SAME OUTPUT.  

When I attempt to add those (3) other fields, I get that "...is invalid in the select list because it is not contained in either an aggregate function..." error.  What is the correct syntax/method to use here?

Please help!...much appreciated!...Mark
Question by:datatechcorp
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
  • +2

Expert Comment

by:Peter Kiprop
ID: 38766001

Select CustomerNumber,FirstName,LastName,PhoneNumber, sum(ProductSales), sum(ServiceSales)
from CustomerPurchases
group by CustomerNumber,FirstName,LastName,PhoneNumber
LVL 29

Expert Comment

ID: 38766002
Try this:
Select CustomerNumber, FirstName, LastName, PhoneNumber, sum(ProductSales), sum(ServiceSales)
from CustomerPurchases
group by CustomerNumber, FirstName, LastName, PhoneNumber

Open in new window


Author Comment

ID: 38766050
Thank you both, IrogSinta, and Pthepebble, for responding.  When I do that, I get a bunch of NULLS fed back, which I know are not true.  Should I be looking to do a querry...and then a subquery with a join, perhaps?  Thoughts?

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

LVL 29

Assisted Solution

IrogSinta earned 668 total points
ID: 38766128
Would that be because some of your PhoneNumbers are null for the same customer in this CustomerPurchases table?  If so, you probably should do a join to a Customers table to get the FirstName, LastName, and PhoneNumber fields.  Something like this:

SELECT P.CustomerNumber, C.FirstName, C.LastName, C.PhoneNumber, Sum(P.ProductSales) AS SumOfProductSales, Sum(P.ServiceSales) AS SumOfServiceSales
FROM CustomerPurchases As P INNER JOIN Customers As C ON P.CustomerNumber = C.CustomerNumber
GROUP BY P.CustomerNumber, C.FirstName, C.LastName, C.PhoneNumber

Open in new window


Assisted Solution

krtyknmsql earned 668 total points
ID: 38766139
Try this and let me know

SELECT CustomerNumber, MAX(FirstName) FirstName, MAX(LastName) LastName, MAX(PhoneNumber) PhoneNumber, Sum(ProductSales) AS SumOfProductSales, Sum(ServiceSales) AS SumOfServiceSales
FROM CustomerPurchases 
GROUP BY CustomerNumber

Open in new window

LVL 32

Accepted Solution

awking00 earned 664 total points
ID: 38767365
select CustomerNumber, FirstName, LastName, PhoneNumber,
sum(ProductSales) over (partition by Customer order by CustomerNumber) as sum_prod_sales,
sum(ServiceSales) over (partition by Customer order by CustomerNumber) as sum_serv_sales
from CustomerPurchases;
LVL 32

Expert Comment

ID: 38767374
Sorry for the typo, should be ...(partition by CustomerNumber ...

Author Closing Comment

ID: 38767853
Thank you all SOOOOOO much!  I just *knew* it was (freakin' MSSQL) syntax!  All (3) solutions worked, so I'm being fair and awarding the points *asap* to y'all.  Again, very much appreciated!...Mark

P.S...EE is forcing me to choose a "best" solution...but, honestly, all (3) worked great...so please, take no offense anyone...I'm truly grateful !!!

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
One of the most important things in an application is the query performance. This article intends to give you good tips to improve the performance of your queries.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

610 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