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 CustomArray.

function userDefinedFunction(input){
var obj={};
input.forEach(function(element,index){ 
obj["tag"+index] = {
    TagName :element.TagName,
    Value: element.Value
};
}); return obj;}

Here, I run the UDF with my select statement

It returns the following data.

Schema is very close to what you are looking for. I think you can make it better by changing some jscript in UDF. I hope this helps!



来源:https://stackoverflow.com/questions/61055417/group-data-by-same-timestamp-using-cosmos-db-sql

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