0

我正在尝试从大型数据集中计算值的分布。这些值是 1 到 7 之间的 FLOAT,我想将它们聚合到一定的粒度。现在我正在汇总到一半值(1.0、1.5、2.0 等),但我希望能够更动态地设置粒度(例如到小数点后一位)。有这样做的聪明方法吗?现在我这样做

    //create distribution table
$distribution_table="CREATE TABLE distribution_table(Collective_ability FLOAT Primary Key, Number INT)";

// Execute query
if (mysql_query($distribution_table))
  {
  echo "Table distribution_table created successfully";
  }
else
  {
  echo "Error creating distribution table: " . mysql_error();
  die;
  }


//Calculate distribution of Collective Ability
$distribution = "INSERT INTO distribution_table (Collective_ability) Values (1.0),(1.5),(2.0),(2.5),(3.0),(3.5),(4.0),(4.5),(5.0),(5.5),(6.0),(6.5),(7.0)";

// Execute query
if (mysql_query($distribution))
  {
  }
else
  {
  echo "Error Selecting Inserting values into distribution table: " . mysql_error();
  die;
  }

$number_of_1 = "UPDATE distribution_table SET Number = (SELECT SUM(CASE WHEN CollectiveAbility > 0.75 AND CollectiveAbility < 1.25 THEN 1 ELSE 0 END) FROM collective_ability) WHERE Collective_ability = 1.0";
$number_of_1_5 = "UPDATE distribution_table SET Number = (SELECT SUM(CASE WHEN CollectiveAbility > 1.25 AND CollectiveAbility < 1.75 THEN 1 ELSE 0 END) FROM collective_ability) WHERE Collective_ability = 1.5";
$number_of_2 = "UPDATE distribution_table SET Number = (SELECT SUM(CASE WHEN CollectiveAbility > 1.75 AND CollectiveAbility < 2.25 THEN 1 ELSE 0 END) FROM collective_ability) WHERE Collective_ability = 2.0";
$number_of_2_5 = "UPDATE distribution_table SET Number = (SELECT SUM(CASE WHEN CollectiveAbility > 2.25 AND CollectiveAbility < 2.75 THEN 1 ELSE 0 END) FROM collective_ability) WHERE Collective_ability = 2.5";
$number_of_3 = "UPDATE distribution_table SET Number = (SELECT SUM(CASE WHEN CollectiveAbility > 2.75 AND CollectiveAbility < 3.25 THEN 1 ELSE 0 END) FROM collective_ability) WHERE Collective_ability = 3.0";
$number_of_3_5 = "UPDATE distribution_table SET Number = (SELECT SUM(CASE WHEN CollectiveAbility > 3.25 AND CollectiveAbility < 3.75 THEN 1 ELSE 0 END) FROM collective_ability) WHERE Collective_ability = 3.5";
$number_of_4 = "UPDATE distribution_table SET Number = (SELECT SUM(CASE WHEN CollectiveAbility > 3.75 AND CollectiveAbility < 4.25 THEN 1 ELSE 0 END) FROM collective_ability) WHERE Collective_ability = 4.0";
$number_of_4_5 = "UPDATE distribution_table SET Number = (SELECT SUM(CASE WHEN CollectiveAbility > 4.25 AND CollectiveAbility < 4.75 THEN 1 ELSE 0 END) FROM collective_ability) WHERE Collective_ability = 4.5";
$number_of_5 = "UPDATE distribution_table SET Number = (SELECT SUM(CASE WHEN CollectiveAbility > 4.75 AND CollectiveAbility < 5.25 THEN 1 ELSE 0 END) FROM collective_ability) WHERE Collective_ability = 5.0";
$number_of_5_5 = "UPDATE distribution_table SET Number = (SELECT SUM(CASE WHEN CollectiveAbility > 5.25 AND CollectiveAbility < 5.75 THEN 1 ELSE 0 END) FROM collective_ability) WHERE Collective_ability = 5.5";
$number_of_6 = "UPDATE distribution_table SET Number = (SELECT SUM(CASE WHEN CollectiveAbility > 5.75 AND CollectiveAbility < 6.25 THEN 1 ELSE 0 END) FROM collective_ability) WHERE Collective_ability = 6.0";
$number_of_6_5 = "UPDATE distribution_table SET Number = (SELECT SUM(CASE WHEN CollectiveAbility > 6.25 AND CollectiveAbility < 6.75 THEN 1 ELSE 0 END) FROM collective_ability) WHERE Collective_ability = 6.5";
$number_of_7 = "UPDATE distribution_table SET Number = (SELECT SUM(CASE WHEN CollectiveAbility > 6.75 AND CollectiveAbility < 7.25 THEN 1 ELSE 0 END) FROM collective_ability) WHERE Collective_ability = 7.0";

// Execute query
if (mysql_query($number_of_1))
  {
  }
else
  {
  echo "Error entering number count: " . mysql_error();
  die;
  }

// Execute query
if (mysql_query($number_of_1_5))
  {
  }
else
  {
  echo "Error entering number count: " . mysql_error();
  die;
  }

  // Execute query
if (mysql_query($number_of_2))
  {
  }
else
  {
  echo "Error entering number count: " . mysql_error();
  die;
  }

  // Execute query
if (mysql_query($number_of_2_5))
  {
  }
else
  {
  echo "Error entering number count: " . mysql_error();
  die;
  }

  // Execute query
if (mysql_query($number_of_3))
  {
  }
else
  {
  echo "Error entering number count: " . mysql_error();
  die;
  }
  // Execute query
if (mysql_query($number_of_3_5))
  {
  }
else
  {
  echo "Error entering number count: " . mysql_error();
  die;
  }
  // Execute query
if (mysql_query($number_of_4))
  {
  }
else
  {
  echo "Error entering number count: " . mysql_error();
  die;
  }
  // Execute query
if (mysql_query($number_of_4_5))
  {
  }
else
  {
  echo "Error entering number count: " . mysql_error();
  die;
  }
  // Execute query
if (mysql_query($number_of_5))
  {
  }
else
  {
  echo "Error entering number count: " . mysql_error();
  die;
  }
  // Execute query
if (mysql_query($number_of_5_5))
  {
  }
else
  {
  echo "Error entering number count: " . mysql_error();
  die;
  }
  // Execute query
if (mysql_query($number_of_6))
  {
  }
else
  {
  echo "Error entering number count: " . mysql_error();
  die;
  }
  // Execute query
if (mysql_query($number_of_6_5))
  {
  }
else
  {
  echo "Error entering number count: " . mysql_error();
  die;
  }
  // Execute query
if (mysql_query($number_of_7))
  {
  }
else
  {
  echo "Error entering number count: " . mysql_error();
  die;
  }
4

0 回答 0