我在Oracle中有以下查询
select (case when seqnum = 1 then id else '0' end) as id,
(case when seqnum = 1 then start else "end" end) as timestamp,
unit
from (select t.*,
row_number() over (partition by id, unit order by start) as seqnum,
count(*) over (partition by id, unit) as cnt
from table t
) t
where seqnum = 1 or seqnum = cnt;
而不是选择单位,这是一个数字,我想带来它的描述。描述存储在另一个表中,我正在尝试使用desciption而不是数字。
SELECT t2.unit_description from t2,t where t2.unit = t.unit
我正在尝试加入这些结果,但它无效。在这种情况下,有人可以帮助我吗?
答案 0 :(得分:2)
这不起作用吗?
select (case when seqnum = 1 then id else '0' end) as id,
(case when seqnum = 1 then start else "end" end) as timestamp,
t2.unit_description
from (select t.*,
row_number() over (partition by id, unit order by start) as seqnum,
count(*) over (partition by id, unit) as cnt
from table t
) t
join t2 on t.unit = t2.unit
where seqnum = 1 or seqnum = cnt;
答案 1 :(得分:1)
我认为这就是你想要的:
select (case when t.seqnum = 1 then t.id else '0' end) as id,
(case when t.seqnum = 1 then t.start else t."end" end) as timestamp,
t.unit,
t2.unit_description
from (select t.*,
row_number() over (partition by id, unit order by start) as seqnum,
count(*) over (partition by id, unit) as cnt
from table t
) t join
t2
on t2.unit = t.unit
where seqnum = 1 or seqnum = cnt;
答案 2 :(得分:1)
试试这个:
select (case when seqnum = 1 then id else 0 end) as id,
(case when seqnum = 1 then start else "end" end) as timestamp,
(SELECT t2.unit_description from t2 where t2.unit = t.unit) as unit
from (select t.*,
row_number() over (partition by id, unit order by start) as seqnum,
count(*) over (partition by id, unit) as cnt
from table t
) t
where seqnum = 1 or seqnum = cnt;