SQL清理重复结果在一列

时间:2017-04-05 08:27:54

标签: sql oracle11g

我有这个查询结果,有两列,BU和OPTION

BU    OPTION
A        1
A        2
A        3
B        1
B        2
B        3

有没有办法获得这样的结果?

BU    OPTION
A        1
         2
         3
B        1
         2
         3

3 个答案:

答案 0 :(得分:2)

with CTE as
(
select MT.*, row_number() over(partition by MT.BU order by MT.Option) as rn
from MyTable
)
select case when rn = 1 then BU else null end as BU,
       CTE.Option
from CTE

但正如Jarlh所说,这最好在表示层处理

答案 1 :(得分:1)

我认为当option = 1

时bu是可见的
select
(case when option = 1 then bu else '' end) as bu,
option
from your_table

答案 2 :(得分:1)

虽然在表示层上处理得更好,但你仍然可以通过

实现它
SELECT case when rw = 1 THEN Bu ELSE '' END AS BU, Option FROM (

SELECT ROW_NUMBER() over (partition by bu order by bu, option) as rw, * FROM #AA
) X