gpt4 book ai didi

MySQL - 计算未清项目并从不同表中获取日期

转载 作者:行者123 更新时间:2023-11-29 06:35:22 25 4
gpt4 key购买 nike

大家星期五快乐,我有一些事情无法解决。我之前曾请求过支持,发现这是我能做的最好的事情:)因此,我想根据两个日期(打开和关闭)计算未清项目的数量。所有数据都将计入 t2,日期将从 t1 开始(t1 存储了大量日期,因此我使用SELECT DISTINCT)

因此,表格如下:

CREATE TABLE t1
(

ID int (10),
Date1 date);


insert into T1 values
( 1, '2018-12-17'),
( 2, '2018-12-18'),
( 3, '2018-12-19'),
( 4, '2018-12-19'),
( 5, '2018-12-19'),
( 6, '2018-12-20'),
( 7, '2018-12-20'),
( 8, '2018-12-21'),
( 9, '2018-12-22'),
(10, '2018-12-23'),
(11, '2018-12-24'),
(12, '2018-12-25'),
(13, '2018-12-26'),
(14, '2018-12-27'),
(15, '2018-12-28');

CREATE TABLE t2
(

ID int (10),
Open_Date date,
Close_Date date);


insert into t2 values
( 1, '2018-12-17', '2018-12-18'),
( 2, '2018-12-18', '2018-12-18'),
( 3, '2018-12-18', '2018-12-18'),
( 4, '2018-12-19', '2018-12-20'),
( 5, '2018-12-19', '2018-12-21'),
( 6, '2018-12-20', '2018-12-22'),
( 7, '2018-12-20', '2018-12-22'),
( 8, '2018-12-21', '2018-12-25'),
( 9, '2018-12-22', '2018-12-26'),
(10, '2018-12-23', '2018-12-27');

这就是我想要的结果:

+------------+------------+
| Date | Count_open |
+------------+------------+
| 17/12/2018 | 1 |
| 18/12/2018 | 3 |
| 19/12/2018 | 2 |
| 20/12/2018 | 3 |
| 21/12/2018 | 4 |
| 22/12/2018 | 4 |
| 23/12/2018 | 3 |
| 23/12/2018 | 0 |
| 25/12/2018 | 0 |
| 27/12/2018 | 0 |
| 27/12/2018 | 0 |
| 28/12/2018 | 0 |
+------------+------------+

我完全无法理解代码,需要你的帮助。

最佳答案

也许这就是您正在寻找的东西?

select
x.Date1 as 'Date',
count(distinct t2.id) as 'Count_open'
from (select distinct Date1 from t1) x
left join t2 on x.Date1 between t2.Open_Date and t2.Close_Date
group by x.Date1
order by x.Date1

结果:

Date        Count_open
---------- ----------
2018-12-17 1
2018-12-18 3
2018-12-19 2
2018-12-20 4
2018-12-21 4
2018-12-22 4
2018-12-23 3
2018-12-24 3
2018-12-25 3
2018-12-26 2
2018-12-27 1
2018-12-28 0

关于MySQL - 计算未清项目并从不同表中获取日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54148705/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com