sql - Ignoring null values in in a postgresql rank() window function -
i writing sql query using postgresql needs rank people "arrive" @ location. not arrives however. using rank()
window function generate arrival ranks, in places arrival time null, rather returning null rank, rank()
aggregate function treats them if arrived after else. want happen these no-shows rank of null
instead of imputed rank.
here example. suppose have table dinner_show_up
looks this:
| person | arrival_time | restaurant | +--------+--------------+------------+ | dave | 7 | in_and_out | | mike | 2 | in_and_out | | bob | null | in_and_out |
bob never shows up. query i'm writing be:
select person, rank() on (partition restaurant order arrival_time asc) arrival_rank dinner_show_up;
and result be
| person | arrival_rank | +--------+--------------+ | dave | 2 | | mike | 1 | | bob | 3 |
what want happen instead this:
| person | arrival_rank | +--------+--------------+ | dave | 2 | | mike | 1 | | bob | null |
just use case
statement around rank()
:
select person, (case when arrival_time not null rank() on (partition restaurant order arrival_time asc) end) arrival_rank dinner_show_up;
Comments
Post a Comment