Just for curiosity, if I have to use rank
to tag distinct values for some columns only, and ignore whatever different values occurred on other columns, why not use distinct
and just remove the other columns? Any real life examples?
SELECT * FROM (
SELECT
col1,
col2,
col3,
col4,
row_number () over (partition by col1, col2 ORDER BY col3) RN
FROM table) tmp_table
WHERE rn = 1
vs.
SELECT DISTINCT
col1,
col2
FROM table
When ordered by col3
, there is a chance you can get different values of col3
compared to rank 2, right? Then if that col3
is not important because it is not consistent, why not use distinct
instead for only col1
and col2
?