汇总一周的统计记录

时间:2018-04-11 18:32:16

标签: mysql mariadb pivot-table

我有一张包含以下信息的表格。

Cross Reference Table

每天有几千个条目,网站信息是硬编码的(abc.com,def.com等)。我想得到上周每天点击的摘要,结果将是:

Table: DETAILS
id     received     site
(PK)   timestamp    varchar

或:

site     count(11/04)   count(10/04)    count(09/04) count(08/04)     
abc.com  300            320             310          320
def.com  200            210             200          220
ghi.com  300            320             310          320
jkl.com  200            210             200          220

谢谢大家。

1 个答案:

答案 0 :(得分:1)

您可以使用上周准备好的状态生成查询:

-- Generate Query for last week (from Monday to Sunday)
-- store query in @myQuery
--
SELECT CONCAT(
'SELECT site,\n',
'sum(if(date(received) = \'', DATE(NOW() - INTERVAL (DAYOFWEEK(NOW())-1) day),'\',1,0)) as `count(',DATE_FORMAT(NOW() - INTERVAL (DAYOFWEEK(NOW())-1) day,'%d/%m'),')`,\n'
'sum(if(date(received) = \'', DATE(NOW() - INTERVAL (DAYOFWEEK(NOW())+0) day),'\',1,0)) as `count(',DATE_FORMAT(NOW() - INTERVAL (DAYOFWEEK(NOW())+0) day,'%d/%m'),')`,\n'
'sum(if(date(received) = \'', DATE(NOW() - INTERVAL (DAYOFWEEK(NOW())+1) day),'\',1,0)) as `count(',DATE_FORMAT(NOW() - INTERVAL (DAYOFWEEK(NOW())+1) day,'%d/%m'),')`,\n'
'sum(if(date(received) = \'', DATE(NOW() - INTERVAL (DAYOFWEEK(NOW())+2) day),'\',1,0)) as `count(',DATE_FORMAT(NOW() - INTERVAL (DAYOFWEEK(NOW())+2) day,'%d/%m'),')`,\n'
'sum(if(date(received) = \'', DATE(NOW() - INTERVAL (DAYOFWEEK(NOW())+3) day),'\',1,0)) as `count(',DATE_FORMAT(NOW() - INTERVAL (DAYOFWEEK(NOW())+3) day,'%d/%m'),')`,\n'
'sum(if(date(received) = \'', DATE(NOW() - INTERVAL (DAYOFWEEK(NOW())+4) day),'\',1,0)) as `count(',DATE_FORMAT(NOW() - INTERVAL (DAYOFWEEK(NOW())+4) day,'%d/%m'),')`,\n'
'sum(if(date(received) = \'', DATE(NOW() - INTERVAL (DAYOFWEEK(NOW())+5) day),'\',1,0)) as `count(',DATE_FORMAT(NOW() - INTERVAL (DAYOFWEEK(NOW())+5) day,'%d/%m'),')`\n',
'FROM yourTable\n',
'WHERE received BETWEEN \'',DATE_FORMAT(NOW() - INTERVAL (DAYOFWEEK(NOW())+5) day,'%Y-%m-%d 00:00:00'),'\' AND \'',DATE_FORMAT(NOW() - INTERVAL (DAYOFWEEK(NOW())-1) day,'%Y-%m-%d 23:59:59'),'\'\n',
'GROUP by site\n',
'ORDER by site;'
) INTO @myQuery;

-- Prepare and execute Query
--
PREPARE stmt FROM @myQuery;
EXECUTE stmt;

-- Free Prepare
--
DEALLOCATE PREPARE stmt;

<强>结果

+-----------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+
| site      | count(08/04) | count(07/04) | count(06/04) | count(05/04) | count(04/04) | count(03/04) | count(02/04) |
+-----------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+
| abc.de    | 0            | 0            | 0            | 0            | 0            | 0            | 2            |
| def.at    | 0            | 0            | 0            | 0            | 2            | 0            | 0            |
| first.net | 0            | 0            | 0            | 0            | 0            | 0            | 1            |
| last.org  | 1            | 0            | 0            | 0            | 0            | 0            | 0            |
+-----------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+

<强>样品

MariaDB [test]> -- show table values
MariaDB [test]> --
MariaDB [test]> SELECT * from yourTable;
+----+---------------------+------------+
| id | received            | site       |
+----+---------------------+------------+
|  1 | 2018-04-01 23:59:59 | before.com |
|  2 | 2018-04-02 00:00:00 | first.net  |
|  5 | 2018-04-02 03:00:00 | abc.de     |
|  6 | 2018-04-02 04:12:00 | abc.de     |
|  7 | 2018-04-04 07:00:00 | def.at     |
|  8 | 2018-04-04 07:07:00 | def.at     |
|  9 | 2018-04-08 23:59:59 | last.org   |
| 10 | 2018-04-09 00:00:00 | after.ch   |
+----+---------------------+------------+
8 rows in set (0.00 sec)

MariaDB [test]> 
MariaDB [test]> -- Generate Query for last week (from Monday to Sunday)
MariaDB [test]> -- store query in @myQuery
MariaDB [test]> --
MariaDB [test]> SELECT CONCAT(
    -> 'SELECT site,\n',
    -> 'sum(if(date(received) = \'', DATE(NOW() - INTERVAL (DAYOFWEEK(NOW())-1) day),'\',1,0)) as `count(',DATE_FORMAT(NOW() - INTERVAL (DAYOFWEEK(NOW())-1) day,'%d/%m'),')`,\n'
    -> 'sum(if(date(received) = \'', DATE(NOW() - INTERVAL (DAYOFWEEK(NOW())+0) day),'\',1,0)) as `count(',DATE_FORMAT(NOW() - INTERVAL (DAYOFWEEK(NOW())+0) day,'%d/%m'),')`,\n'
    -> 'sum(if(date(received) = \'', DATE(NOW() - INTERVAL (DAYOFWEEK(NOW())+1) day),'\',1,0)) as `count(',DATE_FORMAT(NOW() - INTERVAL (DAYOFWEEK(NOW())+1) day,'%d/%m'),')`,\n'
    -> 'sum(if(date(received) = \'', DATE(NOW() - INTERVAL (DAYOFWEEK(NOW())+2) day),'\',1,0)) as `count(',DATE_FORMAT(NOW() - INTERVAL (DAYOFWEEK(NOW())+2) day,'%d/%m'),')`,\n'
    -> 'sum(if(date(received) = \'', DATE(NOW() - INTERVAL (DAYOFWEEK(NOW())+3) day),'\',1,0)) as `count(',DATE_FORMAT(NOW() - INTERVAL (DAYOFWEEK(NOW())+3) day,'%d/%m'),')`,\n'
    -> 'sum(if(date(received) = \'', DATE(NOW() - INTERVAL (DAYOFWEEK(NOW())+4) day),'\',1,0)) as `count(',DATE_FORMAT(NOW() - INTERVAL (DAYOFWEEK(NOW())+4) day,'%d/%m'),')`,\n'
    -> 'sum(if(date(received) = \'', DATE(NOW() - INTERVAL (DAYOFWEEK(NOW())+5) day),'\',1,0)) as `count(',DATE_FORMAT(NOW() - INTERVAL (DAYOFWEEK(NOW())+5) day,'%d/%m'),')`\n',
    -> 'FROM yourTable\n',
    -> 'WHERE received BETWEEN \'',DATE_FORMAT(NOW() - INTERVAL (DAYOFWEEK(NOW())+5) day,'%Y-%m-%d 00:00:00'),'\' AND \'',DATE_FORMAT(NOW() - INTERVAL (DAYOFWEEK(NOW())-1) day,'%Y-%m-%d 23:59:59'),'\'\n',
    -> 'GROUP by site\n',
    -> 'ORDER by site;'
    -> ) INTO @myQuery;
Query OK, 1 row affected (0.01 sec)

MariaDB [test]> 
MariaDB [test]> 
MariaDB [test]> -- Show query - only to review
MariaDB [test]> --
MariaDB [test]> SELECT @myQuery;
+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| @myQuery                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         |
+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| SELECT site,
sum(if(date(received) = '2018-04-08',1,0)) as `count(08/04)`,
sum(if(date(received) = '2018-04-07',1,0)) as `count(07/04)`,
sum(if(date(received) = '2018-04-06',1,0)) as `count(06/04)`,
sum(if(date(received) = '2018-04-05',1,0)) as `count(05/04)`,
sum(if(date(received) = '2018-04-04',1,0)) as `count(04/04)`,
sum(if(date(received) = '2018-04-03',1,0)) as `count(03/04)`,
sum(if(date(received) = '2018-04-02',1,0)) as `count(02/04)`
FROM yourTable
WHERE received BETWEEN '2018-04-02 00:00:00' AND '2018-04-08 23:59:59'
GROUP by site
ORDER by site; |
+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

MariaDB [test]> 
MariaDB [test]> 
MariaDB [test]> -- Prepare and execute Query
MariaDB [test]> --
MariaDB [test]> PREPARE stmt FROM @myQuery;
Query OK, 0 rows affected (0.00 sec)
Statement prepared

MariaDB [test]> EXECUTE stmt;
+-----------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+
| site      | count(08/04) | count(07/04) | count(06/04) | count(05/04) | count(04/04) | count(03/04) | count(02/04) |
+-----------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+
| abc.de    | 0            | 0            | 0            | 0            | 0            | 0            | 2            |
| def.at    | 0            | 0            | 0            | 0            | 2            | 0            | 0            |
| first.net | 0            | 0            | 0            | 0            | 0            | 0            | 1            |
| last.org  | 1            | 0            | 0            | 0            | 0            | 0            | 0            |
+-----------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+
4 rows in set (0.00 sec)

MariaDB [test]> 
MariaDB [test]> -- Free Prepare
MariaDB [test]> --
MariaDB [test]> DEALLOCATE PREPARE stmt;
Query OK, 0 rows affected (0.00 sec)

MariaDB [test]>