Jump to content

Trouble with SELECT


jblaha

Recommended Posts

I have data that looks like this:

 

userID Column1 Column2 Column3 Column4

1...........0..............7..............6..............5

2...........7..............4..............7..............6

1...........8..............0..............7..............7

1...........9..............9..............9..............0

2...........5..............5..............5..............5

 

I want one query that finds the average of each column where the userID =1, and only includes the values in the average of each column if they are >0. That means that if the userID is 1 that the average of Column1 should be 8.5 (excluded the zero in row 1) but the average of Column 3 should be 7.3333 (row 1 is included).

 

Right now I can get the average for each column including the zero rows, but when I start to add WHERE Column1 !=0 it excludes the entire row for the averages of other columns as well. The average for Column1 becomes 8.5 (excludes row1 properly) but the average for Column3 is 8.0 (excludes row1 also)

 

Here is the code I am using:

 

     $query = "SELECT avg(Column1),

                      avg(Column2),

                      avg(Column3),

                      avg(Column4),

                      avg(Column5),

                      avg(Column6)

              FROM table t

              WHERE t.userID = ".$clientrow["userid"]."

              AND t.Column1 !=0

              ORDER BY t.datemodified";



    if (!($result = @ mysql_query($query, $connection)))

       showerror();



      $ratingrow = mysql_fetch_array($result);

 

Is there a way to exclude rows selectively for each column inside one table to do what I want???

 

Thanks,

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.