如何从一个表中选择 MIN 和 MAX 并从另一个表中选择它们的相关名称?

时间:2021-05-31 12:57:43

标签: sql oracle

Aproduct_idproduct_name

Bproduct_id 及其相关的 values

如何选择 product_name(来自表 A)和 minmax 的值(来自表 B)?

预期结果:

enter image description here

2 个答案:

答案 0 :(得分:0)

你只想要joingroup by吗?

select a.product_id, a.product_name, min(b.value), max(b.value)
from a join
     b
     on a.product_id = b.product_id
group by a.product_id, a.product_name;

答案 1 :(得分:0)

select product_name, MAX(value) as Maximal, MIN(value) as Minimal from a, b
where a.product_id = b.product_id 
group by product_name