douduocuima61392 2012-02-14 17:30
浏览 46
已采纳

计算日期范围内的天数总计

I want to count totals for all mondays, tuesdays, etc. for any given (user selected) date range, StartDate (sDate) through EndDate (eDate). My current query shows all days within range and I'd like it to SUM my table "count" from each day as it is but display total of each day of week as one occurrence. Using image sample, I want my result to be: Monday - 164 Tuesday - 139 Wednesday - 261 etc.

SQL

SELECT areaname,created, SUM(count) as totals FROM reports WHERE created 
between '".$sDate."'  AND  '".$eDate."' AND area = '".$tArea."' GROUP BY areaname, 
created ORDER BY id ASC

html

foreach ($counts as $row) { ?>
<tr><td><?php echo date('l',strtotime($row['reports']['created'])); ?></td>
<?php echo $row[0]['totals']; ?>
}

sum of count for days within date range

  • 写回答

1条回答 默认 最新

  • douxie2023 2012-02-14 17:33
    关注

    You can use the DATE_FORMAT() function to get the day of the week from a given date, then you can group by that. The syntax is DATE_FORMAT(date, '%W'). So:

    SELECT areaname,
      DATE_FORMAT(created, '%W') AS weekday,
      SUM(count) AS totals
    FROM reports
    WHERE created BETWEEN '".$sDate."' AND '".$eDate."'
      AND area = '".$tArea."'
    GROUP BY areaname, weekday
    ORDER BY id
    

    Note that I left in areaname in the GROUP BY, so this will return one result for every combination of areaname/weekday. If you want totals for all areanames, just remove areaname from the GROUP BY clause.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探