Solved

MSSQL Query With SUM Function To Include Other Fields

Posted on 2013-01-10
8
649 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
[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
  • +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
Guide to Performance: Optimization & Monitoring

Nowadays, monitoring is a mixture of tools, systems, and codes—making it a very complex process. And with this complexity, comes variables for failure. Get DZone’s new Guide to Performance to learn how to proactively find these variables and solve them before a disruption occurs.

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

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 32

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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
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
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

729 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