2016-11-07 18 views
0

我從xml創建一個配置單元外部表。我想拉取時間戳最大的元素的值。我如何在Create Table Statement中編寫這個代碼?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的價格? Hive中如何做?

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

添加purchase_timestamp列到Recommended_StagingTable然後使用SQL ROW_NUMBER解析funtion找到通過時間戳最新:

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