根据上次更新(createdOn)和按ID分组的Bigquery查询输出以下数据

时间:2016-03-15 10:02:49

标签: google-bigquery

Bigquery表记录

[
  {
    "testing": "test",
    "test1_test2_test3": "test1",
    "test4": "test",
    "id": "56e7a237ea7b45950e90ac4c",
    "createdOn": "48172-11-04 03:32:29 UTC"
  },
  {
    "testing": "abc",
    "test1_test2_test3": "bcd",
    "test4": "efg",
    "id": "56e79ee81f4394d70dcc8bec",
    "createdOn": "48172-10-25 08:31:05 UTC"
  },
  {
    "testing": "fgter",
    "test1_test2_test3": "yu67",
    "test4": "testingdata",
    "id": "56e79ee81f4394d70dcc8bec",
    "createdOn": "48172-08-25 08:31:05 UTC"
  },
  {
    "testing": "abc",
    "test1_test2_test3": "bcd",
    "test4": "efg",
    "id": "56e79ee81f4394d70dcc8bec",
    "createdOn": "48172-12-11 11:06:11 UTC"
  },
  {
    "testing": "abcd",
    "test1_test2_test3": "bcde",
    "test4": "abcd",
    "id": "56e7ac9cf287b34510b0a6a9",
    "createdOn": "48172-12-04 23:07:55 UTC"
  }
]

我尝试使用Or Top分组,但没有以下面的格式输出

[
  {
    "testing": "test",
    "test1_test2_test3": "test1",
    "test4": "test",
    "id": "56e7a237ea7b45950e90ac4c",
    "createdOn": "48172-11-04 03:32:29 UTC"
  },
   {
    "testing": "abc",
    "test1_test2_test3": "bcd",
    "test4": "efg",
    "id": "56e79ee81f4394d70dcc8bec",
    "createdOn": "48172-12-11 11:06:11 UTC"
  },
  {
    "testing": "abcd",
    "test1_test2_test3": "bcde",
    "test4": "abcd",
    "id": "56e7ac9cf287b34510b0a6a9",
    "createdOn": "48172-12-04 23:07:55 UTC"
  }
]

1 个答案:

答案 0 :(得分:1)

你是说这个吗?

select
testing
,test1_test2_test3
,test4
,id
,createdOn

from (
select
testing
,test1_test2_test3
,test4
,id
,createdOn
,row_number() over (partition by id order by createdOn desc) as rank

from table
)

where rank = 1