Solved

SQL sum INNER JOIN column

Posted on 2014-11-14
7
210 Views
Last Modified: 2014-11-19
Consider the following:

tblEntity
entityid int pk
entityname varchar
userid varchar

tblProducts
productid int pk
productDesc varchar
productPrice decimal

tblEntityProducts
epid int pk
entityid int fk
productid int fk

Open in new window


tblEntity
1 Famco bill
2 HatWagons joe
3 Stinkco bill

tblProducts
1 Corn 3.00
2 Hamster 4.00
3 Microscope 10.00

tblEntityProducts
1 1 1
2 1 2
3 2 2
4 2 3
5 3 3

Open in new window


Based on input of userid bill, I am looking for a query to get expected result of

Famco bill 7.00
Stinkco bill 10.00

Open in new window


I can results of
Famco bill 3.00
Famco bill 4.00
Stinkco bill 10.00

Open in new window


Using code

SELECT     e.entityid, e.userid, p.productPrice, z.productid
FROM         tblEntity AS e INNER JOIN
                      tblEntityProducts AS z ON z.entityid = e.entityid INNER JOIN
                      tblProducts AS p ON z.productid = p.productid
WHERE     (e.userid = 'bill')

Open in new window


But if I try to add any GROUP BY or SUM functions, it throws "Column is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause."

Any ideas?  Can provide further info if needed.  Thank you
0
Comment
Question by:alright
7 Comments
 
LVL 52

Accepted Solution

by:
_agx_ earned 310 total points
ID: 40444047
Don't include the other columns. If you want to SUM the price by entityName, only include those two columns:


SELECT  e.entityname, SUM(p.ProductPrice) AS TotalPrice
FROM       tblEntity e 
		INNER JOIN tblEntityProducts z ON z.entityid = e.entityid 
                INNER JOIN tblProducts p ON z.productid = p.productid
WHERE     (e.userid = 'bill')
GROUP BY e.entityname 

Open in new window

0
 
LVL 68

Assisted Solution

by:Qlemo
Qlemo earned 95 total points
ID: 40444467
As you also want to see the userid:
SELECT  e.entityID, e.userid, SUM(p.ProductPrice) AS TotalPrice
FROM       tblEntity e 
		INNER JOIN tblEntityProducts z ON z.entityid = e.entityid 
                INNER JOIN tblProducts p ON z.productid = p.productid
WHERE     (e.userid = 'bill')
GROUP BY e.entityID, e.userid

Open in new window

But using entityname instead of entitiyID, as _agx_ did, sounds more reasonable.
0
 
LVL 45

Assisted Solution

by:Vitor Montalvão
Vitor Montalvão earned 95 total points
ID: 40446765
For what you want the following query is the solution:
SELECT  e.entityname, e.userid, SUM(p.ProductPrice) AS TotalPrice
FROM       tblEntity e 
		INNER JOIN tblEntityProducts z ON z.entityid = e.entityid 
                INNER JOIN tblProducts p ON z.productid = p.productid
WHERE     (e.userid = 'bill')
GROUP BY e.entityname, e.userid

Open in new window

The explanation for that error is in SQL Server if you want to use an aggregate function (MAX, MIN, SUM, COUNT) you will always need to create a GROUP BY clause with the rest of the fields that your SELECT is returning with the exception of the aggregate functions.
0
Get up to 2TB FREE CLOUD per backup license!

An exclusive Black Friday offer just for Expert Exchange audience! Buy any of our top-rated backup solutions & get up to 2TB free cloud per system! Perform local & cloud backup in the same step, and restore instantly—anytime, anywhere. Grab this deal now before it disappears!

 

Author Closing Comment

by:alright
ID: 40451192
Thank you for the assistance and the explanation.  Makes perfect sense now
0
 
LVL 68

Expert Comment

by:Qlemo
ID: 40451259
You did take notice of the fact that _agx_'s suggestion does not return the desired result as requested?
0
 
LVL 52

Expert Comment

by:_agx_
ID: 40451357
@Qlemo - Hah, you're right. All this time I thought the sample data value "Famco bill" was the entityName - not entityName + userID.  I couldn't figure out why you added userID to your query ;-)
0
 
LVL 68

Expert Comment

by:Qlemo
ID: 40451941
Yes, that kind of display is obfuscating.
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Introduction Hopefully the following mnemonic and, ultimately, the acronym it represents is common place to all those reading: Please Excuse My Dear Aunt Sally (PEMDAS). Briefly, though, PEMDAS is used to signify the order of operations (http://en.…
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
This video discusses moving either the default database or any database to a new volume.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

760 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

21 Experts available now in Live!

Get 1:1 Help Now