【发布时间】:2019-11-19 15:03:50
【问题描述】:
我想要一个窗口函数来根据它们的值对月份进行排名。所以在这个例子中,2018-12 是排名 1,2019-01 是 2,等等。
而且我还希望排名计数器在进入新队列后重置,在这种情况下,队列 2,排名应该再次从 1 开始,模式将类似于队列 1
SELECT *,
rank() over (partition by cohort, month order by month asc)
FROM (
SELECT 1 as cohort, id, date_trunc('month',start_date) as month
FROM _analysis.terms
WHERE holiday=FALSE and id >= 125
UNION SELECT 2, id, date_trunc('month', start_date) FROM _analysis.terms
WHERE holiday=FALSE and id >= 126
ORDER BY cohort, id, month
)
ORDER BY cohort, id, month
【问题讨论】:
标签: sql amazon-redshift window-functions