我試圖對跨兩列具有保存值的行進行分組,並根據第三列對結果進行排序/排序。配置單元 - 根據某些列選擇唯一行
結果應包含所有其他列。
對於表:
with sample as (
select 'A' as c1, 'B' as c2, '22:00' as c3, 'Da' as c4
union all
select 'A' as c1, 'B' as c2, '23:00' as c3, 'Db' as c4
union all
select 'A' as c1, 'B' as c2, '09:00' as c3, 'Dc' as c4
union all
select 'A' as c1, 'C' as c2, '22:00' as c3, 'Dd' as c4
union all
select 'B' as c1, 'C' as c2, '09:00' as c3, 'De' as c4
)
分組或過濾通過柱C1和C2由時間上C3排名,輸出將是:
row_number() over (partition by c1, c2 order by c3) as rnk
| c1, c2, c3, c4, rnk|
-----------------------
| A | B |09:00| Dc| 1 |
| A | B |22:00| Da| 2 |
| A | B |23:00| Db| 3 |
| A | C |22:00| Dd| 1 |
| B | C |09:00| De| 1 |
所有其他像c4,c5 ..這樣的列應該保留,但對組標準或等級沒有任何影響。
一相信,隨着分區上C1和C2和秩序的窗口功能被C3可以工作,但不知道這是否是在非常大的表的情況下,最好的辦法,並通過多個列中的必要組。
最終輸出將是排名爲1(頂部)的UNIQUE行。這些列應該與樣本表格(無級別)完全相同。
Select * from tableX where rnk = 1
會做的工作,但保持專業'rnk'。 我想避免寫中的所有列,不包括的rnk。
| c1, c2, c3, c4 |
-------------------
| A | B |09:00| Dc|
| A | C |22:00| Dd|
| B | C |09:00| De|
*編輯,加決賽桌
'rnk'作爲'row_number'的別名不是一個好的選擇,因爲還有一個'rank'窗口函數,具有不同的含義 –