基于查询couchbase

时间:2017-03-23 05:39:55

标签: json couchbase n1ql

我可以使用查询在couchbase中检索以下数据。

选择不同的SupplierName,Currency,CountryOfOrigin     来自RangePlan,其中type =“Supplier”

[{      “CountryOfOrigin”:“中国”,      “货币:美元”,      “supplierName”:“abc”     },

{      “CountryOfOrigin”:“中国”,      “货币:美元”,      “supplierName”:“bcd”       },

{     “CountryOfOrigin”:“印度”,      “货币:美元”,      “supplierName”:“hij”     },

{      “CountryOfOrigin”:“印度”,      “货币:美元”,      “supplierName”:“klm”     }]

现在我需要帮助来获取基于原产国的数据,如下所示。

{ 
 "china" : [ 
  { 
    "supplierName" : "abc", 
    "currency" : "USD" 
  } ,
  { 
    "supplierName" : "bcd", 
    "currency" : "USD" 
  } 
] 
 } 
  "india" : [ 
{ 
    "supplierName" : "hij", 
    "currency" : "INR" 
  } ,
  { 
    "supplierName" : "klm", 
    "currency" : "INR" 
  }     
  ] 
}

我需要查询上面的输出。

谢谢, Emraan

1 个答案:

答案 0 :(得分:1)

SELECT RAW OBJECT v.CountryOfOrigin:v.val FOR v IN              (SELECT q.CountryOfOrigin,ARRAY_AGG({“currency”:q.Currency,“supplierName”:q.SupplierName})val FROM                    (从RangePlan中选择DISTINCT SupplierName,Currency,CountryOfOrigin WHERE type =“Supplier”)作为q              GROUP BY q.CountryOfOrigin)END;