2014-09-19 42 views
0

我有一個自我連接的結果表,我有很多重複的行(因爲我在該表上也有歷史記錄)。
結果表是(日期,日期,varchar,int,int,int,int)。
我怎樣才能返回唯一的行(其中所有列相同..)從結果表中選擇獨特的行

謝謝!

+1

'選擇distinct'。 – 2014-09-19 19:30:07

回答

1

您可以使用DISTINCT

select distinct * 
from yourTable 
+0

ohh snap .. so dumb .. – gabi 2014-09-19 19:32:15

1

這取決於你的獨一無二的意思;給定一個表:

create table example 
(
    col1 int 
    ,col2 int 
) 
insert example (col1,col2) values (1,2) 
insert example (col1,col2) values (1,2) 
insert example (col1,col2) values (1,3) 
insert example (col1,col2) values (1,4) 

你想要得到的結果是:

1,2 
1,3 
1,4 

(即返回的行是唯一的,雖然有可能與源表中的這些相同的價值觀多行)

或:

1,3 
1,4 

(即你只希望它是在源唯一的行數據首先)

如果是前者,使用:

select distinct col1 
, col2 
from example 

如果是後者,使用:

select col1 
, col2 
from example 
group by col1 
, col2 
having count(1) = 1 

SQL小提琴:http://sqlfiddle.com/#!3/594ac/1