如下表:
表名:Test
IDRowIDCol1Col2
11AA
21BA
31AB
41CB
1,查找表中字段重复的只查找一次
select distinct Col1 from Test ;
select Col1 from Test where ID in(select min(ID) from Test group by Col1 );
结果为:
A
B
C
2,统计并查询该字段出现的数量
SELECT Col1,COUNT(Col1) FROM Test GROUP BY Col1;
结果为:
A 2
B 1
C 1
3,把一列多行内容拼成一行
select STUFF ((select ','+F.Col1 from Test F where F.RowID= 1 order by F.ID FOR XML PATH('')),1,1,'') As Col3;
结果为:
Col3
A,B,A,C
(编辑:雷林鹏 来源:网络)