2017-08-30 104 views
1

我稱一表「訂單」COUNT DISTINCT MS ACCESS

clientID | order_status 
client1 | pending 
client1 | shipped 
client2 | pending 
client2 | complete 
client3 | complete 
client3 | shipped 
client4 | pending 
client4 | pending 
client5 | shipped 
client5 | shipped 
client6 | complete 
client6 | complete 

我想選擇一個與兩個「待定」的ORDER_STATUS記錄和「已裝船」

這個例子應該任何clientID的僅返回 「客戶端1」

這裏是我的查詢:

SELECT clientID 
FROM orders 
WHERE order_status IN("pending", "shipped") 
GROUP BY clientID 
HAVING COUNT(*) > 1 

將返回:

client1 
client4 
client5 

我知道我需要使用COUNT(DISTINCT ORDER_STATUS),但是這是一個Microsoft Access數據庫,我不知道如何正確地執行它

回答

0

一般通過使用MSACCESS一個內嵌視圖處理。內聯視圖通過客戶端獲得不同的order_status,然後您可以計數。

SELECT clientID 
FROM (SELECT ClientID, order_status 
     FROM orders 
     WHERE order_status IN("pending", "shipped") 
     GROUP BY clientID, order_status) B 
GROUP BY ClientID 
HAVING count(*) > 1 

或者

SELECT clientID 
FROM (SELECT Distinct ClientID, order_status 
     FROM orders 
     WHERE order_status IN("pending", "shipped")) B 
GROUP BY ClientID 
HAVING count(*) > 1 
+0

這是有效的,謝謝! – B0WS3R

0

你的哪個問題

選擇具有記錄與兩個 的ORDER_STATUS任何clientID的 「待定」 和 「運」

我想查詢語句應運營商使用並在條件類似的

SELECT clientID 
FROM orders 
WHERE order_status = "pending" AND order_status = "shipped" 
+1

(因爲我不通過W/O聚合類基團),這將不返回任何結果,因爲ORDER_STATUS只能包含每個記錄一個值。 「待處理」和「已發貨」是兩個單獨記錄的order_status – B0WS3R