簡體   English   中英

我如何計算一個月的平均總數

[英]How do i calculate the average total of a month

嗨,我在下面有表格daily_report顯示列

report_id | peak_generation | pdate | plant_id

每行代表特定工廠產生的總能量(每天為7棵工廠)。 我有1年(2014年1月12日)的數據分布表。 我需要查詢以繪制每月的總“平均”峰值生成量。 我使用融合圖能夠繪制的是每月的總峰值生成量(即,總的峰值生成Y軸相對於月份的X軸)。 如何獲得每月總工廠的平均值? 下面是代碼的清晰性。

<?php
//We've included ../Includes/FusionCharts.php and ../Includes/DBConn.php,     which contains
//functions to help us easily embed the charts and connect to a database.
include("Includes/FusionCharts.php");
include("Includes/DBConn.php");
?>
<HTML>
<HEAD>
<TITLE>
FusionCharts Free - Database Example
</TITLE>
<?php
//You need to include the following JS file, if you intend to embed the chart using JavaScript.
//Embedding using JavaScripts avoids the "Click to Activate..." issue in Internet Explorer
//When you make your own charts, make sure that the path to this JS file is   correct. Else, you would get JavaScript errors.
?>  
<SCRIPT LANGUAGE="Javascript" SRC="js/FusionCharts.js"></SCRIPT>
<style type="text/css">
<!--
body {
    font-family: Arial, Helvetica, sans-serif;
    font-size: 12px;
}
.text{
    font-family: Arial, Helvetica, sans-serif;
    font-size: 12px;
}
-->
</style>
</HEAD>
<BODY>


<?php
//In this example, we show how to connect FusionCharts to a database.
//For the sake of ease, we've used an MySQL databases containing two
//tables.

// Connect to the DB
$link = connectToDB();


// Fetch all factory records
//$strQuery = "select * from daily_output";
//$result = mysql_query($strQuery) or die(mysql_error());


//Iterate through each factory
//if ($result) {
    //
        //Now create a second query to get details for this factory
        $strQuery = "select SUM(peak_generation) as TotOutput, MONTHNAME (pdate) as MONTH FROM daily_report WHERE pdate BETWEEN '2015-01-01' AND '2015-12-30' GROUP BY MONTH (pdate)";
        $result = mysql_query($strQuery) or die(mysql_error()); 

        $strXML = "<graph caption='Total Peak Electricity Generated ---- 2015 Month by Month' numberSuffix='MW' xAxisName='Month' yAxisName='MegaWatts' decimalPrecision='0' formatNumberScale='0' yaxismaxvalue='5000'>";


        while($row = mysql_fetch_array($result)) {
        //Generate <set name='..' value='..' />        
        $strXML .= "<set name = '".$row['MONTH']."' value = '".$row['TotOutput']."' />";

        //$strXML .="<set name ='" . datePart("d",$row['DAYS']) . "/" . datePart("m",$row['DAYS']) . "' value='" . $row['TotOutput'] . "'/>"; 
        }
        //Finally, close <graph> element
        $strXML .= "</graph>";
        //Create the chart - Pie 3D Chart with data from $strXML
        //echo renderChart("charts/FCF_Column3D.swf", "", $strXML, "FactorySum", 650, 450);
        echo renderChart("charts/FCF_Column3D.swf", "", $strXML, "annual_revenue", 1200, 500);


        //free the resultset
        mysql_free_result($result);


    mysql_close($link);
?>




<BR><BR>
<a href='../NoChart.html' target="_blank">Unable to see the chart above?</a>
<H5 ><a href='../default.htm'>&laquo; Back to list of examples</a></h5>


<CENTER></CENTER>
</BODY>
</HTML>

嘗試group by month使用mysql的AVG()group by month您應該會得到輸出。

您可以在此處查看教程: 此處

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM