The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Databases
> MS SQL Development
|
PHP Query Multiplication help...
Discuss PHP Query Multiplication help... in the MS SQL Development forum on Dev Shed. PHP Query Multiplication help... MS SQL Development forum discussing administration, MS SQL queries, and other MS SQL-related topics. SQL Server is Microsoft's enterprise database engine.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

March 30th, 2012, 07:51 AM
|
|
Registered User
|
|
Join Date: Mar 2012
Posts: 5
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?
|

March 30th, 2012, 08:12 AM
|
 |
Still alive
|
|
Join Date: Mar 2007
Location: Washington, USA
|
|
|
Aren't you missing a SUM?
|

March 30th, 2012, 08:27 AM
|
|
Registered User
|
|
Join Date: Mar 2012
Posts: 5
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?
|

March 30th, 2012, 09:33 AM
|
|
Registered User
|
|
Join Date: Mar 2012
Posts: 5
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 ";
|

March 30th, 2012, 09:37 AM
|
 |
Still alive
|
|
Join Date: Mar 2007
Location: Washington, USA
|
|
|
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.
|

March 30th, 2012, 09:50 AM
|
|
Registered User
|
|
Join Date: Mar 2012
Posts: 5
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.
|

March 30th, 2012, 09:54 AM
|
 |
SQL Consultant
|
|
Join Date: Feb 2003
Location: Toronto Canada
|
|
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

|

March 30th, 2012, 10:03 AM
|
|
Registered User
|
|
Join Date: Mar 2012
Posts: 5
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.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|