gpt4 book ai didi

postgresql - 如何在删除空格后获得唯一值并按 postgres 中的某些不同列分组

转载 作者:行者123 更新时间:2023-11-29 12:58:59 26 4
gpt4 key购买 nike

我有一张 table

id type note
1 A ab cd
1 B cdef
1 A abd
1 A abcd

我想在删除空格后得到所有独特的注释,所以我的结果是这样的

id type note
1 A ab cd
1 A abd
1 B cdef

因为删除空格后 ab cdabcd 将相同。我正在使用 Postgres。请注意,id 列不是唯一的。

最佳答案

这是一个经典的分组最大值,但你只是将它分组在一个表达式而不是列上

SELECT id, type, note FROM
(SELECT DISTINCT ON(replace(note,' ',''))
id
,type
,note
FROM table1
ORDER BY replace(note,' ','')
) AS q

sqlfiddle

关于postgresql - 如何在删除空格后获得唯一值并按 postgres 中的某些不同列分组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35323087/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com