Query last data of different documents in Azure Cosmos db (SQL)

这一生的挚爱 提交于 2021-02-11 14:38:38

问题


Sample data :

{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'}

Expected output:

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

Tried

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

But the result selected all data.


回答1:


Please try something like the following:

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

This gives the following output:

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


来源:https://stackoverflow.com/questions/62527835/query-last-data-of-different-documents-in-azure-cosmos-db-sql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!