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
Solved

SQL sum INNER JOIN column

Posted on 2014-11-14
7
226 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 69

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 48

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
Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 

Author Closing Comment

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

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 69

Expert Comment

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

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
sql help 8 58
Addition to SQL for dynamic fields 6 47
Setting Macro in Access to Automate Running an Append at a Certain Time 2 28
T-SQL Default value in Select? 5 37
by Mark Wills Attending one of Rob Farley's seminars the other day, I heard the phrase "The Accidental DBA" and fell in love with it. It got me thinking about the plight of the newcomer to SQL Server...  So if you are the accidental DBA, or, simp…
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

840 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