我有表t1与4列,并希望获得行,学生留在两个或更多的时间
思路:在date time中添加每个学生的持续时间值,然后与up列date time进行比较
Student date time Duration
a 12-09-19 11:12:30 30
b 12-09-19 11:13:30 60
c 12-09-19 11:14:00 60
d 12-09-19 11:18:30 40我想要的结果
Student date time Duration
b 12-09-19 11:13:30 60
c 12-09-19 11:14:00 60手动添加持续时间,然后找到b学生11:13:30 + 60 = 11:14:30,这比学生c日期时间更大,因此b和c都在一起30秒
发布于 2019-03-30 13:39:00
这里有一个选择(甲骨文;我不会说MySQL)。
拆分日期和时间不是一个好主意;这些值应该包含在相同的DATE数据类型列中(我就是这么做的;如果您的模型真的使用了两列,请先将它们连接起来,然后应用TO_DATE函数)。
这是做什么的?
T1 CTE是您的示例dataINTER将秒数添加到每个DATUM列值(对于Oracle而言,DATE列名是无效的,因为它是为数据类型名称保留的)T1 final SELECT返回INTER.DATIME值与重叠的学生
SQL> alter session set nls_Date_format = 'dd.mm.yyyy hh24:Mi:ss';
Session altered.
SQL> with t1 (student, datum, duration) as
2 (select 'a', to_date('12.09.2019 11:12:30', 'dd.mm.yyyy hh24:mi:ss'), 30 from dual
3 union all
4 select 'b', to_date('12.09.2019 11:13:30', 'dd.mm.yyyy hh24:mi:ss'), 60 from dual
5 union all
6 select 'c', to_date('12.09.2019 11:14:00', 'dd.mm.yyyy hh24:mi:ss'), 60 from dual
7 union all
8 select 'd', to_date('12.09.2019 11:18:30', 'dd.mm.yyyy hh24:mi:ss'), 40 from dual
9 ),
10 inter as
11 (select student, datum, duration,
12 datum + (column_value - 1) / (24 * 60 * 60) datime
13 from t1,
14 table(cast(multiset(select level from dual
15 connect by level <= duration + 1
16 ) as sys.odcinumberlist))
17 )
18 select distinct student, datum, duration
19 from inter
20 where datime in (select datime
21 from inter
22 group by datime
23 having count(*) > 1);
S DATUM DURATION
- ------------------- ----------
b 12.09.2019 11:13:30 60
c 12.09.2019 11:14:00 60
SQL>发布于 2019-03-31 03:54:11
我同意,在oracle中拆分日期和时间不是一个好主意。一个简单的解决方案是当另一个学生已经在那里时找到所有来的学生,或者当一个学生到达时另一个学生已经在那里。
设置
alter session set nls_Date_format = 'dd.mm.yyyy hh24:Mi:ss';
create table test (
Student varchar2(31),
sdate date,
Duration number);
insert into test values ('a','12-09-19 11:12:30',30);
insert into test values ('b','12-09-19 11:13:30',60);
insert into test values ('c','12-09-19 11:14:00',60);
insert into test values ('d','12-09-19 11:18:30',40);SQL
select * from test t
where exists (select 'x' from test where t.sdate between sdate and sdate + (duration / (24*60*60)) and t.student != student)
or exists (select 'x' from test where sdate between t.sdate and t.sdate + (t.duration / (24*60*60)) and t.student != student); https://stackoverflow.com/questions/55428272
复制相似问题