在PSQL中,我正在从名为genus_synonym
的表中聚合连接字符串该表的一个例子如下
id|genus_synonym|specific_epithet_synonym ---|----------|----------- 1 | Acer | rubrum 2 | Acer | nigrum 3 | Betula | lenta 4 | Carya | ovata 5 | Carya | glabra 6 | Carya | tomentosa
here is an image of my table if that is easier
我正在使用的代码就像这样
Select
string_agg(CONCAT(CONCAT(s."genus_synonym"), ' ', s.specific_epithet_synonym), ', ')as syno
FROM
"public"."synonyms" as s
结果是:
Acer rubrum,Acer nigrum,Betula lenta,Carya ovata,Carya glabra,Carya tomentosa
我想弄清楚的是,是否有可能产生这个:
Acer rubrum,A。nigrum,Betula lenta,Carya ovata,C。glabra,C。tomentosa
基本上我想将属名缩写为单个字母,后面跟着一个句号,第二个和另外一个时间重复一个属。
即使这是不可能的,但知道这一点会很好,然后如果还有其他方法可以解决这个问题。
此外,看起来没有人回答我的问题。不清楚吗?我以前没能找到这样的问题。请让我知道如何才能更好地解决这个问题。
答案 0 :(得分:0)
QRY:
t=# with a as (
select *,case when row_number() over (partition by genus_synonym) > 1 and count(1) over (partition by genus_synonym) > 1 then substr(genus_synonym,1,1)||'.' else genus_synonym end sh
from s92
)
select string_agg(concat(sh,' ',specific_epithet_synonym),',')
from a;
string_agg
-----------------------------------------------------------------------
Acer rubrum,A. nigrum,Betula lenta,Carya ovata,C. glabra,C. tomentosa
(1 row)
Time: 0.353 ms
模拟您的数据:
t=# create table s92 (id int,genus_synonym text,specific_epithet_synonym text);
CREATE TABLE
Time: 7.587 ms
t=# copy s92 from stdin delimiter '|';
Enter data to be copied followed by a newline.
End with a backslash and a period on a line by itself.
>> 1 | Acer | rubrum
2 | Acer | nigrum
3 | Betula | lenta
4 | Carya | ovata
5 | Carya | glabra
6 | Carya | tomentosa
>> >> >> >> >> >> \.
COPY 6
Time: 6308.728 ms