简体   繁体   English

在postgresql中如何使用group by并获取所有列

[英]In postgresql how to use group by and fetch all the columns

I am new in writing raw sql queries and not able to find the solution for my problem, this is the sample the data in my postgresql table- 我是新的编写原始SQL查询而无法找到我的问题的解决方案,这是我的postgresql表中的数据示例 -

              product_id              | stage |           added_on            | stage_id | plan_id | producer_id 
--------------------------------------+-------+-------------------------------+----------+---------+-------------
 a0361a6f-5cd0-4b3e-98ad-6be115983496 |    70 | 2017-03-11 13:36:52.21992+00  |     1463 |      60 |         137
 a0361a6f-5cd0-4b3e-98ad-6be115983496 |    60 | 2017-03-11 13:36:36.293494+00 |     1462 |      60 |         137
 a0361a6f-5cd0-4b3e-98ad-6be115983496 |    50 | 2017-03-11 13:36:31.923588+00 |     1461 |      60 |         137
 a0361a6f-5cd0-4b3e-98ad-6be115983496 |    40 | 2017-03-11 13:35:02.299077+00 |     1460 |      60 |         137
 a0361a6f-5cd0-4b3e-98ad-6be115983496 |    30 | 2017-03-11 13:34:53.541734+00 |     1459 |      60 |         137
 a0361a6f-5cd0-4b3e-98ad-6be115983496 |    20 | 2017-03-11 13:34:46.650811+00 |     1458 |      60 |         137
 a0361a6f-5cd0-4b3e-98ad-6be115983496 |    10 | 2017-03-11 13:34:20.866694+00 |     1457 |      60 |         137
 619c5d60-1815-47d5-9602-7faf355f953b |   -30 | 2017-03-11 13:34:10.968819+00 |     1456 |      60 |         137
 619c5d60-1815-47d5-9602-7faf355f953b |    10 | 2017-03-11 13:34:10.39307+00  |     1455 |      60 |         137

I want to fetch the result in such a way which can return me the first row and last row. 我想以这样的方式获取结果,这可以返回第一行和最后一行。 Means I want to lookup on most recent row group by product_id. 意味着我想通过product_id查找最近的行组。 I tried the below query- 我尝试了下面的查询 -

select product_id, max(added_on) from product_stages where producer_id = 137 group by product_id;

This gives me satisfying result but I want to select other columns as well so when I try below it gives me error. 这给了我满意的结果,但我想选择其他列,所以当我尝试下面它给我错误。

 select product_id, stage, stage_id, plan_id, max(added_on) from product_stages where producer_id = 137 group by product_id;

What is the good way to achieve this? 实现这一目标的好方法是什么? Thanks in Advance. 提前致谢。

Use DISTINCT ON instead of GROUP BY : 使用DISTINCT ON而不是GROUP BY

select distinct on (product_id) *
from product_stages 
where producer_id = 137 
order by product_id, added_on desc;

SELECT DISTINCT ON expressions must match initial ORDER BY expressions so product_id has to be the first column in ORDER BY . SELECT DISTINCT ON表达式必须与初始ORDER BY表达式匹配,因此product_id必须是ORDER BY的第一列。 However you can use a derived table to freely sort the results by other columns, eg: 但是,您可以使用派生表自由地按其他列对结果进行排序,例如:

select *
from (
    select distinct on (product_id) *
    from product_stages 
    where producer_id = 137 
    order by product_id, added_on desc
    ) s
order by added_on desc;

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

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