2017-03-31 9 views
0

에 의해 GROUP과 결합 :카우치베이스 주식회사 N1QL 하위 선택 I는 다음 비행을 제공 데이터 구조가 준

{ 
    "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 쿼리를 내놓았다 :

[ 
    { 
    "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" 
    } 
] 
,691 :
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 

불행하게도 그것은 다음과 같은 오류로 사망

하위 개체의 결과를 결과 개체로 가져 오는 올바른 방법은 무엇입니까?

답변

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