聚合查询中的单个列并包含许多列 [英] Aggregate a single column in query with many columns

查看:107
本文介绍了聚合查询中的单个列并包含许多列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当查询中有许多其他列时,是否有适当的方法聚合单个列?

Is there a proper way to aggregate a single column when I have many other columns in the query?

我已经尝试了此答案,虽然有效,但是我的查询变得更加冗长.

I've tried this answer which works, but my query has become a lot more verbose.

我当前的查询如下:

SELECT t1.foo1, t1.foo2, t2.foo3, t2.foo4, string_agg(t3.aggregated_field, ', ')
FROM tbl1 t1
LEFT JOIN tbl2 t2 ON t1.id = t2.fkeyid
LEFT JOIN tbl3 t3 ON t2.id = t3.fkeyid
GROUP BY t1.foo1, t1.foo2, t2.foo3, t2.foo4, t2.foo5, t2.foo6
ORDER BY t2.foo5, t2.foo6

该查询还有更多的字段和LEFT JOIN,重要的部分是所有这些字段都具有1到1或1到0的关系,除了我要聚合的一个字段1到n之外,用在上面的伪查询中.

The query has many more fields and LEFT JOINs, the important part is that all these fields have 1 to 1 or 1 to 0 relationship except one field that is 1 to n which I want to aggregate, represented by t3.aggregated_field in the pseudo-query above.

当我使用聚合函数时,SELECTORDER BY中列出的所有字段都必须是聚合的或GROUP BY子句的一部分.这使我的查询比现在更加冗长.

As I'm using an aggregate function, all fields listed in the SELECT and ORDER BY must be either aggregated or part of the GROUP BY clause. This makes my query way more verbose than it already is.

也就是说,假设foo1是主键,则当重复此字段时,除aggregated_field以外的所有其他字段也都相同.我希望将这些重复的行作为具有聚合字段值的单行结果. (基本上是select distinct带有聚合列)

That is, assuming foo1 is a primary key, when this field is repeated, all others except aggregated_field are also equal. I want these repeated rows as a single row result with the aggregated field value. (basically a select distinct with an aggregated column)

是否有更好的方法来执行此操作(而不必将所有其他字段放在GROUP BY中),还是应该仅对后端中的结果集进行迭代,对每行从1到n的行执行查询?关系吗?

Is there a better way to do this (without having to put all other fields in the GROUP BY) or should I just iterate over the result set in my back-end executing a query for each row fetching this 1 to n relationship?

服务器正在运行PostgreSQL 9.1.9,更具体地说:

The server is running PostgreSQL 9.1.9, more specifically:

x86_64-unknown-linux-gnu上的PostgreSQL 9.1.9,由gcc(GCC)4.1.2 20080704(Red Hat 4.1.2-54)编译,64位

PostgreSQL 9.1.9 on x86_64-unknown-linux-gnu, compiled by gcc (GCC) 4.1.2 20080704 (Red Hat 4.1.2-54), 64-bit

推荐答案

简单查询

对于PostgreSQL 9.1或更高版本,这可能更简单.如这个紧密相关的答案所述:

Simple query

This can be much simpler with PostgreSQL 9.1 or later. As explained in this closely related answer:

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆