MS SQL Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsDatabasesMS SQL Development

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 March 30th, 2012, 07:51 AM
ncncnc ncncnc is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2012
Posts: 5 ncncnc User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 35 m 3 sec
Reputation Power: 0
PHP Query Multiplication help...

Hi guys,
I am currently building a website to deal with different product information and sales with php. I am using SQL to sort the database and pull out information.

The final thing i need to do is work out the total revenue of each product however the problem i am having is that the 'Price' column and 'SalesVolume' column are in two different tables and they need to be multiplied together.

The two tables and column headings are as follows:

Product
ID
Name
Price

MonthlySales
ID
ProductCode
Month
Year
SalesVolume

(ID and ProductCode are linked together in a relationship)

I cannot see anything wrong with the syntax in my query however I can't see the problem.

Here is the query I am using:

Code:
$describeQuery = "SELECT (Products.Price * MonthlySales.SalesVolume) as revenue From Products
INNER JOIN MonthlySales
ON
(Products.ID = MonthlySales.ProductCode)
GROUP BY Products.ID";



$results = sqlsrv_query($conn, $describeQuery);

echo '<table border="1" BORDERCOLOR=Black>';
echo '<tr><th bgcolor = "LightBlue">Name</th><th bgcolor = "LightBlue" >ID</th> <th bgcolor = "LightBlue" >Sales</th></th> <th bgcolor = "LightBlue" >Year</th> </tr>';


while($row = sqlsrv_fetch_array($results, SQLSRV_FETCH_ASSOC)) 
  {   
  echo '<tr>';
	echo '<td >' .$row['revenue'].'</td>'; 
	echo '</tr>';
	} 

echo '</table>';


sqlsrv_close($conn);


As you can see all I'm trying to do is print a list of revenues in a table. But the query fails.

Can anybody tell me what I'm doing wrong?

Reply With Quote
  #2  
Old March 30th, 2012, 08:12 AM
requinix's Avatar
requinix requinix is offline
Still alive
Click here for more information.
 
Join Date: Mar 2007
Location: Washington, USA
Posts: 12,696 requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)  Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1
Time spent in forums: 5 Months 1 Week 4 Days 4 h 42 m 26 sec
Reputation Power: 8969
Send a message via AIM to requinix Send a message via MSN to requinix Send a message via Yahoo to requinix Send a message via Google Talk to requinix
Aren't you missing a SUM?

Reply With Quote
  #3  
Old March 30th, 2012, 08:27 AM
ncncnc ncncnc is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2012
Posts: 5 ncncnc User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 35 m 3 sec
Reputation Power: 0
Quote:
Originally Posted by requinix
Aren't you missing a SUM?


Of course!

Thank you.

However now I have encountered a different problem. I'm also trying to pull the name column from my product table. However I'm not sure where to put it in the query.

I tried the below code, but it keeps failing.

Code:
$describeQuery = "SELECT Name, SUM(Products.Price * MonthlySales.SalesVolume) as revenue From Products
INNER JOIN MonthlySales
ON
(Products.ID = MonthlySales.ProductCode)
GROUP BY Products.ID";


Any ideas?

Reply With Quote
  #4  
Old March 30th, 2012, 09:33 AM
ncncnc ncncnc is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2012
Posts: 5 ncncnc User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 35 m 3 sec
Reputation Power: 0
UPDATE

So now I have the year filter figured out but accessing the other columns is proving more difficult.

Code:
$describeQuery = "SELECT Products.ID, Products.Name, Products.Price * SUM(MonthlySales.SalesVolume) AS revenue FROM Products p

INNER JOIN MonthlySales 
ON
(Products.ID = MonthlySales.ProductCode ) AND MonthlySales.Year = '$desiredYear' 
GROUP BY Products.ID ";

Reply With Quote
  #5  
Old March 30th, 2012, 09:37 AM
requinix's Avatar
requinix requinix is offline
Still alive
Click here for more information.
 
Join Date: Mar 2007
Location: Washington, USA
Posts: 12,696 requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)  Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1
Time spent in forums: 5 Months 1 Week 4 Days 4 h 42 m 26 sec
Reputation Power: 8969
Send a message via AIM to requinix Send a message via MSN to requinix Send a message via Yahoo to requinix Send a message via Google Talk to requinix
Moved to MS SQL, as my MySQL eyes don't see anything wrong.

How about putting the year condition in a normal WHERE?

[edit] Right, I missed the "year filter figured out" part...

Last edited by requinix : March 30th, 2012 at 11:04 AM.

Reply With Quote
  #6  
Old March 30th, 2012, 09:50 AM
ncncnc ncncnc is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2012
Posts: 5 ncncnc User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 35 m 3 sec
Reputation Power: 0
Quote:
Originally Posted by requinix
Moved to MS SQL, as my MySQL eyes don't see anything wrong.

How about putting the year condition in a normal WHERE?


The year filter is working fine, it's just trying to access the names from the product table that isn't working. I know it's something simple I just can't figure it out.

Reply With Quote
  #7  
Old March 30th, 2012, 09:54 AM
r937's Avatar
r937 r937 is offline
SQL Consultant
Click here for more information.
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 26,371 r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level) 
Time spent in forums: 3 Months 1 Week 2 Days 6 h 28 m 13 sec
Reputation Power: 4140
Quote:
Originally Posted by ncncnc
I know it's something simple I just can't figure it out.
the GROUP BY clause must include all non-aggregate columns mentioned in the SELECT clause

__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book

Reply With Quote
  #8  
Old March 30th, 2012, 10:03 AM
ncncnc ncncnc is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2012
Posts: 5 ncncnc User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 35 m 3 sec
Reputation Power: 0
Quote:
Originally Posted by r937
the GROUP BY clause must include all non-aggregate columns mentioned in the SELECT clause



Thanks a lot!

Problem Solved.

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesMS SQL Development > PHP Query Multiplication help...

Developer Shed Advertisers and Affiliates



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 | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap