oracle重复列只显示一次的实现
(2020-03-25 17:09:25)分类: IT |
CREATE TABLE test(
ob_id VARCHAR(32),
ob_name VARCHAR(32)
);
INSERT INTO test VALUES('A001','A001-a');
INSERT INTO test VALUES('A001','A001-b');
INSERT INTO test VALUES('A001','A001-c');
INSERT INTO test VALUES('A001','A001-d');
INSERT INTO test VALUES('A002','A002-a');
INSERT INTO test VALUES('A002','A002-b');
INSERT INTO test VALUES('A002','A002-c');
INSERT INTO test VALUES('A002','A002-d');
COMMIT;
查询结果:
OB_ID
A001
A001
A001
A001
A002
A002
A002
A002
如何实现下面的查询结果:
OB_ID
A001
A002
方法一:
select decode(rownum,1,ob_id,5,ob_id) ob_id,ob_name from test;
方法二:
select decode(row_number() over(partition by ob_id order by ob_name),1,ob_id) ob_id,ob_name from test t;
方法三:
select decode(lag(ob_id) over(partition by ob_id order by ob_name), ob_id, null, ob_name) ob_id,ob_name from test;
方法四:
SELECT CASE WHEN m.rn=m.rn1 THEN NULL ELSE m.ob_id END ob_id, m.ob_name
FROM
(SELECT d.ob_id,d.ob_name,rn,LAG(d.rn) OVER(ORDER BY d.ob_id) rn1 FROM
(SELECT
t.ob_id,t.ob_name,dense_RANK() OVER( ORDER BY t.ob_id) rn
FROM
方法五:
select decode(t1.ob_name, v.name, t1.ob_id, ''), t1.ob_name
order by t1.ob_id, t1.ob_name;
方法六:
在sqlplus中
break on ob_id skip
select * from test;