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可以解决更好。谢谢。

4

1 回答 1

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

两者都应该工作

于 2013-10-19T15:17:36.407 回答