根据开始日期和结束日期提取从开始日期到结束日期的期间

时间:2019-05-04 12:26:00

标签: mysql

CREATE TABLE `tenantsa` (
  `tena_id` int(20) NOT NULL,
  `tena_id_tenant` int(20) NOT NULL,
  `tena_start_date` date NOT NULL,
  `tena_dwmy_no` int(2) NOT NULL,
  `tena_dwmy_name` varchar(6) NOT NULL,
  `tena_end_date` date NOT NULL,
  `tena_rent` int(1) NOT NULL DEFAULT '6',
  `tena_rent_amount` float(10,2) NOT NULL,
  `tena_rent_frequency` varchar(13) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

-转储表tenantsa

的数据
INSERT INTO `tenantsa` (`tena_id`, `tena_id_tenant`, `tena_start_date`, `tena_dwmy_no`, `tena_dwmy_name`, `tena_end_date`, `tena_rent`, `tena_rent_amount`, `tena_rent_frequency`) VALUES
(1, 3, '2019-01-04', 1, 'Months', '2019-02-03', 6, 500.00, 'Monthly'),
(2, 1, '2019-04-01', 1, 'Months', '2019-04-30', 6, 650.00, 'Monthly'),
(3, 2, '2019-01-01', 6, 'Months', '2019-06-30', 6, 500.00, 'Weekly'),
(4, 4, '2019-04-20', 4, 'Months', '2019-08-19', 6, 670.00, '4-Weekly'),
(5, 5, '2019-04-20', 5, 'Days', '2019-04-24', 6, 4.00, 'Daily'),
(6, 6, '2018-10-29', 4, 'Months', '2019-02-27', 6, 520.00, 'Monthly'),
(7, 7, '2019-03-01', 6, 'Weeks', '2019-04-11', 6, 866.00, 'Monthly');

tena_id tena_id_tenant  tena_start_date tena_dwmy_no    tena_dwmy_name  tena_end_date   tena_rent   tena_rent_amount    tena_rent_frequency
1   3   2019-01-04  1   Months  2019-02-03  6   500 Monthly
2   1   2019-04-01  1   Months  2019-04-30  6   650 Monthly
3   2   2019-01-01  6   Months  2019-06-30  6   500 Weekly
4   4   2019-04-20  4   Months  2019-08-19  6   670 4-Weekly
5   5   2019-05-20  5   Days    2019-04-24  6   4   Daily
6   6   2018-10-29  4   Months  2019-02-27  6   520 Monthly
7   7   2019-03-01  6   Months  2019-04-11  6   866 Monthly
SELECT * FROM (SELECT tena_id, tena_id_tenant, tena_room_id, tena_start_date, tena_rent_frequency, CASE WHEN tena_rent_frequency = 'Daily' THEN DATE_ADD(DATE_ADD(tena_start_date, INTERVAL -1 DAY), INTERVAL 1 DAY) WHEN tena_rent_frequency = 'Weekly' THEN DATE_ADD(DATE_ADD(tena_start_date, INTERVAL -1 DAY), INTERVAL 1 WEEK) WHEN tena_rent_frequency = '2-Weekly' THEN DATE_ADD(DATE_ADD(tena_start_date, INTERVAL -1 DAY), INTERVAL 2 WEEK) WHEN tena_rent_frequency = '4-Weekly' THEN DATE_ADD(DATE_ADD(tena_start_date, INTERVAL -1 DAY), INTERVAL 4 WEEK) WHEN tena_rent_frequency = 'Monthly' THEN DATE_ADD(DATE_ADD(tena_start_date, INTERVAL -1 DAY), INTERVAL 1 MONTH) ELSE '' END AS end_date, CASE WHEN tena_rent_frequency = 'Daily' THEN DATE_ADD(DATE_ADD(tena_start_date, INTERVAL 0 DAY), INTERVAL 1 DAY) WHEN tena_rent_frequency = 'Weekly' THEN DATE_ADD(DATE_ADD(tena_start_date, INTERVAL 0 DAY), INTERVAL 1 WEEK) WHEN tena_rent_frequency = '2-Weekly' THEN DATE_ADD(DATE_ADD(tena_start_date, INTERVAL 0 DAY), INTERVAL 2 WEEK) WHEN tena_rent_frequency = '4-Weekly' THEN DATE_ADD(DATE_ADD(tena_start_date, INTERVAL 0 DAY), INTERVAL 4 WEEK) WHEN tena_rent_frequency = 'Monthly' THEN DATE_ADD(DATE_ADD(tena_start_date, INTERVAL 0 DAY), INTERVAL 1 MONTH) ELSE '' END AS next_due_date, tena_rent_amount FROM tenantsa) AS tenant_agreement                        
Id  Rent Start  Rent End    Tenant Id   Due Date    Rent
1   2019-01-04  2019-02-09  3   2019-01-04  500
2   2019-04-01  2019-04-30  1   2019-04-01  650
3   2019-01-01  2019-02-07  2   2019-01-01  500
3   2019-02-08  2019-02-15  2   2019-02-08  500
3   2019-02-16  2019-02-23  2   2019-02-16  500
It will continue till end date 2019-06-30                   
4   2019-04-20  2019-05-17  4   2019-04-20  670
4   2019-05-18  2019-06-13  4   2019-05-18  670
4   2019-06-14  2019-07-11  4   2019-06-14  670
It will continue till end date 2019-08-19                   
5   2019-05-20  2019-05-20  5   2019-05-20  4
5   2019-05-21  2019-05-21  5   2019-05-21  4
5   2019-05-22  2019-05-22  5   2019-05-22  4
5   2019-05-23  2019-05-23  5   2019-05-23  4
5   2019-05-24  2019-05-24  5   2019-05-24  4
6   2018-10-29  2018-11-28  6   2018-10-29  520
6   2018-11-29  2018-12-28  6   2018-11-29  520
6   2018-12-29  2019-01-28  6   2018-12-29  520
6   2019-01-29  2019-02-27  6   2019-01-29  520

0 个答案:

没有答案