1. 程式人生 > >MySQL按周統計 WEEK 實例

MySQL按周統計 WEEK 實例

分組 eight group weight 根據 sts mysq pan arch

MySQL按周統計每周數據總和,用到了WEEK,subdate,date_format,date_sub,date_add函數。

  WEEK() 查看給定日期周數,語法:WEEK(date, mode);

  SUBDATE() 函數做日期減法操作,語法:SUBDATE(date,INTERVAL expr unit) 和 SUBDATE(expr,days);

  DATE_FORMAT() 函數用於以不同的格式顯示日期/時間數據,語法:DATE_FORMAT(date,format);

  DATE_SUB() 函數從日期減去指定的時間間隔,語法:DATE_SUB(date,INTERVAL expr type);

  DATE_ADD() 函數向日期添加指定的時間間,語法:DATE_ADD(date,INTERVAL expr type);

樣例數據:

-- 創建數據樣例表
CREATE TABLE `day_weight_tb` (
  `day_date` varchar(16) DEFAULT NULL,
  `in_weight` varchar(64) DEFAULT NULL,
  `out_weight` varchar(64) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- 插入樣例數據
INSERT INTO `day_weight_tb` (`day_date`, `in_weight`, `out_weight`) VALUES (
2018-03-10, 18.84, 74.14); INSERT INTO `day_weight_tb` (`day_date`, `in_weight`, `out_weight`) VALUES (2019-01-21, 17.69, 25.97); INSERT INTO `day_weight_tb` (`day_date`, `in_weight`, `out_weight`) VALUES (2019-02-01, 16.48, 10.0); INSERT INTO `day_weight_tb` (`day_date`, `in_weight`, `out_weight`) VALUES (
2019-01-29, 10.9, 74.12); INSERT INTO `day_weight_tb` (`day_date`, `in_weight`, `out_weight`) VALUES (2019-02-13, 15.25, 16.62); INSERT INTO `day_weight_tb` (`day_date`, `in_weight`, `out_weight`) VALUES (2019-01-12, 19.21, 95.42); INSERT INTO `day_weight_tb` (`day_date`, `in_weight`, `out_weight`) VALUES (2019-02-14, 26.38, 20.59); INSERT INTO `day_weight_tb` (`day_date`, `in_weight`, `out_weight`) VALUES (2019-01-24, 15.99, 16.823); INSERT INTO `day_weight_tb` (`day_date`, `in_weight`, `out_weight`) VALUES (2019-01-13, 94.846, 56.256); INSERT INTO `day_weight_tb` (`day_date`, `in_weight`, `out_weight`) VALUES (2019-03-03, 15.63, 12.128); INSERT INTO `day_weight_tb` (`day_date`, `in_weight`, `out_weight`) VALUES (2019-03-12, 14.282, 24.046); INSERT INTO `day_weight_tb` (`day_date`, `in_weight`, `out_weight`) VALUES (2019-01-16, 14.045, 11.73); INSERT INTO `day_weight_tb` (`day_date`, `in_weight`, `out_weight`) VALUES (2019-03-08, 23.41, 23.441); INSERT INTO `day_weight_tb` (`day_date`, `in_weight`, `out_weight`) VALUES (2019-01-31, 11.177, 4332.26); INSERT INTO `day_weight_tb` (`day_date`, `in_weight`, `out_weight`) VALUES (2019-01-23, 11.8, 17104.49); INSERT INTO `day_weight_tb` (`day_date`, `in_weight`, `out_weight`) VALUES (2019-03-17, 17.95, 13.81); INSERT INTO `day_weight_tb` (`day_date`, `in_weight`, `out_weight`) VALUES (2019-03-01, 19.23, 20.43); INSERT INTO `day_weight_tb` (`day_date`, `in_weight`, `out_weight`) VALUES (2019-02-05, 21.55, 51.0); INSERT INTO `day_weight_tb` (`day_date`, `in_weight`, `out_weight`) VALUES (2019-02-27, 13.003, 23.32); INSERT INTO `day_weight_tb` (`day_date`, `in_weight`, `out_weight`) VALUES (2019-03-11, 16.44, 24.44); INSERT INTO `day_weight_tb` (`day_date`, `in_weight`, `out_weight`) VALUES (2019-02-28, 16.48, 23.4);
-- 創建結果表,插入統計結果
drop TABLE if exists week_count_tb;
CREATE TABLE `week_count_tb` (
  `week_day` varchar(32) DEFAULT NULL,
  `week_num` varchar(32) DEFAULT NULL,
  `in_weight` varchar(64) DEFAULT NULL,
  `out_weight` varchar(64) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

delete from week_count_tb;
INSERT INTO week_count_tb 
SELECT
    *
FROM
    (
        SELECT
            middle.week_day,
            middle.week_num,
            sum(in_weight) in_weight,
            sum(out_weight) out_weight
        FROM
            (
                SELECT
                    subdate(
                        date_sub(day_date, INTERVAL 1 DAY),
                        date_format(date_sub(day_date, INTERVAL 1 DAY),%w) - 7) week_day,
                    WEEK (
                        date_add(subdate(
                                date_sub(day_date, INTERVAL 1 DAY),
                                date_format(date_sub(day_date, INTERVAL 1 DAY),%w) - 7),INTERVAL 6 DAY),2) week_num,
                    in_weight,
                    out_weight
                FROM
                    day_weight_tb
            ) middle
        GROUP BY
            week_day,
            week_num
    ) result;

思路:先確定一天是屬於一年中的具體周數,再獲取到一周中的周末日期,最後根據周數分組求和。

MySQL按周統計 WEEK 實例