我是Oracle新手,我需要帮助解决此问题。我有数据样本/记录表,如:
name | datetime
-----------
A | 20140414 10:00
A | 20140414 10:30
A | 20140414 11:00
B | 20140414 11:30
B | 20140414 12:00
A | 20140414 12:30
A | 20140414 13:00
A | 20140414 13:30
我需要"分组" /获取此表单的信息:
name | datetime_from | datetime_to
----------------------------------
A | 20140414 10:00 | 20140414 11:00
B | 20140414 11:30 | 20140414 12:00
A | 20140414 12:30 | 20140414 13:30
我无法找到任何与此类似的查询解决方案。有人可以帮帮我吗?
注意:我不想使用临时表。
谢谢, 帕维尔
答案 0 :(得分:2)
SQL> with t (name, datetime) as
2 (
3 select 'A', to_date('20140414 10:00','YYYYMMDD HH24:MI') from dual union all
4 select 'A', to_date('20140414 10:30','YYYYMMDD HH24:MI') from dual union all
5 select 'A', to_date('20140414 11:00','YYYYMMDD HH24:MI') from dual union all
6 select 'B', to_date('20140414 11:30','YYYYMMDD HH24:MI') from dual union all
7 select 'B', to_date('20140414 12:00','YYYYMMDD HH24:MI') from dual union all
8 select 'A', to_date('20140414 12:30','YYYYMMDD HH24:MI') from dual union all
9 select 'A', to_date('20140414 13:00','YYYYMMDD HH24:MI') from dual union all
10 select 'A', to_date('20140414 13:30','YYYYMMDD HH24:MI') from dual
11 )
12 select name, min(datetime) datetime_from, max(datetime) datetime_to
13 from (
14 select name, datetime,
15 datetime-(1/48)*(row_number() over(partition by name order by datetime)) dt
16 from t
17 )
18 group by name,dt
19 order by 2,1
20 /
N DATETIME_FROM DATETIME_TO
- -------------- --------------
A 20140414 10:00 20140414 11:00
B 20140414 11:30 20140414 12:00
A 20140414 12:30 20140414 13:30
答案 1 :(得分:1)
您需要找到值相同的时段。 Oracle中最简单的方法是使用lag()
函数,一些逻辑和聚合:
select name, min(datetime), max(datetime)
from (select t.*,
sum(case when name <> prevname then 1 else 0 end) over (order by datetime) as cnt
from (select t.*, lag(name) over (order by datetime) as prevname
from table t
) t
) t
group by name, cnt;
对于给定的datetime
值,这个名称已经开启或在该日期时间之前的次数是多少。这标识了&#34; constancy&#34;的句点,然后用于聚合。
答案 2 :(得分:1)
由于9000建议您可以进行如下查询:
select
a.name,
Max(a.datetime),
Min(b.datetime)
from
table a,
table b
group by
a.name
where a.name = b.name