ID
----------------
1
2
3
3
2
2
1
可以看出,该表*有7条记录,但是不重复的记录个数是3条,现在问题是如何获得不重复的记录个数和不重复的字段的值(这里就是1,2,3)?
5 个解决方案
#1
取不重复的记录个数:
select count(distinct ID) from Tbl1
取不重复的字段的值:
select distinct ID from Tbl1
select count(distinct ID) from Tbl1
取不重复的字段的值:
select distinct ID from Tbl1
#2
create table Tbl1(ID int)
insert into Tbl1 select 1
insert into Tbl1 select 2
insert into Tbl1 select 3
insert into Tbl1 select 3
insert into Tbl1 select 2
insert into Tbl1 select 2
insert into Tbl1 select 1
--取不重复的记录个数:
select count(distinct ID) from Tbl1
/*
无列名
----
3
*/
--取不重复的字段的值:
select distinct ID from Tbl1
/*
ID
----
1
2
3
*/
insert into Tbl1 select 1
insert into Tbl1 select 2
insert into Tbl1 select 3
insert into Tbl1 select 3
insert into Tbl1 select 2
insert into Tbl1 select 2
insert into Tbl1 select 1
--取不重复的记录个数:
select count(distinct ID) from Tbl1
/*
无列名
----
3
*/
--取不重复的字段的值:
select distinct ID from Tbl1
/*
ID
----
1
2
3
*/
#3
也可以:
--取不重复的记录个数:
select count(1)
from (
select ID from Tbl1 group by ID
)t
--取不重复的字段的值:
select ID from Tbl1 group by ID
--取不重复的记录个数:
select count(1)
from (
select ID from Tbl1 group by ID
)t
--取不重复的字段的值:
select ID from Tbl1 group by ID
#4
--如何获得不重复的记录个数和不重复的字段的值(这里就是1,2,3)?
set nocount on
declare @Tbl1 table (ID int)
insert into @Tbl1 select 1
insert into @Tbl1 select 2
insert into @Tbl1 select 3
insert into @Tbl1 select 3
insert into @Tbl1 select 2
insert into @Tbl1 select 2
insert into @Tbl1 select 1
declare @str varchar(20)
set @str = ''
select @str = @str + ',' + convert(varchar(5),T.ID) from
(select distinct ID from @Tbl1) as T
set @str = stuff(@str,1,1,'')
print @str
/*
1,2,3
*/
set nocount on
declare @Tbl1 table (ID int)
insert into @Tbl1 select 1
insert into @Tbl1 select 2
insert into @Tbl1 select 3
insert into @Tbl1 select 3
insert into @Tbl1 select 2
insert into @Tbl1 select 2
insert into @Tbl1 select 1
declare @str varchar(20)
set @str = ''
select @str = @str + ',' + convert(varchar(5),T.ID) from
(select distinct ID from @Tbl1) as T
set @str = stuff(@str,1,1,'')
print @str
/*
1,2,3
*/
#5
谢谢,应该都对!
#1
取不重复的记录个数:
select count(distinct ID) from Tbl1
取不重复的字段的值:
select distinct ID from Tbl1
select count(distinct ID) from Tbl1
取不重复的字段的值:
select distinct ID from Tbl1
#2
create table Tbl1(ID int)
insert into Tbl1 select 1
insert into Tbl1 select 2
insert into Tbl1 select 3
insert into Tbl1 select 3
insert into Tbl1 select 2
insert into Tbl1 select 2
insert into Tbl1 select 1
--取不重复的记录个数:
select count(distinct ID) from Tbl1
/*
无列名
----
3
*/
--取不重复的字段的值:
select distinct ID from Tbl1
/*
ID
----
1
2
3
*/
insert into Tbl1 select 1
insert into Tbl1 select 2
insert into Tbl1 select 3
insert into Tbl1 select 3
insert into Tbl1 select 2
insert into Tbl1 select 2
insert into Tbl1 select 1
--取不重复的记录个数:
select count(distinct ID) from Tbl1
/*
无列名
----
3
*/
--取不重复的字段的值:
select distinct ID from Tbl1
/*
ID
----
1
2
3
*/
#3
也可以:
--取不重复的记录个数:
select count(1)
from (
select ID from Tbl1 group by ID
)t
--取不重复的字段的值:
select ID from Tbl1 group by ID
--取不重复的记录个数:
select count(1)
from (
select ID from Tbl1 group by ID
)t
--取不重复的字段的值:
select ID from Tbl1 group by ID
#4
--如何获得不重复的记录个数和不重复的字段的值(这里就是1,2,3)?
set nocount on
declare @Tbl1 table (ID int)
insert into @Tbl1 select 1
insert into @Tbl1 select 2
insert into @Tbl1 select 3
insert into @Tbl1 select 3
insert into @Tbl1 select 2
insert into @Tbl1 select 2
insert into @Tbl1 select 1
declare @str varchar(20)
set @str = ''
select @str = @str + ',' + convert(varchar(5),T.ID) from
(select distinct ID from @Tbl1) as T
set @str = stuff(@str,1,1,'')
print @str
/*
1,2,3
*/
set nocount on
declare @Tbl1 table (ID int)
insert into @Tbl1 select 1
insert into @Tbl1 select 2
insert into @Tbl1 select 3
insert into @Tbl1 select 3
insert into @Tbl1 select 2
insert into @Tbl1 select 2
insert into @Tbl1 select 1
declare @str varchar(20)
set @str = ''
select @str = @str + ',' + convert(varchar(5),T.ID) from
(select distinct ID from @Tbl1) as T
set @str = stuff(@str,1,1,'')
print @str
/*
1,2,3
*/
#5
谢谢,应该都对!