ตัวอย่างเช่น ผมมีข้อมูลการวัดค่าๆหนึ่งเป็นเวลาติดต่อกัน 2 วันที่บันทึกไว้ในฐานข้อมูล __________________________________________________________
l ID l........DATETIME (timestamp)....l........DATA(varchar)........l........x........l
-------------------------------------------------------------------------------------------------
l 1 l........2020-01-01 12:00:00........l...................1...................l................... l
l 2 l........2020-01-01 12:30:00........l...................5................... l...................l
l 3 l........2020-01-01 12:45:00........l...................7................... l................... l
l 4 l........2020-01-01 12:50:10........l...................13................... l...................l
l 5 l........2020-01-01 14:10:00........l ...................1................... l...................l
l 6 l........2020-01-01 17:05:30........l...................150................... l ...................l
l 7 l........2020-01-01 20:20:20........l...................56...................l................... l
l 8 l........2020-01-01 21:00:05........l...................34................... l...................l
l 9 l........2020-01-02 08:00:00........l...................78 ...................l...................l
l 10 l........2020-01-02 09:05:00........l...................123...................l................... l
l 11 l........2020-01-02 09:10:00........l ...................17...................l...................l
l 12 l........ 2020-01-02 11:12:50.......l...................178................... l...................l
l 13 l........2020-01-02 13:00:20........l...................90...................l ................... l
l 14 l........2020-01-02 18:40:50........l................... 33...................l...................l
l 15 l........2020-01-02 19:00:07........l...................1................... l................... l
l 16 l........2020-01-02 23:50:00........l...................10...................l...................l
-------------------------------------------------------------------------------------------------
จากตัวอย่างข้อมูลนี้จะเห็นว่าจะแบ่งออกเป็น 2 วัน และค่า x คือค่าที่ผมต้องการจากการคำนวน
โดยที่สูตรการคำนวณค่า X = [ SUM(DATA ในวันนั้นๆ) / 24 ] / max(DATA ในวันนั้นๆ) ดังนั้นค่า X ที่คำนวนได้ในแต่ละวันนั้นจะไม่เท่ากัน
SELECT tb.datetime, tb.data, ((t1.sumd/24)/t1.maxd) x FROM tb
LEFT JOIN (
SELECT DAY(datetime) days, SUM(data) sumd, MAX(data) maxd
FROM tb
GROUP BY days) t1
ON DAY(tb.datetime) = t1.days
Notice: Trying to get property 'num_rows' of non-object in /storage/ssd5/912/11562912/public_html/test1/loadcurve.php on line 20
Fatal error: Uncaught Error: Call to a member function fetch_array() on boolean in /storage/ssd5/912/11562912/public_html/test1/loadcurve.php:22 Stack trace: #0 /storage/ssd5/912/11562912/public_html/test1/index.php(27): include() #1 {main} thrown in /storage/ssd5/912/11562912/public_html/test1/loadcurve.php on line 22
Code (PHP)
<html>
<head><title></title>
</head>
<?php
$mysqli = new mysqli("localhost", "0000", "0000", "0000");
$i=1;
$q="
SELECT a.*,
(SELECT COUNT(b.DATE) FROM meter b WHERE b.DATE=a.DATE GROUP BY b.DATE) as num,
(SELECT SUM(b.M_A_KWH) FROM meter b WHERE b.DATE=a.DATE GROUP BY b.DATE) as TOTAL_A,
(SELECT MAX(b.M_A_WATT) FROM meter b WHERE b.DATE=a.DATE GROUP BY b.DATE) as MAX_A,
(SELECT SUM(b.M_B_KWH) FROM meter b WHERE b.DATE=a.DATE GROUP BY b.DATE) as TOTAL_B,
(SELECT MAX(b.M_B_WATT) FROM meter b WHERE b.DATE=a.DATE GROUP BY b.DATE) as MAX_B,
(SELECT SUM(b.M_C_KWH) FROM meter b WHERE b.DATE=a.DATE GROUP BY b.DATE) as TOTAL_C,
(SELECT MAX(b.M_C_WATT) FROM meter b WHERE b.DATE=a.DATE GROUP BY b.DATE) as MAX_C
FROM meter a ORDER BY a.DATE ASC
";
$result = $mysqli->query($q); // ทำการ query คำสั่ง sql
$total=$result->num_rows; // นับจำนวนถวที่แสดง ทั้งหมด
$aggr_arr=array();
while($row=$result->fetch_array()){ // วนลูปแสดงข้อมูล
$dateKey=date("dmY",strtotime($row['DATE']));
$row_span=0;
if(!isset($aggr_arr[$dateKey])){
$aggr_arr[$dateKey]=array();
$row_span=1;
}
?>
<body>
<table width="1300" border="1" cellpadding="10" cellspacing="0" align="center">
<tr>
<td rowspan="2" align="center" bgcolor="black" width="16%"><h7><font size="6" color="white">DATE DAY</td>
<td colspan="3" align="center" bgcolor="FFB4B9" width="28%"><h7>Phase : A</td>
<td colspan="3" align="center" bgcolor="5AD2FF" width="28%"><h7>Phase : B</td>
<td colspan="3" align="center" bgcolor="FFEB46" width="28%"><h7>Phase : C</td>
</tr>
<tr>
<td align="center" bgcolor="FFE6E6" width="9%">Total Energy per day [kWh]</td>
<td align="center" bgcolor="FFE6E6" width="9%">Maximum Load per day [W]</td>
<td align="center" bgcolor="FFE6E6">Load Factor per day</td>
<td align="center" bgcolor="C8FFFF" width="9%">Total Energy per day [kWh]</td>
<td align="center" bgcolor="C8FFFF" width="9%">Maximum Load per day [W]</td>
<td align="center" bgcolor="C8FFFF">Load Factor per day</td>
<td align="center" bgcolor="FAFAD2" width="9%">Total Energy per day [kWh]</td>
<td align="center" bgcolor="FAFAD2" width="9%">Maximum Load per day [W]</td>
<td align="center" bgcolor="FAFAD2">Load Factor per day</td>
</tr>
<tr>
<td align="center"><?=$row['DATE']?></td>
<?php if($row_span==1){?>
<td align="center" rowspan="<?=$row['num']?>"><?=$row['TOTAL_A']?></td>
<?php } ?>
<?php if($row_span==1){?>
<td align="center" rowspan="<?=$row['num']?>"><?=$row['MAX_A']?></td>
<?php } ?>
<td align="center">_________</td>
<?php if($row_span==1){?>
<td align="center" rowspan="<?=$row['num']?>"><?=$row['TOTAL_B']?></td>
<?php } ?>
<?php if($row_span==1){?>
<td align="center" rowspan="<?=$row['num']?>"><?=$row['MAX_B']?></td>
<?php } ?>
<td align="center">_________</td>
<?php if($row_span==1){?>
<td align="center" rowspan="<?=$row['num']?>"><?=$row['TOTAL_C']?></td>
<?php } ?>
<?php if($row_span==1){?>
<td align="center" rowspan="<?=$row['num']?>"><?=$row['MAX_C']?></td>
<?php } ?>
<td align="center">_________</td>
</tr>
<?php $i++; } ?>
</table>
</body>
<html>