postgresql:如何从group by子句中获取主键?
这是一个选择一组所需行的查询:
select max(a), b, c, d, e
from T
group by b, c, d, e;
该表有一个主键,位于列 id
中。
我想通过从每一行获取主键来在进一步的查询中识别这些行。我该怎么做呢?这不起作用:
select id, max(a), b, c, d, e
from T
group by b, c, d, e;
ERROR: column "T.id" must appear in the GROUP BY clause or be used in an aggregate function
我通过研究其他一些 postgresql 问题尝试过这个,但没有运气:
select distinct on (id) id, max(a), b, c, d, e
from T
group by b, c, d, e;
ERROR: column "T.id" must appear in the GROUP BY clause or be used in an aggregate function
我该怎么办?我知道每个结果只能有一个 id,因为它是主键...我确实想要主键以及其余数据,对于初始(工作)的每一行查询返回。
This is a query which selects a set of desired rows:
select max(a), b, c, d, e
from T
group by b, c, d, e;
The table has a primary key, in column id
.
I would like to identify these rows in a further query, by getting the primary key from each of those rows. How would I do that? This does not work:
select id, max(a), b, c, d, e
from T
group by b, c, d, e;
ERROR: column "T.id" must appear in the GROUP BY clause or be used in an aggregate function
I have tried this from poking around in some other postgresql questions, but no luck:
select distinct on (id) id, max(a), b, c, d, e
from T
group by b, c, d, e;
ERROR: column "T.id" must appear in the GROUP BY clause or be used in an aggregate function
What do I do? I know there can only be one id
for each result, cause it's a primary key... I literally want the primary key along with the rest of the data, for each row that the initial (working) query returns.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
只是好奇将其添加到 group by 子句是否有效?
juuust curious does adding it to the group by clause work?
使用原始查询作为子查询,并使用这些结果连接回原始表以查找 id。
Use your original query as a subquery, and use those results to join back to the original table to find the id.
这不是你问的,但我怀疑你想要做的是获取与由其他几列定义的每个组的一列的最大值相对应的单行。例如,查找全年最热的星期一/星期二等的一天。
我发现执行此操作的最佳方法是使用视图来查找组的所有最大值。如果您的原始表
创建“最大”视图
(这是您的初始查询),然后将该视图加入到您的表中以获取具有最大值的行:
It's not what you asked, but I suspect what you are trying to do is to get the single row corresponding to the maximum value of one column for each group defined by several other columns. For example, to find the day that was the hottest monday/tuesday/etc for an entire year.
The best way I've found to do this uses a view to find all the maximum values for the groups. If your original table is
then create the "max" view as
(which is your initial query) and then join that view to your table to get rows with max values:
由于您分组这一事实,每个返回的记录可能(并且很可能)有多个匹配记录(例如,多个
id
值)。PostgreSQL 非常严格——它不会猜测你的意思。
b,c,d,e
运行另一个查询array_agg
分组函数来获取id< 的数组/code> 每条记录的值。
请参阅此问题:Postgresql GROUP_CONCAT 等效项?
我建议您将#3 视为最有效的的可能性。
希望这有帮助。谢谢!
By virtue of the fact that you are grouping, there can (and will likely) be more than one matched record (eg, more than one
id
value) per returned record.PostgreSQL is pretty strict - it will not guess at what you mean.
b,c,d,e
array_agg
grouping function to get an array ofid
values per record.See this question: Postgresql GROUP_CONCAT equivalent?
I suggest you consider #3 as the most efficient of the possibilities.
Hope this helps. Thanks!
如果您不关心获得哪个
id
,那么您只需将您的id
包装在某个聚合函数中,该函数保证为您提供有效的id.我想到了 max 和 min 聚合:
根据您的数据,我认为使用窗口函数将是一个更好的计划(感谢邪恶的奥托引导到头部) :
If you don't care which
id
you get then you just need to wrap yourid
in some aggregate function that is guaranteed to give you a validid
. Themax
andmin
aggregates come to mind:Depending on your data I think using a window function would be a better plan (thanks to evil otto for the boot to the head):