Sql 带IDs的最小最大值-Oracle
尝试在Oracle 8i中获取此输出:Sql 带IDs的最小最大值-Oracle,sql,oracle,Sql,Oracle,尝试在Oracle 8i中获取此输出: column1 |time ________|_______ ABC | 00:00:01 END | 00:00:03 123 | 00:00:04 END | 00:00:07 ABC | 00:00:08 END | 00:00:10 使用来自另一个查询的此输出 column1 |time |ID ________|___________| ABC | 00
column1 |time
________|_______
ABC | 00:00:01
END | 00:00:03
123 | 00:00:04
END | 00:00:07
ABC | 00:00:08
END | 00:00:10
使用来自另一个查询的此输出
column1 |time |ID
________|___________|
ABC | 00:00:01 | 1
ABC | 00:00:02 | 1
ABC | 00:00:03 | 1
123 | 00:00:04 | 1
123 | 00:00:05 | 1
123 | 00:00:06 | 1
123 | 00:00:07 | 1
ABC | 00:00:08 | 2
ABC | 00:00:09 | 2
ABC | 00:00:10 | 2
此查询在不考虑ID的情况下获取最小值和最大值
select (case when n.n = 1 then column1 else 'END' end) as column1,
(case when n.n = 1 then firsttime else lasttime end) as "time"
from (select column1, min(time) as firsttime, max(time) as lasttime
from t
group by column1
) t cross join
(select 1 as n from dual union all select 2 from dual) n
order by column1, n.n;
如何做同样的事情,而不是在CulnN1中获得第一个和最后一个值,也考虑ID吗?
< P>你需要在逻辑中包含<代码> ID <代码>,在<>代码>组中按和<<代码>顺序< < /代码>:select (case when n.n = 1 then column1 else 'END' end) as column1,
(case when n.n = 1 then firsttime else lasttime end) as "time"
from (select column1, id, min(time) as firsttime, max(time) as lasttime
from t
group by column1, id
) t cross join
(select 1 as n from dual union all select 2 from dual) n
order by id, column1, n.n;
将
按列分组1
替换为按列分组1,id
?我想您希望按t.firsttime,t.column1,n.n进行排序,并将相邻值保持在一起?因此,当我添加对结果进行分组的新列时,我将它们添加到组ny中并按顺序排序?它们的正确顺序是什么?例如,如果我有另一个级别的分组值,我会添加列adter column1?@Dalek。分组依据中列的顺序没有区别。