Sql 根据列值从select返回记录。神谕
所以我确实有这样的选择: 从数据库1中选择*,其中ID=3933185 witch select返回给我的记录包括:Sql 根据列值从select返回记录。神谕,sql,oracle,select,greatest-n-per-group,window-functions,Sql,Oracle,Select,Greatest N Per Group,Window Functions,所以我确实有这样的选择: 从数据库1中选择*,其中ID=3933185 witch select返回给我的记录包括: ID VALUE ATTR_VALUE 3,933,185 1 1 3,933,185 1 1 3,933,185 1 1 3,933,185 1 2 3,933,185 1 2 正如您可以看到的,每个attr_value列可能有不同的值
ID VALUE ATTR_VALUE
3,933,185 1 1
3,933,185 1 1
3,933,185 1 1
3,933,185 1 2
3,933,185 1 2
正如您可以看到的,每个attr_value列可能有不同的值,1或2,仅此而已
所以我应该添加什么来进行检查,当attr_值与值1一起存在时,它将返回attr_值=1的记录,在其他情况下,它将返回attr_值=2的记录
希望我的问题清楚。我想你想要:
select t.*
from t
where t.value = (select min(t2.value) from t t2 where t2.id = t.id);
还可以使用分析函数:
select t.*
from (select t.*, min(t.value) over (partition by id) as min_value
from t
) t
where value = min_value;
select id, value, attr_value
from (select t.*, rank() over(order by attr_value) rn from mytable t) t
where rn = 1
您可以在此处使用exists逻辑:
SELECT d1.*
FROM database1 d1
WHERE NOT EXISTS (SELECT 1 FROM database1 d2
WHERE d1.ID = d2.ID AND d2.ATTR_VALUE > d1.ATTR_VALUE);
如果我没有弄错,您可以使用分析函数:
select t.*
from (select t.*, min(t.value) over (partition by id) as min_value
from t
) t
where value = min_value;
select id, value, attr_value
from (select t.*, rank() over(order by attr_value) rn from mytable t) t
where rn = 1