CosmosDB - return 列表,其中每个项目的前 1 名在具有许多记录的列表中

CosmosDB - return list with Top 1 of each item in a list with many records

我有以下 table:

Id | Plate | RecordedAt

对于每个不同的板块,可以有N条记录。

我正在努力解决以下问题:

 SELECT TOP 1 * 
 FROM table
 WHERE table.Plate IN ('plate1', 'plate2', 'plate3') 
 ORDER BY c.Recordedat DESC

问题是此查询 return 只有 1 条包含最后 Recordedat 项的记录。

我需要列表中每个项目的最后 Recordedat

有什么办法只用一个查询就可以做到这一点吗?

例如以下记录:

Id  |     Plate    | RecordedAt
 1  |   Plate1     | 2021/09/26 6:53:06
 3  |   Plate2     | 2021/09/25 7:45:10
 4  |   Plate3     | 2021/09/23 02:10:42
 5  |   Plate1     | 2021/09/25 02:40:02
 6  |   Plate2     | 2021/09/26 15:14:02
 7  |   Plate1     | 2021/09/26 01:02:04
 8  |   Plate3     | 2021/09/26 16:02:20 
 9  |   Plate1     | 2021/09/24 05:02:20
 10 |   Plate2     | 2021/09/24 04:03:02

我需要 return:

[
  {
    "Plate1":"2021/09/26 15:14:02"
  },
  {
    "Plate2":"2021/09/26 15:14:02"
  },
  {
    "Plate3":"2021/09/26 16:02:20"
  },
]

无法在 Cosmos DB 中执行此类查询。

您需要具体化每个板块的聚合或值,然后进行查询。

您可以使用GROUP BY在盘子上分组。然后您可以在其他列上使用聚合函数。在这种情况下,您需要 last/highest 日期,以便可以使用 MAX:

SELECT c.Plate, MAX(c.Recordedat) AS Recordedat
FROM c
WHERE c.Plate IN ('plate1', 'plate2', 'plate3') 
GROUP BY c.Plate