2017-03-31 9 views
0

のGROUP BYと組み合わせる:CouchbaseのN1QLサブ選択

{ 
    "offerIdentifier": "123", 
    "flyFrom": "HAM", 
    "flyTo": "LGW", 
    "provider": "LH", 
    "price": 207, 
    "bookingUrl": "https://example.com/", 
    "flightCombinationIdentifier": "HAM-LGW-201791570-20179171835" 
} 

flightCombinationIdentifierプロパティの値は、複数のオファーに表示されますすることができます。

は、今私はflightCombinationIdentifierによってグループに好きで、理想的に、このような構造を生じるはずである。この組み合わせで使用可能な最低価格を見つけるだろう:

{ 
    "offerIdentifier": "456", 
    "flightCombinationIdentifier": "HAM-LGW-201791570-20179171835", 
    "offer": [ 
    { 
     "bookingUrl": "http://example.com/", 
     "price": 97, 
     "provider": "LH" 
    } 
    ] 
} 

だから私は、次のN1QLクエリを思い付いた:

select b.flightCombinationIdentifier, 
    (
     select b1.price, b1.provider, b1.bookingUrl from bucket b1 
     use keys b.offerIdentifier 
     where b1.flightCombinationIdentifier = b.flightCombinationIdentifier 
     order by b1.price asc 
     limit 1 
    ) as offer 

from bucket b 
where b.flyFrom = 'HAM' and b.flyTo = 'LGW' 
group by b.flightCombinationIdentifier 

残念ながら、それは次のエラーで死ぬ:

[ 
    { 
    "code": 4210, 
    "msg": "Expression must be a group key or aggregate: (select (`b1`.`price`), (`b1`.`provider`), (`b1`.`bookingUrl`) from `bucket` as `b1` use keys (`b`.`offerIdentifier`) where ((`b1`.`flightCombinationIdentifier`) = (`b`.`flightCombinationIdentifier`))) order by (`b1`.`price`) limit 1) as `offer`", 
    "query_from_user": "select b.flightCombinationIdentifier,\n (\n  select b1.price, b1.provider, b1.bookingUrl from bucket b1\n  use keys b.offerIdentifier\n  where b1.flightCombinationIdentifier = b.flightCombinationIdentifier\n  order by b1.price asc\n  limit 1\n ) as offer\n \nfrom bucket w\nwhere b.flyFrom = 'HAM' and b.flyTo = 'LGW'\ngroup by b.flightCombinationIdentifier" 
    } 
] 

サブクエリの結果を結果オブジェクトに取り込む正しい方法は何でしょうか?

答えて

2
SELECT flightCombinationIdentifier, MIN([price, {bookingUrl,price,provider}])[1] AS offer 
FROM bucket WHERE flyFrom = 'HAM' AND flyTo = 'LGW' 
GROUP BY flightCombinationIdentifier;