test_run_id | test_id | suite_id | exe_length | message |
---|---|---|---|---|
4201 | 32 | 39 | 11 | Hub Status does1 |
4201 | 32 | 39 | 17 | Hub Status does3 |
4201 | 32 | 39 | 17 | Hub Status does4 |
4201 | 32 | 39 | 18 | Hub Status does7 |
4200 | 32 | 39 | 11 | Hub Status does8 |
4197 | 32 | 39 | 11 | Hub Status does0 |
4196 | 32 | 39 | 17 | Hub Status doesn14 |
4196 | 32 | 39 | 18 | Hub Status does134 |
4195 | NULL | 31 | 14 | Hub Status does123 |
4187 | 7 | 12 | 35 | Hub Status d34567 |
4187 | 7 | 12 | 35 | Hub Status 345566 |
I have the table above, how can I select for each matching test_run_id
, the test_run_id
and suite_id
of the message with the minimum length?
Example:
for test_run_id=4201
, test_id=32
, suite_id=39
, I want the message
where exe_length
is minimum (11). i.e message = "Hub Status does1"
when the column values are hardcoded it’s easy:
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)
How can I get the same message per column parameter in the database?
4
Answers
Use
row_number
windows function if wanting just one per group (even if ties). If you want to keep the ties, then usedense_rank
.fiddle
You can do it without an inner select with
first_value
The relevant function is:
For more information about
first_value
See the DB-Fiddle
try using
GROUP BY
andHAVING
Distinct on
does exactly that: demo1,2,3
are column numbers, which means for each combination oftest_run_id
,test_id
andsuite_id
you get one row, and it will be the first one in the order ofexe_length
, ascending.If you only want the
message
, you’ll need explicit column names: