2016-11-04 93 views
0

我的問題陳述是:我想獲得那些只有那些狀態已關閉的訂單的客戶。客戶不應該擁有其他狀態的單一訂單。無法運行Hive子查詢

我有一個訂單表與模式爲Order_id,Order_customer_id, Order_date,Order_status

有9個訂單狀態是'完成','處理','PAYMENT_REVIEW','PENDING','ON_HOLD','取消','SUSPECTED_FRAUD','PENDING_PAYMENT'和'CLOSED'。

當我在查詢下運行時,我沒有得到想要的結果。 請幫我理解我做錯了什麼。

查詢:

select order_customer_id ,count(*) from orders where order_customer_id 
IN (select order_customer_id from orders where order_status NOT IN 
('COMPLETE' , 'PROCESSING','PAYMENT_REVIEW','PENDING' ,'ON_HOLD' ,'CANCELED','SUSPECTED_FRAUD','PENDING_PAYMENT')) 
group by order_customer_id; 

執行查詢後,我得到是誰,甚至有在其他狀態的訂單那些客戶。

回答

1

您不強制您的客戶必須只有Closed訂單的條件。您在內部查詢中收集來自所有客戶的所有closed訂單。

你可以使用collect_set來解決這個問題:

select order_customer_id ,count from 
(select order_customer_id,collect_set(order_status),count(*) as count from orders where 
    size(collect_set(order_status)) = 1 and 
    collect_set(order_status)[0] = 'CLOSED' 
    group by order_customer_id) inner;