如何查询只出现特定列中具有最高值的行的行?
How to query rows where only the rows with the highest value in a specific column appear?
抱歉,如果我的措辞令人困惑,请自学PL/SQL。我正在尝试根据一列查询具有最高值的行中的所有列。
示例:我有一个包含三行三列的 table
Table:PTest
Ptest_no | Test_id | Test_inst
------------------------------
ABC11 | 1 | 1
ABC11 | 2 | 1
ABC11 | 2 | 2
我只需要得到顶行和底行及其所有列(最终 table 将有近 10 多列)
结果:
ABC11 | 1 | 1
ABC11 | 2 | 2
我试过了:
--但它只打印第 3 行。
select * from ptest
where test_inst = (select max(test_inst) from ptest);
-- 尝试自连接认为子查询可以帮助指定条件。
--但只打印第三行
select a.Ptest_no, a.test_id, a.test_inst
from PTest a
join (select max(test_inst) as max_insty
from PTest b
where PTest_no = 'ABC11') on max_insty = a.test_inst
where PTest_no = 'ABC11';
--导致无效的关系运算符。
--我不确定那是什么意思。
select test_inst, ptest_no, test_id
from ptest
group by test_inst, ptest_no, test_id having max(test_inst);
目前正在尝试:
- 再次尝试使用 self join 但使用 CASE,在使用 CASE 时遇到困难并且不确定如何正确结束它是否是最佳路线。注释掉 case 和 运行,只打印第 3 行
- 在所有行上添加值为“69”的第 4 行名称 ptest_snu。不确定我为什么这样做。
select a.Ptest_no, a.test_id, a.test_inst, a.ptest_snu
from PTest a
--case
--when a.test_id = b.test_id then select max(test_inst)
--else (select * from Ptest a) end
join (select max(test_inst) as max_insty
from PTest b
where PTest_no = 'ABC11') on max_insty = a.test_inst
where a.ptest_snu = '69';
我怀疑您想要每个 test_id
具有最大 test_inst
的行。如果是,这是一个 greatest-n-per-group 问题;一种选择是使用相关子查询进行过滤:
select t.*
from ptest t
where t.test_inst = (
select max(t1.test_inst) from ptest t1 where t1.test_id = t1.test_id
)
您还可以使用 window 函数:
select *
from (
select t.*, row_number() over(partition by test_id order by test_inst desc) rn
from ptest t
) t
where rn = 1
我认为这会 return 您想要的结果:
select * from ptest
where
(
test_inst = (select max(test_inst) from ptest)
and
test_id = (select max(test_id) from ptest)
)
or
(
test_inst = (select min(test_inst) from ptest)
and
test_id = (select min(test_id) from ptest)
)
所以两列都必须等于这些列中的最高值或最低值。不只是其中之一。
抱歉,如果我的措辞令人困惑,请自学PL/SQL。我正在尝试根据一列查询具有最高值的行中的所有列。
示例:我有一个包含三行三列的 table Table:PTest
Ptest_no | Test_id | Test_inst
------------------------------
ABC11 | 1 | 1
ABC11 | 2 | 1
ABC11 | 2 | 2
我只需要得到顶行和底行及其所有列(最终 table 将有近 10 多列)
结果:
ABC11 | 1 | 1
ABC11 | 2 | 2
我试过了:
--但它只打印第 3 行。
select * from ptest
where test_inst = (select max(test_inst) from ptest);
-- 尝试自连接认为子查询可以帮助指定条件。 --但只打印第三行
select a.Ptest_no, a.test_id, a.test_inst
from PTest a
join (select max(test_inst) as max_insty
from PTest b
where PTest_no = 'ABC11') on max_insty = a.test_inst
where PTest_no = 'ABC11';
--导致无效的关系运算符。 --我不确定那是什么意思。
select test_inst, ptest_no, test_id
from ptest
group by test_inst, ptest_no, test_id having max(test_inst);
目前正在尝试: - 再次尝试使用 self join 但使用 CASE,在使用 CASE 时遇到困难并且不确定如何正确结束它是否是最佳路线。注释掉 case 和 运行,只打印第 3 行 - 在所有行上添加值为“69”的第 4 行名称 ptest_snu。不确定我为什么这样做。
select a.Ptest_no, a.test_id, a.test_inst, a.ptest_snu
from PTest a
--case
--when a.test_id = b.test_id then select max(test_inst)
--else (select * from Ptest a) end
join (select max(test_inst) as max_insty
from PTest b
where PTest_no = 'ABC11') on max_insty = a.test_inst
where a.ptest_snu = '69';
我怀疑您想要每个 test_id
具有最大 test_inst
的行。如果是,这是一个 greatest-n-per-group 问题;一种选择是使用相关子查询进行过滤:
select t.*
from ptest t
where t.test_inst = (
select max(t1.test_inst) from ptest t1 where t1.test_id = t1.test_id
)
您还可以使用 window 函数:
select *
from (
select t.*, row_number() over(partition by test_id order by test_inst desc) rn
from ptest t
) t
where rn = 1
我认为这会 return 您想要的结果:
select * from ptest
where
(
test_inst = (select max(test_inst) from ptest)
and
test_id = (select max(test_id) from ptest)
)
or
(
test_inst = (select min(test_inst) from ptest)
and
test_id = (select min(test_id) from ptest)
)
所以两列都必须等于这些列中的最高值或最低值。不只是其中之一。