2016-11-07 4 views
0

xml에서 하이브 외부 테이블을 만듭니다. Timestamp가 최대 인 요소의 값을 가져 오려고합니다. 이 테이블 문 작성에 어떻게 이것을 작성합니까?일치하는 항목이 여러 개인 경우 최대 값을 가져 오는 Xpath

내 XML :

<Parent> 
    <Child> 
     <Purchase value ="100" id ="350" timestamp="2016-10-08T14:22:31.0000000"> 
    </Child> 
    <Child> 
     <Purchase value ="110" id ="350" timestamp="2016-10-08T14:22:32.0000000"> 
    </Child> 
    <Child> 
     <Purchase value ="105" id ="350" timestamp="2016-10-09T14:22:32.0000000"> 
    </Child> 
    <Child> 
     <Purchase value ="75" id ="350" timestamp="2016-10-10T14:22:32.0000000"> 
    </Child> 
</Parent> 

아래 쿼리는 나에게 모든 4 개 가격을 제공합니다. 하지만 가장 최근의 TimeStamp 가격 만 원하십니까? 하이브에서하는 방법?

CREATE EXTERNAL TABLE Recommended_StagingTable (

ItemPrice INT 
) 
ROW FORMAT SERDE 
    'com.ibm.spss.hive.serde2.xml.XmlSerDe' 
WITH SERDEPROPERTIES ( 
    "column.xpath.id" ="/Parent/Child/Purchase[@id='350']/@value" 
) 

답변

0

다음 Recommended_StagingTable에 purchase_timestamp 열을 추가 타임 스탬프로 최신 찾을 SQL ROW_NUMBER를 분석 연료 소모량을 사용

select ItemPrice 
    from 
     (
     select 
      ItemPrice , 
      purchase_timestamp, 
      row_number() over(order by purchase_timestamp desc) rn 
           --add partition by if necessary 
     from Recommended_StagingTable 
    )s 
where rn = 1; --the latest by timestamp