我有包含产品代码和数据更新日期的数据,我想对每个产品代码的行进行编号,以便我只能选择最新的更新。
我尝试了以下代码:
SELECT
ChangedDateTime,
ROW_NUMBER() OVER(PARTITION BY ProductCode, ChangedDateTime ORDER BY ChangedDateTime DESC) change_id,
ProductCode
FROM `dataset.table`
但是它返回:
Row ChangedDateTime change_id ProductCode
1 2019-06-06 08:08:01.510 UTC 1 A11
2 2019-06-05 08:08:01.510 UTC 2 A11
3 2019-06-04 16:02:13.087 UTC 1 A11
4 2019-06-05 08:08:01.510 UTC 1 A110
5 2019-06-04 16:02:13.087 UTC 1 A110
6 2019-06-04 14:36:54.930 UTC 1 A110
当我希望看到:L
Row ChangedDateTime change_id ProductCode
1 2019-06-06 08:08:01.510 UTC 1 A11
2 2019-06-05 08:08:01.510 UTC 3 A11
3 2019-06-04 16:02:13.087 UTC 2 A11
4 2019-06-05 08:08:01.510 UTC 1 A110
5 2019-06-04 16:02:13.087 UTC 2 A110
6 2019-06-04 14:36:54.930 UTC 2 A110
如何调整代码以使其正常运行?我当前的分区在做什么以对这些数字进行错误编号?
答案 0 :(得分:0)
只需通过date
删除分区:
SELECT
ChangedDateTime,
ROW_NUMBER() OVER(PARTITION BY ProductCode ORDER BY ChangedDateTime DESC) change_id,
ProductCode
FROM `dataset.table`