SELECT * FROM (
SELECT *, row_number() OVER(PARTITION BY grank order by ts) as global_id FROM (
SELECT *, row_number() OVER(PARTITION BY id, type, ts, dataset order by ts) as grank FROM (
SELECT ---
UNION ALL
SELECT ---
) gen
) general
) global
WHERE ts > 1501786799999
ORDER BY ts, dataset, type LIMIT 50;
第三行的PARTITION BY 后面的条件能唯一确定一条记录, order by 任意(看实际业务), 这时候 所有的grank都只有1(每组只有提条记录), 再PARTITION BY grank 所有的都会在一个组 然后rank就刚好可以当序号
网友评论