 |
ต้องการหาค่า % เปรียบเทียบเดือนเดียวกัน เช่น $ผลรวม=ปีปัจุบัน-ปีก่อนหน้า*100/ปีก่อนหน้า |
|
 |
|
|
 |
 |
|
Code (SQL)
select cur_y, count_cur, count_old, (count_cur - count_old) / 100 * count_old as total
from (
select cur_y, count(tb1.field) as count_cur, count(tb2.field) as count_old
from (select year(fielddate) as cur_y, (year(fielddate)-1) as old_y from tablename group by year(fielddate)) as yy
left join tablename tb1 on year(tb1.fielddate) = yy.cur_y
left join tablename tb2 on year(tb2.fielddate) = yy.old_y
group by cur_y
) as tmp
order by cur_y
|
ประวัติการแก้ไข 2018-04-10 18:39:10
 |
 |
 |
 |
Date :
2018-04-10 18:38:13 |
By :
Chaidhanan |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ยังไม่ผ่านเลยคะ รบกวนแก้ให้อีกรอบหน่อยคะ
เป็น SQL SERVER คะ ไม่ใช่ MySQL ใช้คำสั่ง Query ODBC เชื่อมต่อคะ
ข้อมูล มันอยู่ ตาราง dbo.HNOPD_MASTER ตารางเดียวคะ
ต้อง SELECT COUNT ออกมาคะ เพราะนับตาม VN และ VN เป็นรูปแบบ Datetime คะ เดี๋ยวจะ ปรับใช้ SELECT COUNT ดูคะ
ถ้าติดปัญหายังไงจะมาถามต่อคะ
ไม่มีปีในตารางคะ ใช้ วน for เอาคะ
Code (PHP)
<?php
function days_in_month($month, $year)
{
// calculate number of days in a month
return $month == 2 ? ($year % 4 ? 28 : ($year % 100 ? 29 : ($year % 400 ? 28 : 29))) :(($month - 1) % 7 % 2 ? 30 : 31);
}
$nowYear = date('Y');
$startYear = 2549-543;
for($year=$nowYear;$year>=$startYear;$year--)
{
echo "<tr>";
echo "<td><b>".($year+543)."</b></td>";
for($month=1;$month<=12;$month++)
{
$dmonth =cal_days_in_month(CAL_GREGORIAN, $month, $year);
$strSQL= "SELECT COUNT(VN) (count_cur - count_old) * 100 / count_old as total
FROM
(SELECT COUNT (VN) dbo.HNOPD_MASTER.VN as count_cur, COUNT(dbo.HNOPD_MASTER.VN) as count_old FROM
(SELECT ".$year." as cur_y, (".$year.")-1) as old_y FROM dbo.HNOPD_MASTER GROUP BY ".$year." as yy
LEFT JOIN dbo.HNOPD_MASTER on ".$year."(dbo.HNOPD_MASTER.VN) = yy.cur_y
LEFT JOIN dbo.HNOPD_MASTER on ".$year."(dbo.HNOPD_MASTER.VN) = yy.old_y
GROUP BY cur_y
) as tmp
ORDER BUY cur_y";
$objExec = odbc_exec($conn,$strSQL) or die ("Error SQL [".$strSQL."]");
while($objResult = odbc_fetch_object($objExec))
{
$total=$objResult->total;
echo "<td span class=border>".(number_format($total))."<br></td>";
}
echo $strSQL;
}
echo "</tr>";
$nowYear--;
}
?>
|
ประวัติการแก้ไข 2018-04-12 09:48:23 2018-04-12 09:55:40 2018-04-12 10:45:10 2018-04-12 10:45:50
 |
 |
 |
 |
Date :
2018-04-12 09:47:56 |
By :
nottpoo |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
คะ เดี๋ยวลองดูอีกทีคะ
|
 |
 |
 |
 |
Date :
2018-04-12 11:01:07 |
By :
nottpoo |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
กรณีถ้าต้องการแยกเป็นเดือน
Code (SQL)
select
cur_y,
count(case month(tb1.fieldate) when 1 then 1 else null end) as cur_jan,
count(case month(tb1.fieldate) when 2 then 1 else null end) as cur_feb,
.....
count(case month(tb2.fieldate) when 1 then 1 else null end) as old_jan,
count(case month(tb2.fieldate) when 2 then 1 else null end) as old_feb,
|
 |
 |
 |
 |
Date :
2018-04-12 12:48:54 |
By :
Chaidhanan |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|