如何过滤表以在Oracle中显示结果?

时间:2018-12-12 17:54:47

标签: sql oracle case

我有一个源表:

enter image description here

我需要将其映射到目标表,在该表中,我将选择一个唯一的ID和状态值最小的颜色,并选择该ID状态值最大的形状。例如, enter image description here

2 个答案:

答案 0 :(得分:2)

您可以在子查询中使用Oracle Analytic Functions。无需加入。

select
r.id,
r.color,
r.shape
from
(
    select
    s.id,
    first_value(s.color) over (partition by s.id order by s.status) as color,
    first_value(s.shape) over (partition by s.id order by s.status desc) as shape,
    row_number() over (partition by s.id order by s.status) as row_index
    from shapes s
) r
where r.row_index = 1;

答案 1 :(得分:-1)

好的,我终于可以写下来了:

Select x.id, x.shape,y.color 
from 
     (Select id ,shape 
      from Shapes
      where status in ( Select max (status) 
                        from shapes 
                        group  by id)
        ) x 
  join 
        (Select id , color 
          from shapes 
          where status in (Select min(status) 
                           from shapes 
                           group by id )
       ) y 
on x.id = y.id;

欢迎更短的答案