(บันทึกกันลืม) คำสั่ง SQL Query + PHP ตามเงื่อนไขด้านล่าง
แสดงจำนวน Leads ที่สร้างขึ้นในแต่ละเดือนของปีนี้ Group ตาม Department
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
<?php $current_year= date("Y"); $query = " SELECT "; for($i=1;$i<=12;$i++){ $first_of_month = gmmktime(0,0,0,sprintf('%02d',$i),1,$current_year); $days_in_month = gmdate('t',$first_of_month); $query .=" SUM( CASE WHEN DATE( createdtime ) BETWEEN DATE('".$current_year."-".sprintf('%02d',$i)."-01') AND LAST_DAY('".$current_year."-".sprintf('%02d',$i)."-".$days_in_month."') THEN 1 ELSE 0 END ) AS c_".$i.","; } $query .=" SUM( CASE WHEN DATE( createdtime ) BETWEEN DATE('".$current_year."-01-01') AND LAST_DAY('".$current_year."-12-31') THEN 1 ELSE 0 END ) AS c_all, department, FROM tbl_leads GROUP BY department Having c_all>0 "; ?> |