我想获取在列下找到的所有字符串,并根据它们构建字符串组合。
举例说明:
id group_id name type
----------------------------------------------------
1 100 Computer Computer - Standard
2 100 Telephone Telephone - Modified
3 100 Table Table - Standard - Deal
正如您在type
下看到的那样,我正在寻找单词Standard
和Modified
(还有其他人,但为了简单起见我只显示了2)并希望构建一个字符串组合这些单词一次,并将结果作为每行的列。
我可以使用CASE
和LIKE
获取每行的关键字,并将该字词指定为列的值,如下所示:
id group_id name type consolidated
-----------------------------------------------------------------------
1 100 Computer Computer - Standard Standard
2 100 Telephone Telephone - Modified Modified
3 100 Table Table - Standard - Deal Standard
但那不是我想要的。我只是不知道如何像我想的那样加入他们。
id group_id name type consolidated
-----------------------------------------------------------------------
1 100 Computer Computer - Standard Standard / Modified
2 100 Telephone Telephone - Modified Standard / Modified
3 100 Table Table - Standard - Deal Standard / Modified
抱歉,我忘了添加当前的查询。
SELECT o.id, o.group_id, o.name, o.type,
CASE
WHEN o.type LIKE '%Standard%' THEN 'Standard'
WHEN o.type LIKE '%Modified%' THEN 'Modified'
ELSE ''
END AS consolidated
FROM objects o
WHERE o.group_id = 100
答案 0 :(得分:1)
WITH q as (
SELECT o.id, o.group_id, o.name, o.type,
CASE
WHEN o.type LIKE '%Standard%' THEN 'Standard'
WHEN o.type LIKE '%Modified%' THEN 'Modified'
ELSE ''
END AS lbl
FROM objects o
WHERE o.group_id = 100
)
SELECT
id, group_id, name, type,
(
SELECT LISTAGG(lbl, ' / ') WITHIN GROUP (ORDER BY ??)
FROM (SELECT DISTINCT lbl FROM q) dc
) as consolidated
FROM q;