Monday, July 9, 2012

MySQL standard deviance and variance


MySQL standard deviance and variance 

For this tutorial ,I have created following table in my-database.

CREATE TABLE IF NOT EXISTS `vehicle` (
  `ID` int(30) NOT NULL AUTO_INCREMENT,
  `Type` varchar(40) NOT NULL,
  `Number` int(40) NOT NULL,
  PRIMARY KEY (`ID`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=9;

--
-- Dumping data for table `vehicle`
--

INSERT INTO `vehicle` (`ID`, `Type`, `Number`) VALUES
(1, 'Toyota', 8),
(2, 'Mercedes ', 9),
(3, 'Volkswagen ', 10),
(4, 'Volvo', 12),
(5, 'Audi', 6),
(6, 'Ford', 7),
(7, 'Mitsubishi', 8),
(8, 'Nissan', 9);
ID Type Number
1 Toyota 8
2 Mercedes 9
3 Volkswagen 10
4 Volvo 12
5 Audi 6
6 Ford 7
7 Mitsubishi 8
8 Nissan 9
Creating following php file you could get standard deviance and variance using the STD() and VARIANCE() functions

<?php
echo "<center><table bgcolor=#0b2d04>
<tr bgcolor=#327423>
<th><font color=#ffffff>ID</font></th>
<th><font color=#ffffff>Type</font></th>
<th><font color=#ffffff>Number</font></th>
</tr>";
$username="root";
$password="";
$database="my_database";
mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$query ="(SELECT * FROM vehicle)";
$result = mysql_query($query);

while ($row = mysql_fetch_assoc($result)){

echo "<tr  bgcolor=#d7eecb>

<td>" .$row['ID']."</td>
<td>" .$row['Type']."</td>
<td>" .$row['Number']."</td>
</tr>";
}
echo "</table></center>";
$query2="SELECT SUM(Number), COUNT(ID), STD(Number),
   VARIANCE(Number) FROM vehicle";
$result2 = mysql_query($query2);
mysql_close();
while ($row = mysql_fetch_assoc($result2)){
echo "<center>Sum of numbers:-".$row['SUM(Number)']."</center></br>";
echo "<center>standard deviance is:-".$row['STD(Number)']."</center></br>";
echo "<center>Variance is:-".$row['VARIANCE(Number)']."</center></br>";
echo "<center>Count is:-".$row['COUNT(ID)']."</center></br>";
}
?>
ID Type Number
1 Toyota 8
2 Mercedes 9
3 Volkswagen 10
4 Volvo 12
5 Audi 6
6 Ford 7
7 Mitsubishi 8
8 Nissan 9



Sum of numbers:-69




Standered Diviation is:-1.7275




Variance is:-2.9844




Count is:-8

No comments:

Post a Comment