Jump to content

summation of results


marmato

Recommended Posts

Hi guys,

I have a database called "Online Cars" containing a table called "status" which have 3 columns :source(text) ,destination (text) and status (int)
I wanna select a specific source and destination to get all the status entries for them and then get the mean and variance of these statuses
anyone can help me ?

 

my code :

 

<?php
$con = mysql_connect("localhost","root","");

$db = mysql_select_db("online cars");
$query = mysql_query("SELECT `Status` FROM `status` WHERE source = 'cairo' and destination = 'alex'");
while($row = mysql_fetch_assoc($query))
{
echo $row['Status']."<br>";

}
?>
.........................
the result is
1
2
5
4
3
4
i want to know how to get the summation and variance of these results

 

 

Link to comment
Share on other sites

try

SELECT s.source, s.destination, av.mean,
    AVG((s.status - av.mean)*(s.status - av.mean)) as variance
FROM status s
    INNER JOIN
        (
        SELECT source, destination, AVG(status) as mean
        FROM status
        GROUP BY source, destination
        ) as av
        USING (source, destination)
GROUP BY source, destination;
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.