Database Management
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsDatabasesDatabase Management

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old November 5th, 2003, 03:59 AM
shafan's Avatar
shafan shafan is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2002
Posts: 123 shafan User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 51 m 51 sec
Reputation Power: 7
Summing a Sum field

Hi,
I'm trying to Sum a field which is a Sum itself.
MySQL doesn't seems to like it.

Any ideas?

The SQL is (a bit complex):
SELECT Sum(ti.triQuantity) * i.ingQuantity, d.drnId, d.drnName
FROM tblIngredients as i
INNER JOIN tbldrinks AS d ON i.ingIngredientNum = d.drnId
INNER JOIN tblTransactionItems as ti ON d.drnId = ti.triDrinkId
INNER JOIN tblTransaction AS t ON ti.triTAId = t.trcId
WHERE i.ingDrinkNum IN (SELECT ingDrinkNum FROM tblIngredients WHERE
ingIngredientNum = 45)

Help would be appreciated.
Thanx

Reply With Quote
  #2  
Old November 5th, 2003, 07:05 AM
r937's Avatar
r937 r937 is offline
SQL Consultant
Click here for more information.
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 17,653 r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level) 
Time spent in forums: 1 Month 3 Weeks 6 Days 12 h 42 m 2 sec
Reputation Power: 981
add

GROUP BY i.ingQuantity, d.drnId, d.drnName

Reply With Quote
  #3  
Old November 5th, 2003, 07:49 AM
shafan's Avatar
shafan shafan is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2002
Posts: 123 shafan User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 51 m 51 sec
Reputation Power: 7
I'm afraid I've posted the wrong SQL. Sorry.
Here is the correct one:
SELECT Sum(ti.triQuantity) * i.ingQuantity as TotalQTY, d1.drnId, d1.drnName
FROM tblDrinks as d1
INNER JOIN tblIngredients as i ON d1.drnId = i.ingIngredientNum
INNER JOIN tbldrinks AS d ON i.ingDrinkNum = d.drnId
INNER JOIN tblTransactionItems as ti ON d.drnId = ti.triDrinkId
WHERE i.ingDrinkNum IN (48, 47)
GROUP BY d1.drnId, d.drnId

The problem is that the query returns sums of the same item, where as I want to get the total sum of that item (of all the occurrences)

Thanx

Reply With Quote
  #4  
Old November 5th, 2003, 08:56 AM
r937's Avatar
r937 r937 is offline
SQL Consultant
Click here for more information.
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 17,653 r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level) 
Time spent in forums: 1 Month 3 Weeks 6 Days 12 h 42 m 2 sec
Reputation Power: 981
keep looking at your groups

your GROUP BY list of columns must be all the non-aggregate columns in the SELECT list

thus, it must be --

GROUP BY i.ingQuantity, d1.drnId, d1.drnName

now, if that's the wrong grouping, then you have to address that

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesDatabase Management > Summing a Sum field


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway