請問如何把下面資料做統計(我把資料庫的資料貼到excel呈現)
<?php
.
.
.
----上面部分是抓資料及輸入欄位----
echo "<table border='1'>";
echo "<tr>";
echo "<th>Month</th>";
echo "<th>Week</th>";
echo "<th>Day</th>";
echo "<th>ISSUE_QTY</th>";
echo "<th>IN_QTY</th>";
echo "<th>OUT_QTY</th>";
.
.
.
echo "</tr>";
foreach($row as $row1){
echo "<tr>";
foreach($row1 as $key => $value){
echo "<td>".$value."</td>";}
echo "</tr>";}
echo "</table>";
?>
這是我想做的顯示方式
Month 202105 Week 0 Day 0 Sum 818
Month 202104 Week 202217 Day 0 Sum 846
Month 202107 Week 202228 Day 20020704 Sum 297
Month 202107 Week 202228 Day 20020706 Sum 300
SQL
select 'Month'+Month+'Week'+Week+'DAY'+Day+'SUM'+CAST((SUM(cost1)+SUM(cost2)+SUM(cost3)) as varchar(10)) from #Test group by Month,Week,Day
PHP
'Month'.$Month.'Week'.$Week+....
抱歉,我可能沒有表達清楚,SQL裡面的資料已經都呈現出來了,現在是要另外把要統計的抓出來另外呈現在表格的下方(紅色圈圈)
不太清楚你的意思
意思是要用現有SQL傳進來的數組
用PHP統計完 呈現表格在網頁下方嗎?
CREATE TABLE #Test (Month varchar(10),Week varchar(10),Day varchar(10),cost1 int,cost2 int,cost3 int)
INSERT INTO #Test
values
('20210105','0','0',100,90,80),
('20210105','0','0',101,91,81),
('20210104','202117','0',102,92,82),
('20210104','202117','0',103,93,83),
('20210104','202118','0',104,94,84),
('20210104','202118','0',105,95,85);
select 'Month '+Month+' Week '+Week+' DAY '+Day+' SUM '+CAST((SUM(cost1)+SUM(cost2)+SUM(cost3)) as varchar(10)) from #Test group by Month,Week,Day
DROP TABLE #Test
多撈一次sql印出會不會比較簡單?
是撈下面表格顯示的資料,再依
Month 202105 Week 0 Day 0 Sum 818
Month 202104 Week 202217 Day 0 Sum 846
Month 202107 Week 202228 Day 20020704 Sum 297
Month 202107 Week 202228 Day 20020706 Sum 300
這樣的格式,顯示在表格下面
因為資料庫東西很多,表格內的內容是經過填值篩選之後出來的
所以想用動態的方法
依日期(年、月、日)算出總和(cost1+2+3)
然後在表格下面顯示出來(當然每次篩選的值不盡相同)
恩.. 那這跟SQL沒甚麼關呢
因為資料庫東西很多,表格內的內容是經過填值篩選之後出來的
雖然沒看過你的table 但相信我 這絕對不算多
關於加總的用法 ronrun 大已經提示過你了 我就不多做贅述
最後 作業加油
感謝你的回答
可以寫兩段SQL讓它跑
第一段去抓你的Month、Week、Day
SELECT Month,Week,Day FROM table1 GROUP BY Month,Week,Day
第二段將同個條件的Group起來加總
//例你的table叫table1
SELECT (SUM(cost1)+SUM(cost2)+SUM(cost3)) AS total FROM table1
WHERE Month='$m' AND Week='$w' AND Day='$d'
GROUP BY Month,Week,Day
之後就可以輸出你要的結果了
Month:202105 Week: 0 Day: 0 Sum: 819
Month:202204 Week: 202217 Day: 0 Sum: 846
Month:202204 Week: 202218 Day: 0 Sum: 873
Month:202207 Week: 202228 Day: 20220704 Sum: 297
Month:202207 Week: 202228 Day: 20220706 Sum: 300
SELECT `Month`, `Week`, `Day`, SUM(`cost1`+`cost2`+`cost3`) AS 'SUM' FROM `[table_name]` GROUP BY `Month`,`Week`,`Day`