测试运行_id | 测试ID | suite_id | exe_长度 | 留言 |
---|---|---|---|---|
4201 | 32 | 39 | 11 | 集线器状态确实1 |
4201 | 32 | 39 | 17 | 集线器状态确实3 |
4201 | 32 | 39 | 17 | 集线器状态确实4 |
4201 | 32 | 39 | 18 | 集线器状态确实7 |
4200 | 32 | 39 | 11 | 集线器状态确实8 |
4197 | 32 | 39 | 11 | 集线器状态为0 |
4196 | 32 | 39 | 17 | 集线器状态不14 |
4196 | 32 | 39 | 18 | 集线器状态确实134 |
4195 | 空 | 31 | 14 | 集线器状态确实123 |
4187 | 7 | 12 | 35 | 集线器状态 d34567 |
4187 | 7 | 12 | 35 | 集线器状态345566 |
我有上面的表格,如何为每个匹配的
test_run_id
、消息的test_run_id
和suite_id
选择最小长度?
示例: 对于
test_run_id=4201
、test_id=32
、suite_id=39
,我想要 message
,其中 exe_length
最小 (11)。即 message = "Hub Status does1"
当列值被硬编码时,这很容易:
select message
from log_message_view_uat
where test_run_id= 4201 and test_id=32 and suite_id=39 and exe_length = (select min(exe_length) from log_message_view_uat
where test_run_id= 4201 and test_id=32 and suite_id=39)
如何在数据库中的每列参数中获取相同的消息?
如果每组只需要一个(即使是平局),请使用
row_number
窗口函数。如果您想保留领带,请使用 dense_rank
。
--if wanting just one even if ties
select
test_run_id,
test_id,
suite_id,
exe_length,
message
from (
select *,
row_number() over (partition by test_run_id, test_id, suite_id order by exe_length) as rn
from log_message_view_uat
)z
where rn = 1
order by 1
测试运行_id | 测试ID | suite_id | exe_长度 | 留言 |
---|---|---|---|---|
4187 | 7 | 12 | 35 | 集线器状态345566 |
4195 | 空 | 31 | 14 | 集线器状态确实123 |
4196 | 32 | 39 | 17 | 集线器状态不14 |
4197 | 32 | 39 | 11 | 集线器状态为0 |
4200 | 32 | 39 | 11 | 集线器状态确实8 |
4201 | 32 | 39 | 11 | 集线器状态确实1 |
--if wanting ties
select
test_run_id,
test_id,
suite_id,
exe_length,
message
from (
select *,
dense_rank() over (partition by test_run_id, test_id, suite_id order by exe_length) as rn
from log_message_view_uat
)z
where rn = 1
order by 1
测试运行_id | 测试ID | suite_id | exe_长度 | 留言 |
---|---|---|---|---|
4187 | 7 | 12 | 35 | 集线器状态345566 |
4187 | 7 | 12 | 35 | 集线器状态 d34567 |
4195 | 空 | 31 | 14 | 集线器状态确实123 |
4196 | 32 | 39 | 17 | 集线器状态不14 |
4197 | 32 | 39 | 11 | 集线器状态为0 |
4200 | 32 | 39 | 11 | 集线器状态确实8 |
4201 | 32 | 39 | 11 | 集线器状态确实1 |
您可以在没有内部选择的情况下使用
first_value
来完成此操作
select
test_run_id,
test_id,
suite_id,
exe_length,
message,
first_value(message) over (partition by test_run_id, test_run_id, suite_id order by exe_length) as min_len
from log_message_view_uat
order by test_run_id desc
相关功能为:
first_value(message) -- we want the first value of a group of values
over (
partition by test_run_id, test_run_id, suite_id -- this works basically like a group by for the function without combining all the rows into one.
order by exe_length -- in order for first_value to make any sense we better define an order.
) as min_len
first_value