我的表是:
table_nm:问题
q_nm | q_txt
----------------------
store | abc
store | efg
store | lmn
store | pqr
store | noc
buy | abab
buy | bcbcv
buy | drtju
overall | lili
overall | mimi
我希望输出如下:
q_nm | q_txt
----------------------
store | abc
| efg
| lmn
| pqr
| noc
buy | abab
| bcbcv
| drtju
overall | lili
| mimi
如何获得以上输出。请帮帮我。
答案 0 :(得分:1)
简单!!! 将当前q_nm与前一行的q_nm进行比较。如果它们相等则使其为空,否则为q_nm。 以下是应用上述逻辑的查询。
Select
CASE WHEN (LAG(Q_NM,1) OVER (ORDER BY Q_NM)) LIKE Q_NM
THEN NULL
ELSE Q_NM
END as Q_NM,
Q_TXT
from table_name
答案 1 :(得分:0)
在Oracle中,您可以通过lag
函数执行此操作。
注意:由于没有要排序的列,因此无法保证同一q_txt
的{{1}}顺序。
q_num
输出:
with tbl(q_nm,q_txt) as (
select 'store','abc ' from dual union all
select 'store','efg ' from dual union all
select 'store','lmn ' from dual union all
select 'store','pqr ' from dual union all
select 'store','noc ' from dual union all
select 'buy','abab ' from dual union all
select 'buy','bcbcv' from dual union all
select 'buy','drtju' from dual union all
select 'overall','lili ' from dual union all
select 'overall','mimi ' from dual
)
select
case when q_nm=prev_q_nm
then null
else q_nm
end as q_nm
,q_txt
from
(select q_nm,q_txt,lag(q_nm,1,0) over (order by q_nm,q_txt) as prev_q_nm from tbl
)