Forum Moderators: open

Message Too Old, No Replies

Sum Query returning duplicate output

mysql

         

dougmcc1

7:51 pm on May 25, 2006 (gmt 0)

10+ Year Member



I'm trying to run a query that sums up data for a certain value. It's summing up the data correctly but it's returning duplicate output because the value exists in the table more than once. I tried using distinctrow but got this error message:

Mixing of GROUP columns (MIN(),MAX(),COUNT(),...) with no GROUP columns is illegal if there is no GROUP BY clause

Here's the initial query that returns duplicates:

SELECT SUM(field1) as sales,SUM(field2) as cost, AVG(field3) as avgLength FROM table WHERE field4='$field4'

Here's some sample data:
field1 field2 field3 field4
100 10 5 something
110 15 3 something
50 5 5 somethingelse

Here's some sample output:

something 210 25 8
something 210 25 8
something else 50 5 5

ChadSEO

4:41 pm on May 26, 2006 (gmt 0)

10+ Year Member



dougmcc1,

On the sample output, it looks like you have field4, which is not being returned in your example query. If you do need to have it returned, you'd want to do something like this:

SELECT field4, SUM(field1) as sales, sum(field2) as cost, avg(field3) as avgLength FROM table WHERE field4='something' GROUP BY field4;

Chad