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

Cara menggabungkan 2 atau lebih rentang tanggal menjadi 1

Di sinilah solusi menggunakan variabel pengguna. Diverifikasi dengan data yang Anda berikan dan saya memasukkan lebih banyak data untuk verifikasi.

Untuk menyimpan hasil kueri secara permanen dan membuatnya dapat diakses nanti, hasilnya ditulis ke dalam tabel tbl_new.

Demo penuh>>>P>

SQL:

-- data preparation
create table tbl(acct varchar(100), start_date date, end_date date);
insert into tbl values
('Acct-1', '2016-01-01', '2016-01-07'),
('Acct-1', '2016-01-05', '2016-01-11'),
('Acct-1', '2016-01-18', '2016-01-24'),
('Acct-1', '2016-02-02', '2016-02-08'),
('Acct-1', '2016-02-07', '2016-02-13'),
('Acct-2', '2016-01-01', '2016-01-07'),
('Acct-2', '2016-01-18', '2016-01-24'),
('Acct-2', '2016-01-02', '2016-02-08');


SELECT * FROM tbl;

-- Need queries
SET @last_acct = '', @last_start_date = '1970-01-01', @last_end_date = '9999-12-31', @group = 1;

CREATE TABLE tbl_new (acct varchar(100), start_date date, end_date date);

INSERT INTO tbl_new 
SELECT 
    acct,
    MIN(start_date) start_date,
    MAX(end_date) end_date
FROM
(
    SELECT
        acct, 
        start_date, 
        end_date,
        CASE 
            WHEN 
                acct = @last_acct AND start_date <= @last_end_date 
            THEN 
                @group
            ELSE
                @group := @group + 1
        END group_num, 
        @last_acct := acct,
        @last_start_date := start_date,
        @last_end_date := end_date
    FROM
        tbl
) tbl2
GROUP BY group_num;

SELECT * FROM tbl_new;

Keluaran:

mysql> SELECT * FROM tbl;
+--------+------------+------------+
| acct   | start_date | end_date   |
+--------+------------+------------+
| Acct-1 | 2016-01-01 | 2016-01-07 |
| Acct-1 | 2016-01-05 | 2016-01-11 |
| Acct-1 | 2016-01-18 | 2016-01-24 |
| Acct-1 | 2016-02-02 | 2016-02-08 |
| Acct-1 | 2016-02-07 | 2016-02-13 |
| Acct-2 | 2016-01-01 | 2016-01-07 |
| Acct-2 | 2016-01-18 | 2016-01-24 |
| Acct-2 | 2016-01-02 | 2016-02-08 |
+--------+------------+------------+
8 rows in set (0.00 sec)

mysql>
mysql> -- Need queries
mysql> SET @last_acct = '', @last_start_date = '1970-01-01', @last_end_date = '9999-12-31', @group = 1;
Query OK, 0 rows affected (0.00 sec)

mysql>
mysql> CREATE TABLE tbl_new (acct varchar(100), start_date date, end_date date);
Query OK, 0 rows affected (0.01 sec)

mysql>
mysql> INSERT INTO tbl_new
    -> SELECT
    ->     acct,
    ->     MIN(start_date) start_date,
    ->     MAX(end_date) end_date
    -> FROM
    -> (
    ->     SELECT
    ->         acct,
    ->         start_date,
    ->         end_date,
    ->         CASE
    ->             WHEN
    ->                 acct = @last_acct AND start_date <= @last_end_date
    ->             THEN
    ->                 @group
    ->             ELSE
    ->                 @group := @group + 1
    ->         END group_num,
    ->         @last_acct := acct,
    ->         @last_start_date := start_date,
    ->         @last_end_date := end_date
    ->     FROM
    ->         tbl
    -> ) tbl2
    -> GROUP BY group_num;

Query OK, 5 rows affected (0.00 sec)
Records: 5  Duplicates: 0  Warnings: 0

mysql>
mysql> SELECT * FROM tbl_new;
+--------+------------+------------+
| acct   | start_date | end_date   |
+--------+------------+------------+
| Acct-1 | 2016-01-01 | 2016-01-11 |
| Acct-1 | 2016-01-18 | 2016-01-24 |
| Acct-1 | 2016-02-02 | 2016-02-13 |
| Acct-2 | 2016-01-01 | 2016-01-07 |
| Acct-2 | 2016-01-02 | 2016-02-08 |
+--------+------------+------------+
5 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. Memisahkan Hasil Query PHP menjadi Kolom

  2. metode posting acara kalender penuh ke MySQL

  3. Panggilan Database Asinkron Node JS

  4. PHP PDO - Jumlah Baris

  5. MySql menyiapkan pernyataan - apakah mungkin untuk memparametrikan nama kolom atau nama fungsi?