2013-10-19 52 views
0

我要選擇SQL Server數據類型和名稱,但它有列複製,這是我的SQL:鮮明的多列

with typesum 
as 
(
    select sc.xtype,sc.[length],st.name 
    from syscolumns sc,systypes st 
    where sc.xtype=st.xtype 
    group by sc.xtype,sc.[length],st.name 
) 
select distinct tsa.xtype,tsb.name,tsb.[length] 
from typesum tsa left join typesum tsb 
on tsa.xtype=tsb.xtype 

它有很多複製數據。

我用另一種方式:

select distinct (sc.xtype,st.name) from syscolumns sc 
left join systypes st 
on sc.xtype=st.xtype 
group by sc.xtype,st.name 

它不工作。

我的問題是:我怎麼能明顯多列由不同的一個欄目,並獲得獨特的數據的結果,有10列,我想作爲標準不同的第1列,和其他列的數據應該被刪除?

例如:

1   1 
1   1 
2   2 
2   2 

結果是:

1   1 
2   2 

也許樣品SQL可以解決的是better.Thank你。

+0

什麼是 '這是行不通的。'意思?最後一個查詢中有錯誤的圓括號。 –

回答

3

你並不需要同時group bydistinct

要麼

select distinct sc.xtype,st.name 
from syscolumns sc 
left join systypes st 
on sc.xtype=st.xtype 

select sc.xtype,st.name 
from syscolumns sc 
left join systypes st 
on sc.xtype=st.xtype 
group by sc.xtype,st.name 

都應該工作

+0

選擇不同的sc.xtype,st.name 我應該這樣理解:select distinct(sc.xtype,st.name)或者像這樣:select(distinct sc.xtype),st.name – Dolphin

+1

@Dolphin'SELECT DISTINCT'適用於所有選定的值。 – podiluska

+0

當我想選擇更多的列如:select sc.name,sc.xtype,st.name,st。[length] from syscolumns sc left join systypes st on sc.xtype = st.xtype group by st。名,sc.xtype,sc.name,ST [長度]。以及如何區分st.name?我希望結果st.name是唯一的,其他數據可以刪除。 – Dolphin