Mysql
 sql >> Teknologi Basis Data >  >> RDS >> Mysql

Kelompokkan menurut rentang X hari

Berikut adalah contoh dengan tanggal.

create table t1(created date not null);

insert 
  into t1(created) values (date '2011-01-09')
                         ,(date '2011-01-10')
                         ,(date '2011-01-11')
                         ,(date '2011-01-12')
                         ,(date '2011-01-13')
                         ,(date '2011-01-14')
                         ,(date '2011-01-15')
                         ,(date '2011-01-16')
                         ,(date '2011-01-17')
                         ,(date '2011-01-18')
                         ,(date '2011-01-19')
                         ,(date '2011-01-20');

select floor(datediff(now(), created) / 3) * 3 as days_ago
      ,min(created)
      ,max(created)
      ,count(*)
  from t1
 group 
    by floor(datediff(now(), created) / 3);

+----------+--------------+--------------+----------+
| days_ago | min(created) | max(created) | count(*) |
+----------+--------------+--------------+----------+
|        0 | 2011-01-18   | 2011-01-20   |        3 |
|        3 | 2011-01-15   | 2011-01-17   |        3 |
|        6 | 2011-01-12   | 2011-01-14   |        3 |
|        9 | 2011-01-09   | 2011-01-11   |        3 |
+----------+--------------+--------------+----------+
4 rows in set (0.00 sec)


  1. Database
  2.   
  3. Mysql
  4.   
  5. Oracle
  6.   
  7. Sqlserver
  8.   
  9. PostgreSQL
  10.   
  11. Access
  12.   
  13. SQLite
  14.   
  15. MariaDB
  1. Bagaimana cara membuat PEMICU di SEQUELIZE (nodeJS)?

  2. Masalah UTF-8 dengan tinymce?

  3. Django - menginstal kesalahan mysqlclient:mysqlclient 1.3.13 atau yang lebih baru diperlukan; kamu punya 0.9.3

  4. MySQL LIMIT dalam Subquery Berkorelasi

  5. Bagaimana cara menginstal adaptor Rails MySQL?