sqlite 每年所有月份的年度总计

9fkzdhlc  于 2022-11-24  发布在  SQLite
关注(0)|答案(1)|浏览(165)

从我所掌握的情况来看,我需要今年的总数:

我使用了以下查询;

SELECT
    strftime('%Y', Timestamp) AS year,
    substr('JanFebMarAprMayJunJulAugSepOctNovDec', 1 + 3*strftime('%m', Timestamp), -3) AS month,
    SUM(snowDepth) AS depth
FROM DiaryData
GROUP BY year, month
ORDER BY year DESC

我有以下栏目:

Timestamp | entry | snowFalling | snowLying | snowDepth

在一个dataDiary表中。我需要得到每年的snowDepth总数,而不仅仅是每个月的总数。我的代码使用我已经有的查询,并将结果放在一个HTML表中:

while ($row = $result->fetch(PDO::FETCH_ASSOC)) { //fetch the query and put into array(s)
  $year                   = $row['year'];
  $month                  = $row['month'];
  $years[$year][$month][] = $row;
  //print_r($something);  -- used for debugging --
}

$totalSQLtime = (microtime(true) - $startSQLtime); // ends time for time it took to get query result(s)

$monthx = array('Jan', 'Feb', 'Mar', 'Apr', 'May',
 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec');

foreach ($years as $year => $months) {
 echo "<tr><td>" . $year . "</td>";

 foreach ($monthx as $month) {

  if (isset($months[$month])) {
   foreach ($months[$month] as $item) {
    if ($item['month'] === 'Jan') {
     echo "<td>" . $item['depth'] . "</td>";
    }

    if ($item['month'] === 'Feb') {
     echo "<td>" . $item['depth'] . "</td>";
    }

    if ($item['month'] === 'Mar') {
     echo "<td>" . $item['depth'] . "</td>";
    }

    if ($item['month'] === 'Apr') {
     echo "<td>" . $item['depth'] . "</td>";
    }

    if ($item['month'] === 'May') {
     echo "<td>" . $item['depth'] . "</td>";
    }

    if ($item['month'] === 'Jun') {
     echo "<td>" . $item['depth'] . "</td>";

    }if ($item['month'] === 'Jul') {
     echo "<td>" . $item['depth'] . "</td>";
    }

    if ($item['month'] === 'Aug') {
     echo "<td>" . $item['depth'] . "</td>";
    }

    if ($item['month'] === 'Sep') {
     echo "<td>" . $item['depth'] . "</td>";
    }

    if ($item['month'] === 'Oct') {
     echo "<td>" . $item['depth'] . "</td>";
    }

    if ($item['month'] === 'Nov') {
     echo "<td>" . $item['depth'] . "</td>";
    }

    if ($item['month'] === 'Dec') {
     echo "<td>" . $item['depth'] . "</td>";
    }

   }

  } else {echo "<td>" . $item['month'] = '0' . "</td>";}
 }
 echo '</tr>';
}

以下代码导致错误:
致命错误:未捕获的类型错误:不支持的操作数类型:整数+数组

while ($row = $result->fetch(PDO::FETCH_ASSOC)) { //fetch the query and put into array(s)
 $year                   = $row['year'];
 $month                  = $row['month'];
 $years[$year][$month][] = $row;
 //print_r($something);  -- used for debugging --
}
$totalSQLtime = (microtime(true) - $startSQLtime); // ends time for time it took to get query result(s)

$monthx = array('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec');

foreach ($years as $year => $months) {
 echo "<tr><td>" . $year . "</td>";

foreach ($monthx as $month) {

 if (isset($months[$month])) {
  foreach ($months[$month] as $item) {
   echo "<td>" . $item['depth'] . "</td>";
  }
 } else {echo "<td>" . $item['month'] = '0' . "</td>";}
}
  //var_dump("thisMonthCount");
  //echo '<pre>' . print_r($total, true) . '</pre>';
 
foreach ($years as $year => $months) {   // <-------- This is the part that's giving me difficulties ------------>
 $total = 0;
 foreach ($monthx as $monthShort) {
  $thisMonthCount = $months[$monthShort] ?? 0;           // Trying to sum each monthly snow amount for a yearly
  $total += (int) $thisMonthCount;                       // total and hav it placed in the proper row.
  echo "<td>" . $thisMonthCount . "</td>", PHP_EOL;
 }
 echo "<td>" . $total . "</td>";
}  // <--------------------------------------------  End of the part that's giving me difficulties ----------------->

var_dump($total);
 echo '</tr>';
}

// end of the code for displaying results in html table
echo '</tbody>';
echo '</table>';
echo '</div>';

?>
<!-- query time result displayed below the data table -->
<p style="font-size:0.9em; padding: 10px 0 0 0;">
        <?php
echo 'Queries took: ' . number_format($totalSQLtime, 6) . ' secs<br>' . PHP_EOL;
//End of page execution timer
$totaltime = (microtime(true) - $starttime);
echo 'Total gen time: ' . number_format($totaltime, 6) . ' secs'; ?>
</p>
js81xvg6

js81xvg61#

下面的代码还包含了空合并运算符??,用于查看源数组中是否有项,如果没有,福尔斯0

$data = [
    2022 => [
        'Jan' => 1,
        'Mar' => 2,
    ]
];

$allMonths = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];

foreach($data as $year => $months) {
    $total = 0;
    foreach($allMonths as $monthShort) {
        $thisMonthCount = $months[$monthShort] ?? 0;
        $total += $thisMonthCount;
        echo $thisMonthCount, PHP_EOL;
    }
    echo 'Total:' . $total;
}

输出量:

1
0
2
0
0
0
0
0
0
0
0
0
Total:3

在此处演示:https://3v4l.org/ahHonY

相关问题