0
相关联的值假设我有下面的表:SQL:拉动与骨料
ID1,ID2,价值
对于每个ID1,可以有多于一个的ID2和对应的值,即:
a,1,5
a,2,6
a,3,7
。
Edit: Simpler version of ask:
How can I pull ID1, Max(ID2), Value without having to group on value (i want
to pull the value that corresponds to the max(id2) and without having to do a second join.
我试图找出一种方法来提供以下内容: ID1,闵(ID2),马克斯(ID2),以及用最小/最大ID2相关联的值:
一,1,3,5,7
我想出的唯一办法是沿着线的东西:
select
a.id1, a.min_id2, a.max_id2, b.value as min_value, c.value as max_value
from (select id1, min(id2) as min_id2, max(id2) as max_id2) from table group by 1) a
left outer join (select id1, id2, value from table) b on a.min_id2 = b.id2
left outer join (select id1, id2, value from table) c on a.max_id2 = c.id2
这是一个假设的例子,但在我的数据运行此需要的非常长的时间我。希望可能有某种我不知道的快捷方式。