Solved

MSSQL Query With SUM Function To Include Other Fields

Posted on 2013-01-10
8
634 Views
Last Modified: 2013-01-11
HELLO...

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

CustomerNumber
FirstName
LastName
PhoneNumber
ProdutSales
ServiceSales

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:
   FirstName
   LastName
   PhoneNumber
...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
0
Comment
Question by:datatechcorp
  • 2
  • 2
  • 2
  • +2
8 Comments
 
LVL 6

Expert Comment

by:Peter Kiprop
ID: 38766001
Try

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

Expert Comment

by:IrogSinta
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

0
 

Author Comment

by:datatechcorp
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?

Thanks!...Mark
0
 
LVL 29

Assisted Solution

by:IrogSinta
IrogSinta earned 167 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

0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 4

Assisted Solution

by:krtyknmsql
krtyknmsql earned 167 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

0
 
LVL 31

Accepted Solution

by:
awking00 earned 166 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;
0
 
LVL 31

Expert Comment

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

Author Closing Comment

by:datatechcorp
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 !!!
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Join & Write a Comment

Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

757 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

18 Experts available now in Live!

Get 1:1 Help Now