azure-cosmosdb-sqlapi

How do I get the latest record for each item in CosmosDB using SQL

微笑、不失礼 提交于 2021-02-16 15:36:29
问题 I have a schema which is similar to "id": "uuid", "deviceId": "uuid", "message": { "content": "string", "ts": 1 }, "data": { "temperature": 21 } I'd like to get the latest "data" (using message.ts as the timestamp) for each "deviceId". So far, I've managed to get the data back, in order of timestamp using the query SELECT c.deviceId, c.message.ts, c.data FROM c ORDER BY c.message.ts DESC but I can't figure out how to remove the duplicate device records. Is this possible to do within the

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

group data by same timestamp using cosmos db sql

牧云@^-^@ 提交于 2021-01-29 18:54:25
问题 Question: I am trying SQL query as the image showed below,I want it to be grouped by the same timestamp expected output: [ { "tag1": { "TagName": "PV1-input-power-L(10W)", "Value": 0 }, "tag2": { "TagName": "Sunshine-Display-Value", "Value": 0 }, "tag3": { "TagName": "TotalEnergy-(100kWh)_1", "Value": 0 }, "timestamp": "2020-03-27T02:40:18Z" } ] sample document: 回答1: You can use User Defined Functions. Here is the data from my containers Here is the function I have created. I named it

How to connect to azure SQL server using visual studio

∥☆過路亽.° 提交于 2021-01-29 08:46:02
问题 I have an azure function triggered by Cosmos DB. I am getting the data from the JSON file and send it over to my SQL database on Azure. I wanted to know if I can connect directly to the Azure SQL using my Visual Studio? I have connected to it once through the portal, but I can't see that I am connected to my database under View/Cloud Explorer in Visual Studio. The database is only listed under View/SQL Server Object Explorer . I assume this connection is through my local machine, and not

Substitute for having clause in CosmosDB

最后都变了- 提交于 2021-01-28 18:48:10
问题 I have the following result from a query, where count field is derived from an aggregate function [ { "count": 1, "facilityName": "Hyundai Service Center" }, { "count": 2, "facilityName": "Honda Service Center" }, { "count": 1, "facilityName": "Kat Service Center" } ] I want to display only those facilityName where count >= 2. How can we achieve this? 回答1: I tried to implement your requirement with Stored procedure,please refer to my SP code: function sample(idArray) { var collection =

Change Feed Consistency Level

淺唱寂寞╮ 提交于 2021-01-28 14:30:23
问题 Assuming we do not have strong consistency set, when using azure functions change feed are we guaranteed to get the latest document when querying against the same partition? Also, are all queries issues from within the change feed guaranteed the latest records since the change feed runs on the write region? Thanks! 回答1: You can read changefeed from any read region. Check the pull request https://github.com/Azure/azure-webjobs-sdk-extensions/pull/508. This code will be part of function soon.

Azure CosmosDB Query Explorer vs Data Explorer

久未见 提交于 2021-01-28 12:26:01
问题 I'm running the same query against my CosmosDB instance (using SQL API): SELECT c.partition, COUNT(1) AS total FROM c WHERE c.system = "SF" GROUP BY c.partition I'm a bit surprised that I'm getting expected results from Data Explorer while under Query Explorer tab I'm getting 400 Bad Request with below message: {"code":400,"body":"{\"code\":\"BadRequest\",\"message\":\"Message: {\\"Errors\\":[\\"Cross partition query only supports 'VALUE ' for aggregates.\\"]}\r\nActivityId: d8523615-c2ff

Why does including partition key in WHERE clause to Cosmos SQL API query increase consumed RUs for some queries?

守給你的承諾、 提交于 2021-01-28 09:51:03
问题 I would like to optimise my Azure Cosmos DB SQL API queries for consumed RUs (in part in order to reduce the frequency of 429 responses). Specifically I thought that including the partition key in WHERE clauses would decrease consumed RUs (e.g. I read https://docs.microsoft.com/en-us/azure/cosmos-db/optimize-cost-queries and https://docs.microsoft.com/en-us/azure/cosmos-db/partitioning-overview which made me think this). However, when I run SELECT TOP 1 * FROM c WHERE c.Field = "some value"

How do I define unique keys involving properties in embedded arrays in Azure Cosmos DB?

心已入冬 提交于 2021-01-28 08:25:45
问题 In Azure Cosmos DB, there is support for unique keys. Each unique key is defined as a set of paths representing values in the stored documents. An example of such a path would be /contact/firstName . It's not clear from the official docs (in fact it's not mentioned at all) how those paths apply down through embedded arrays within the document, or how unique key semantics apply when paths navigate into nested documents with a cardinality of more than one. For example, let's say I have a

How to split into Sub Documents with Nesting separator?

蹲街弑〆低调 提交于 2021-01-28 05:27:52
问题 I am migrating data from SQL to COSMOS DB through Azure cosmos DB Data Migration Tool Can someone please help help to migrate data in Sub-Documents, how to specify in nesting separator SELECT TOP 5 P.ProjectDocumentId, P.ProjectId, PU.UpdatedByFullName FROM [Docs].[ProjectDocuments] P INNER JOIN [Docs].[ProjectDocumentUpdate] PU ON P.ProjectDocumentID = PU.ProjectDocumentID WHERE P.ProjectDocumentId = '7DA0011B-7105-4B6C-AF13-12B5AC50B608' Result: Expected Document in Cosmos DB: {