2011-12-27 32 views
1

我希望做一個請求,我的表,我選擇了排在那裏我找到了一個時代的客戶ID如何選擇行,我發現一個次客戶ID

我的表結構如下所示:

id , date_commande, id_customer 

我不想返回行,如果我發現客戶ID兩次或更多的數據

例如:

id | date_commande | id_customer 
-------------------------------- 
01 | 2011-12-01 |  10 
02 | 2011-12-01 |  11 
03 | 2011-12-02 |  14 
04 | 2011-12-03 |  10 

我只想要回線

id | date_commande | id_customer 
-------------------------------- 
02 | 2011-12-01 |  11 
03 | 2011-12-02 |  14 
+0

你能提供一些樣本數據和你的預期結果嗎? – Taryn 2011-12-27 14:58:59

+0

什麼版本的oracle? – xQbert 2011-12-27 15:18:58

回答

1

這是@Florin建議查詢的略微改進版..

Select ID, date_commande, ID_customer 
from table 
where ID_Customer in 
(Select ID_Customer from table Group by ID_Customer having count(*) = 1) 
+0

同意w /羅伯特的補充評論:並給弗洛林+1。 – xQbert 2011-12-27 17:33:28

1

感謝甚至更多的反饋,這裏是再次修訂版

SELECT id, date_commande,id_customer 
FROM (
    SELECT 
    MIN(id) as id, 
    MIN(date_commande) as date_commande, 
    MIN(id_customer) as id_customer, 
    COUNT(*) as numrows 
    FROM mytable 
    GROUP BY id_customer 
) 
WHERE numrows=1 
+0

在oracle中不起作用。 _不按表達式分組_ – 2011-12-27 15:04:42

+0

仍然需要改進:)不是按表達式分組 – 2011-12-27 15:07:50

+0

@FlorinGhita謝謝,我會認爲已經使用了很多次,但似乎這從來沒有與Oracle一起使用。修改了我的SQL。 – 2011-12-27 15:08:02

4
select * 
from table 
where id_customer in 
(select id_customer 
    from table 
    group by id_customer 
    having count(*) = 1) 
+0

在oracle中不起作用。不是由表達式組成 – Mercer 2011-12-27 15:07:44

+0

我只是在oracle 10g中運行這個工作對我很好。 – xQbert 2011-12-27 15:17:35

+0

我也確定「有count(*)= 1」可以工作,但也許它是一個版本的東西。 – 2011-12-27 15:19:07