PostgreSQL中的SQL MIN(值)匹配行 [英] SQL MIN(value) matching row in PostgreSQL

查看:321
本文介绍了PostgreSQL中的SQL MIN(值)匹配行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下表格:

表A:

 ID          ID NAME      PRICE         CODE      
 00001          B         1000          1       
 00002          A         2000          1       
 00003          C         3000          1       

这是我使用的SQL:

Select Min (ID),
       Min (ID NAME),
       Sum(PRICE)
From A
GROUP BY CODE

这是我得到的:

ID         ID NAME     PRICE         
00001       A          6000   

如您所见,ID NAME与最小行值不匹配。我需要它们匹配。

As you can see, ID NAME don't match up with the min row value. I need them to match up.

我希望查询返回以下内容

I would like the query to return the following

ID         ID NAME     PRICE         
00001       B          6000  

我可以使用什么SQL要获得该结果?

What SQL can I use to get that result?

推荐答案

您还可以使用子查询

select t1.*,t2.* from
(select ID,Name from t where ID= (select min(ID) from t)
) as t1
cross join (select sum(Price) as total from t) as t2

https://dbfiddle.uk/?rdbms=postgres_10&fiddle=a496232b552390a641c0e5c0fae791 >

https://dbfiddle.uk/?rdbms=postgres_10&fiddle=a496232b552390a641c0e5c0fae791d1

id  name    total
1    B      6000

这篇关于PostgreSQL中的SQL MIN(值)匹配行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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