从包含类似外键值的行中查找最低值

时间:2022-07-09 04:37:26

Table:
pk   fk   price
1      1      23
2      1      12
3      1      3
4      2      53
5      2      75
6      3      95
7      3      113
8      3      63
9      3      73
10     3      93
11     4      113
11     4      150
11     4      105

数据字典 - 表:pk fk price 1 1 23 2 1 12 3 1 3 4 2 53 5 2 75 6 3 95 7 3 113 8 3 63 9 3 73 10 3 93 11 4 113 11 4 150 11 4 105


In the above table: How to find out the lowest price based on it's common fk value.

For example: lowest price for fk=1 is 3, for fk=2 is 53, for fk=3 is 63 and for fk=4 is 105.

在上表中:如何根据常见的fk值找出最低价格。例如:fk = 1的最低价格是3,fk = 2是53,fk = 3是63,fk = 4是105。

I want a single SQL statement which could find lowest price for each common fk value.

我想要一个SQL语句,可以找到每个常见fk值的最低价格。

1 个解决方案

#1


You just want a basic aggregate per group.

您只需要每组基本聚合。

select fk, min(price)
  from your_table
  group by fk;

#1


You just want a basic aggregate per group.

您只需要每组基本聚合。

select fk, min(price)
  from your_table
  group by fk;