将日期范围分为几周,并根据该周汇总列

时间:2019-12-23 17:56:16

标签: php mysql

因此,我从数据库中调用数据,其中每个记录都与(列)“ start_time”,“ no_shows和” canceled”相关联。出席成功与否取决于'no_shows'&&'cancelled'==0。

我想检查一下SELECT的结果,并以每周(基于start_time)为基础计算未出现,取消和参加的人数。我该如何实现?

用户将提供一个日期范围,并且数据库将基于该日期范围选择记录。现在,此日期范围将必须按周划分,然后进行计数。我完全坚持按周计算。这是我到目前为止的内容:

SavedToyBase

我无法对此进行任何计数,而是只提取了数据并在对应的一周内分成了数组。

我想将数据提取为以下内容:

    // Multidimensional Array with [boolean][week #]
  $no_shows_weekly = [
    'no_show' => [],
    'week' => []
  ];
  $cancelled_weekly = [
    'cancelled' => [],
    'week' => []
  ];
  $attended_weekly = [
    'attended' => [],
    'week' => []
  ];

  foreach($result as $r) {
    $start_time = new DateTime($r['start_time']);
    if($r['is_no_show'] == 0 && $r['is_cancelled'] == 0) {
        array_push($attended_weekly['attended'], 1);
        array_push($attended_weekly['week'], date_format($start_time, "W"));
    }
    else {
      array_push($attended_weekly['attended'], 0);
      array_push($attended_weekly['week'], date_format($start_time, "W"));
    }
    array_push($no_shows_weekly['no_show'], $r['is_no_show']);
    array_push($no_shows_weekly['week'], date_format($start_time, "W"));
    array_push($cancelled_weekly['cancelled'], $r['is_cancelled']);
    array_push($cancelled_weekly['week'], date_format($start_time, "W"));
  }
  echo json_encode(array(
       'success'=> 1,
       'msg'=>
         array(
           'No Shows' => $no_shows_weekly,
           'Cancellations' => $cancelled_weekly,
           'Attendend' => $attended_weekly
         )
    ));

常规数据库结构:

Week 50: {No_Shows: 10, Cancelled: 5, Attended: 25} 
Week 52: {No_Shows: 10, Cancelled: 5, Attended: 25} 

我也尝试在MySQL中执行此操作:

+------------+-----------+-----------+
| start_time |  no_shows | cancelled | 
+------------+-----------+-----------+
| 2019-12-20 |     1     |     0     |   
| 2019-12-21 |     0     |     0     |  
| 2019-12-22 |     0     |     1     |  

但是,此语句为我返回Null。任何建议表示赞赏!谢谢

0 个答案:

没有答案