【问题标题】:Distinct in a wm_concat in oracle在 oracle 中的 wm_concat 中不同
【发布时间】:2013-11-08 08:21:28
【问题描述】:

我查询了所有房间及其名称、地址和其他数据。

(select replace(wm_concat( '\par \tab ' || s.address|| '\par \tab ' || s.lib || '\par \tab '), ',', '\par - ')
 from t_room s)

所有数据都太长了,唯一重要的数据是姓名和地址。

事实上,两个房间可以有相同的地址,所以结果我不想要:

room1 address1 - room2 address1

那个,我确实明白了,但是

room1 address1 - room2 at the same address

这在 oracle 10 中可能吗?

我尝试为地址字段添加一个 distinct,但当然不可能。

谢谢。

【问题讨论】:

  • 所以,每当一个房间的地址与它之前的房间相同时,你想获取at the same address字符串而不是实际地址?
  • @PrzemyslawKruglej 是的,因为在这里,我只有 address 作为值,但这可能是一个很长的地址,所以我没有写两倍这个长地址,而是得到了 at the same address .

标签: sql oracle oracle10g concat-ws


【解决方案1】:

您可以使用LAG 函数来实现:

CREATE TABLE t_room_s (
  room VARCHAR2(20),
  address VARCHAR2(20)
);

INSERT INTO t_room_s VALUES ('room1', 'addr 1');
INSERT INTO t_room_s VALUES ('room2', 'addr 1');
INSERT INTO t_room_s VALUES ('room3', 'addr 2');
INSERT INTO t_room_s VALUES ('room4', 'addr 3');
INSERT INTO t_room_s VALUES ('room5', 'addr 4');
INSERT INTO t_room_s VALUES ('room6', 'addr 4');
INSERT INTO t_room_s VALUES ('room7', 'addr 4');
INSERT INTO t_room_s VALUES ('room8', 'addr 5');

SELECT wm_concat(room || ' ' || addr) AS val
  FROM (
    SELECT
        room,
        CASE
          WHEN LAG(address, 1, NULL) OVER (ORDER BY address) = address THEN 'same address'
          ELSE address
        END AS addr
      FROM
        t_room_s
    ORDER BY address
  )
;

输出:

值
-------------------------------------------------- -------------------------------------------------- ---------------------
房间1地址1,房间2相同地址,房间3地址2,房间4地址3,房间5地址4,房间6相同地址,房间7相同地址,房间8地址5

【讨论】:

  • 谢谢。我不知道的oracle关键字太多了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-12-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-02-17
  • 2013-05-23
相关资源
最近更新 更多