【问题标题】:Sql query to return elements with same ID as comma separated stringSql 查询返回与逗号分隔字符串具有相同 ID 的元素
【发布时间】:2023-04-03 22:50:02
【问题描述】:

我有两张表,table1有一个entry_IDentry_date等入口信息。 table2entry_IDentry_subject。每个entry_ID可以有任意多个entry_subjects

我想要一个返回entry_IDentry_date 的查询,以及与该条目对应的主题列表,以逗号分隔。

第一步似乎只是获取一个返回entry_ID 和来自table2 的以逗号分隔的主题列表的查询。一旦我知道加入应该很容易。
我调整了recursive CTE method from this site: 以适应我的情况:

WITH RECURSIVE CTE (entry_ID, subjectlist, subject, length)
    AS ( SELECT entry_ID, cast( '' as varchar(8000))
                        , cast( '' as varchar(8000)), 0
         FROM table2 
         GROUP BY entry_ID
         UNION ALL 
         SELECT t2.entry_ID, 
             cast(subjectlist || CASE length = 0 THEN '' ELSE ', ' END
                              || entry_subject AS varchar(8000) ),
             cast (t2.entry_subject as varchar(8000)),
             length +1
         FROM CTE c 
         INNER JOIN table2 t2 
             on c.entry_ID=t2.entry_ID where t2.entry_subject > c.subject)
SELECT entry_ID, subjectlist FROM (
    SELECT entry_ID, subjectlist, RANK() OVER (
        PARTITION BY entry_ID order by length DESC)
    FROM CTE) D (entry_ID, subjectlist, rank) where rank = 1;

它有效,我得到了我期望的响应。为了实现我的最终目标,我使用的查询是这样的:

SELECT t1.* t2.subjectlist FROM table1 
    JOIN (ABOVE QUERY) AS t2 on t1.entry_ID=t2.entry_ID; 

这看起来很笨拙。这真的是最好的方法吗?

【问题讨论】:

  • 就个人而言,如果可能的话,我会在数据库之外的中间层执行此操作。
  • 中间层是什么意思?我想我可以在进行数据库调用的 bash 脚本中更轻松地做到这一点,但我使用的是一个实用程序库,它接受输入字符串,它解释为查询并直接在适当的数据库中执行。
  • 哦,我没有得到那种区别。是的,这就是我的意思。

标签: sql postgresql aggregate-functions


【解决方案1】:

如果我理解正确,那么应该有一个更简单的解决方案。

测试设置

根据您的描述 - 您可以为我们做到这一点:

CREATE TABLE table1 (
   entry_id int4 PRIMARY KEY
 , entry_date date
);

CREATE TABLE table2 (
   entry_id int4 REFERENCES table1 (entry_id)
 , entry_subject text
 , PRIMARY KEY (entry_id, entry_subject)
);

INSERT INTO table1 VALUES (1, '2011-09-01'), (2, '2011-09-02'),(3, '2011-09-03');
INSERT INTO table2 VALUES (1, 'foo1'), (2, 'foo2'), (2, 'bar2')
                        , (3, 'foo3'), (3, 'baz3'), (3, 'bar3');  

回答

string_agg() 需要 Postgres 9.0+

SELECT t1.entry_id, t1.entry_date
     , string_agg(t2.entry_subject, ', ') AS entry_subjects
FROM   table1 t1
JOIN   table2 t2 USING (entry_id)
GROUP  BY 1,2
ORDER  BY 1;

 entry_id | entry_date | entry_subjects
----------+------------+------------------
        1 | 2011-09-01 | foo1
        2 | 2011-09-02 | bar2, foo2
        3 | 2011-09-03 | baz3, bar3, foo3

或者,如果您希望 entry_subjects 排序

SELECT DISTINCT ON (1)
       t1.entry_id
     , t1.entry_date
     , string_agg(t2.entry_subject, ', ') OVER (
          PARTITION BY t1.entry_id ORDER BY t2.entry_subject
          RANGE BETWEEN UNBOUNDED PRECEDING
                    AND UNBOUNDED FOLLOWING) AS entry_subjects
  FROM table1 t1
  JOIN table2 t2 USING (entry_id)
  ORDER BY 1;

 entry_id | entry_date | entry_subjects
----------+------------+------------------
        1 | 2011-09-01 | foo1
        2 | 2011-09-02 | bar2, foo2
        3 | 2011-09-03 | bar3, baz3, foo3

您可以对table2 的子选择执行相同的操作到第一个ORDER BY entry_subject

【讨论】:

  • string_agg函数是postgres 9.0才引入的,我有一个更早的版本。然而,这让我想到了使用 array_to_string 和 array_agg 的组合来做与 string_agg 相同的事情。非常感谢您的帮助!
  • 是的,string_agg() 只是 array_to_string(array_agg(..)) 的一个(非常方便的)快捷方式
猜你喜欢
  • 1970-01-01
  • 2012-05-11
  • 2011-10-25
  • 1970-01-01
  • 2020-05-25
  • 1970-01-01
  • 2015-10-06
  • 2017-09-09
  • 2017-12-24
相关资源
最近更新 更多