views:

117

answers:

3

I have a PHP page running in postgres. I have 3 tables - workorders, *wo_parts* and part2vendor. I am trying to multiply 2 table column row datas together, ie *wo_parts* has a field called qty and part2vendor has a field called cost. These 2 are joined by *wo_parts.pn* and part2vendor.pn. I have created a query like this:

$scoreCostQuery = "SELECT SUM(part2vendor.cost*wo_parts.qty) as total_score 
                       FROM part2vendor 
                       INNER JOIN wo_parts 
                         ON (wo_parts.pn=part2vendor.pn)  
                       WHERE workorder=$workorder";

But if I add the costs of the parts multiplied by the qauntities supplied, it adds to a different number than what the script is doing. Help....I am new to this but if someone can show me in SQL I can modify it for postgres. Thanks

A: 

Hey.

You could try a sub-query instead.
(Note, I don't have a Postgres installation to test this on so consider this more like pseudo code than a working example... It does work in MySQL tho)

SELECT 
    SUM(p.`score`) AS 'total_score'
FROM part2vendor AS p2v
INNER JOIN (
    SELECT pn, cost * qty AS `score`
    FROM wo_parts
) AS p
    ON p.pn = p2v.pn
WHERE p2n.workorder=$workorder"
Atli
+2  A: 

Without seeing example data, there's no way for us to know why you're query totals are coming out differently that when you do the math by hand. It could be a bad join, so you are getting more/less records than you expected. It's also possible that your calculations are off. Pick an example with the smallest number of associated records & compare.

My suggestion is to add a GROUP BY to the query:

  SELECT SUM(p.cost * wp.qty) as total_score 
    FROM part2vendor p
    JOIN wo_parts wp ON wp.pn = p.pn 
   WHERE workorder = $workorder
GROUP BY workorder

FYI: MySQL was designed to allow flexibility in the GROUP BY, while no other db I've used does - it's a source of numerous questions on SO "why does this work in MySQL when it doesn't work on db x...".

To Check that your Quantities are correct:

SELECT wp.qty,
       p.cost
  FROM WO_PARTS wp
  JOIN PART2VENDOR p ON p.pn = wp.pn
 WHERE p.workorder = $workorder

Check that the numbers are correct for a given order.

OMG Ponies
I have tried this answer and it works. However the totals still don't add up. It works if there is only 1 part. I have the following. 1 x 3.91, 1 x 41.14, 1 x 29.95 and 4 x 19.36. This gives me 152.44. However, when the script uns, the output is 214.85. My code is
russell
***$scoreCostQuery = "SELECT SUM(p.cost*wp.qty) as total_score FROM part2vendor p JOIN wo_parts wp ON wp.pn = p.pn WHERE workorder = $workorderGROUP BY workorder";$sRow = pg_fetch_array(pg_query($scoreCostQuery));$cost = $sRow['total_score'];echo $cost;
russell
First thing to check is that you are getting accurate counts via SQL query.
OMG Ponies
How do I do that. Sorry but I am complete newbie at this whole thing. I have managed to get codes from the net so far.
russell
A: 

In the question, you say the cost column is in part2vendor, but in the query you reference wo_parts.cost. If the wo_parts table has its own cost column, that's the source of the problem.

outis
I see my error. I have changed this to part2vendor.cost where the cost of the part is. The script runs perfectly but the total it gives is not what I get with a calculator. I have tried this 4 times.
russell
Did you try my other suggestion about running the query without the sum and two extra columns?
outis