在azure cosmos数据库(sql)中查询不同文档的最后数据

llycmphe  于 2021-07-24  发布在  Java
关注(0)|答案(1)|浏览(386)

样本数据:

{age: 20, ts: '00:00'},
{age: 20, ts: '00:01'},
{age: 30, ts: '00:00'},
{age: 30, ts: '00:01'},
{age: 40, ts: '00:00'},
{age: 40, ts: '00:01'},
{age: 40, ts: '00:02'}

预期产量:

[{age: 20, ts: '00:01'},
{age: 30, ts: '00:01'},
{age: 40, ts: '00:02'}]

尝试

SELECT * FROM c where c.age in (20, 30, 40) order by c.ts desc

但结果选择了所有的数据。

qeeaahzv

qeeaahzv1#

请尝试以下操作:

SELECT max(c.ts) as ts, c.age FROM c where c.age in (20, 30, 40) 
Group By c.age

这将产生以下输出:

[
    {
        "ts": "00:02",
        "age": 40
    },
    {
        "ts": "00:01",
        "age": 30
    },
    {
        "ts": "00:01",
        "age": 20
    }
]

相关问题