2017-04-20 34 views
0
的分

我的JSON數據:DocumentDB得到子陣

[ 
    { 
    "Code": "GB-00001", 
    "BasicInformation": { 
     "WGS84Longitude": -4.670000, 
     "WGS84Latitude": 50.340000 
    }, 
    "Availability": [{ 
      "ArrivalDate": "2017-04-21", 
      "Price": 689 
     }, 
     { 
      "ArrivalDate": "2017-04-28", 
      "Price": 1341 
     } 
    ]}, 
    { 
    "Code": "GB-00002", 
    "BasicInformation": { 
     "WGS84Longitude": -4.680000, 
     "WGS84Latitude": 50.350000 
    }, 
    "Availability": [{ 
      "ArrivalDate": "2017-04-21", 
      "Price": 659 
     }, 
     { 
      "ArrivalDate": "2017-04-28", 
      "Price": 1440 
     } 
    ]} 
}] 

我希望得到的結果是這樣的:

[ 
{ 
    "HouseCode": "GB-00001", 
    "Country": "GB", 
    "location": { 
     "type": "Point", 
     "coordinates": [ 
      50.340000, 
      -4.670000 
     ] 
    }, "lowestPrice": 689 
}, 
{ 
    "HouseCode": "GB-00002", 
    "Country": "GB", 
    "location": { 
     "type": "Point", 
     "coordinates": [ 
      50.350000, 
      -4.680000 
     ] 
    }, "lowestPrice" : 659 
} 

我的問題是:如何使用min(c.Availability.Price)

這是我目前的查詢,經緯度轉換爲點,但不知道如何獲得最低/最低價格。

SELECT c.Code, c.BasicInformation.Country , 
    {"type":"Point","coordinates": [c.BasicInformation.Latitude, c.BasicInformation.Longitude]} as location 
FROM c 

已經嘗試過用Join c.Availability a, min(a.Price)

編輯也許是我太早了? https://feedback.azure.com/forums/263030-documentdb/suggestions/18561901-add-group-by-support-for-aggregate-functions 發現url https://stackoverflow.com/a/42697673/169714

回答

2

這對於用戶定義函數(UDF)來說非常接近理想情況。

這是一個應該做的伎倆:

function lowestPrice(availability) { 
    var i, len, lowest, row; 
    lowest = 2e308; 
    for (i = 0, len = availability.length; i < len; i++) { 
    row = availability[i]; 
    lowest = Math.min(lowest, row.Price); 
    } 
    return lowest; 
}; 

你這樣稱呼它:

SELECT 
    c.Code, 
    c.BasicInformation.Country, 
    {"type":"Point","coordinates": [ 
    c.BasicInformation.Latitude, c.BasicInformation.Longitude 
    ]} as location, 
    udf.lowestPrice(c.Availability) as lowestPrice 
FROM c 
+0

所以我不能使用min(a.Price),因爲在使用聚合函數時缺乏分組並且需要創建用戶定義的函數?我以前從未使用過udf,所以我會仔細研究它,並會回到此處。謝謝您的回答。編輯:墜毀http://imgur.com/a/hxoJ9 –

+0

屏幕截圖不是英文 –

+0

它只是一個常規的Windows消息。 DocumentDB.GatewayService.exe已停止工作。等等。 –

1

據我所知,你只能使用UDF來實現您的要求,現在。另外,我已經檢查由拉里Maccherone提供的代碼,它可以在Azure上DocumentDB服務,我DocumentDB仿真器(版本1.11.136.2)都工作如下:

enter image description here

DocumentDB.GatewayService.exe已停止工作

對於DocumentDB.GatewayService崩潰,我認爲您需要收集轉儲文件並用電子郵件將它們附加到[email protected]。有關更多詳細信息,請參閱DocumentDB Emulator troubleshooting

+0

謝謝!我已經郵寄給他們。 –