简体   繁体   English

给定组的列的Postgres串联

[英]Postgres concatenation of a column for a given group

I currently have a Postgres DB with a SHOWS table that looks like this: 我目前有一个带有SHOWS表的Postgres DB,如下所示:

id      date        venue                       price  is_sold  ages    time    multi_day   pit
13      2016-01-02  924 Gilman Street, Berkeley $8      FALSE    a/a     7:30pm  FALSE       FALSE

I have a query that orders everything in this table by date (which is of timestamptz type): 我有一个查询,按日期(属于timestamptz类型)对该表中的所有内容进行timestamptz

SELECT shows.* FROM shows ORDER BY date

Right now, running this query with the pg-promise library produces an array of objects that look like this: 现在,使用pg-promise库运行此查询将生成一个对象数组,如下所示:

[   
    { id: 1,
    date: Thu Oct 20 2016 17:00:00 GMT-0700 (PDT),
    venue: 'Regency Ballroom, S.F.',
    price: null,
    is_sold: false,
    ages: 'a/a',
    time: null,
    multi_day: false,
    pit: false },
  { id: 2,
    date: Thu Oct 20 2016 17:00:00 GMT-0700 (PDT),
    venue: 'Great American Music Hall.',
    price: null,
    is_sold: false,
    ages: 'a/a',
    time: null,
    multi_day: false,
    pit: false } ... ]

I want to return an array that groups this data by date column. 我想返回一个按date列对数据进行分组的数组。 Example output: 输出示例:

[ 
    {date: Thu Oct 20 2016 17:00:00 GMT-0700 (PDT),
     shows: [1, 2]}, // or however this should be structured; I just want a way to get multiple show ids from a single date.
    ...
]

which I assume I could then INNER JOIN to get the rest of the show details that I need. 我想我可以然后加入INNER JOIN以获得我需要的其余表演详细信息。

How do I adjust my query to return this sort of grouping? 如何调整查询以返回此类分组?

Just to answer the question, use 只是为了回答问题,使用

SELECT date, string_agg(id, ', ') AS show_ids
FROM shows
GROUP BY date

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM