2016-11-28 115 views
0

我正在處理的表的列是customer_id,operating_system,device_type,transaction_id,transaction_time。Hive查詢找到百分比值

我想了解客戶在過去360天內在移動/平板電腦設備上完成的交易使用的操作系統的百分比。

基本方法是:設備類型(移動/平板電腦)和時間戳超過360天的交易數量按客戶分組_id,operating_system * 100 /特定客戶爲設備類型完成的交易總數(移動/平板電腦),無論操作系統如何。

我怎麼能寫查詢找到輸出爲:CUSTOMER_ID,OPERATING_SYSTEM,操作系統使用%

預先感謝您!

+0

以下是我準備的查詢。我不知道天氣它會不會起作用。選擇t.customer_id,t.operating_system,100 *(count(t.transaction_id)/ q1.total) 從 表t, (選擇count(transaction_id)作爲表中的總數,其中device_type在(mobile,tablet)和transaction_time 360 集團通過CUSTOMER_ID )Q1 凡 DEVICE_TYPE在(手機,平板電腦),並通過CUSTOMER_ID transaction_time 360​​ 集團,OPERATING_SYSTEM –

回答

0

在子查詢s下面計算消費者總數和操作系統計數。由於使用了分析函數,因此行數保持與源數據集中的相同。這就是爲什麼你需要通過consumer_id和operating_system進行聚合。使用maxmin

select --group by consumer_id and operating_system 
      customer_id, 
      operating_system, 
      max(operating_system_cnt)     operating_system_cnt, 
      max(total_cnt)        total_cnt, 
      max(operating_system_cnt)*100/max(total_cnt) operating_system_percent 
    from 
    ( 
    select --calculate total count and operating_system_count 
    customer_id, 
    operating_system, 
    count(transaction_id) over(partition by customer_id, operating_system) operating_system_cnt, 
    count(transaction_id) over(partition by customer_id) total_cnt 
    from your_table 
    where --your filter conditions here for mobile/tablet and last 360 days 
    )s 
group by 
     customer_id, 
     operating_system 
+0

謝謝你的query.Just一個question.We需要計算交易的總數(transaction_id-每個事務都是唯一的),所以我們可以通過在查詢中使用transaction_id找出所有事務的計數嗎? –

+0

我不完全瞭解您的數據。對於一個客戶和操作系統,是否可以重複transaction_id?請發佈數據示例。 – leftjoin

+0

固定查詢。我沒有檢查它,現在無法做到,也許還有一些其他的錯誤。 – leftjoin