62,254
社区成员
发帖
与我相关
我的任务
分享
create table test_count
(
id int ,
[name] varchar(50),
pice float
)
insert into test_count
select 1 ,'张山' , 20.5 union all
select 2 ,'小白' , 30.5 union all
select 1 ,'张山' , 24.5 union all
select 3 ,'王新' , 66 union all
select 4 ,'张山' , 55.5 union all
select 5 ,'王霸' , 77.2 union all
select 6 ,'王新' , 67.4 union all
select 7 ,'小白' , 107 union all
select 8 ,'王小' , 105 union all
select 9 ,'李里' , 178 union all
select 10 ,'王王' , 189
select distinct
(select count( distinct [name]) from test_count where pice <50 )as '小于50' ,
(select count( distinct [name]) from test_count where pice between 50 and 100) as '50-100',
(select count( distinct [name]) from test_count where pice>100 )as '100-200'
from test_count where 1=1
============
小于50 50-100 100-200
----------- ----------- -----------
2 3 4
(1 行受影响)
create table test_count
(
id int ,
[name] varchar(50),
pice float
)
insert into test_count
select 1 ,'张山' , 20.5 union all
select 2 ,'小白' , 30.5 union all
select 1 ,'张山' , 24.5 union all
select 3 ,'王新' , 66 union all
select 4 ,'张山' , 55.5 union all
select 5 ,'王霸' , 77.2 union all
select 6 ,'王新' , 67.4 union all
select 7 ,'小白' , 107 union all
select 8 ,'王小' , 105 union all
select 9 ,'李里' , 178 union all
select 10 ,'王王' , 189
--小于50 50-100 100-200
select max( case when pice < 50.0 then [name] else null end) as '小于50' ,
max( case when pice between 50.0 and 100.0 then [name] else null end) as '50-100',
max(case when pice between 100.0 and 200.0 then [name] else null end) as '100-200'
into # from test_count group by [name]
select distinct (select count(1) from # where [小于50] is not null) as [小于50] ,
(select count(1) from # where [50-100] is not null) as [50-100],
(select count(1) from # where [100-200] is not null) as [100-200]
from #
drop table #
===========
小于50 50-100 100-200
----------- ----------- -----------
2 3 4
你结果有问题
小于50的有3个
create table #TT
(
id int,
name nvarchar(50),
pice decimal(19,2)
)
insert into #TT select 1,'张山',20.5
insert into #TT select 2,'小白',30.5
insert into #TT select 1,'张山',24.5
insert into #TT select 3,'王新',66
insert into #TT select 4,'张山',55.5
insert into #TT select 5,'王霸',77.2
insert into #TT select 6,'王新',67.4
insert into #TT select 7,'小白',107
insert into #TT select 8,'王小',105
insert into #TT select 9,'李里',178
insert into #TT select 10,'王王',189
select sum(case when pice<50 then 1 else 0 end) '小于50',
sum(case when pice between 50 and 100 then 1 else 0 end) '大于50小于100',
sum(case when pice>100 then 1 else 0 end) '大于100'
from #TT
小于50 大于50小于100 大于100
----------- ----------- -----------
3 4 4create table #TT
(
id int,
name nvarchar(50),
pice decimal(19,2)
)
insert into #TT select 1,'张山',20.5
insert into #TT select 2,'小白',30.5
insert into #TT select 1,'张山',24.5
insert into #TT select 3,'王新',66
insert into #TT select 4,'张山',55.5
insert into #TT select 5,'王霸',77.2
insert into #TT select 6,'王新',67.4
insert into #TT select 7,'小白',107
insert into #TT select 8,'王小',105
insert into #TT select 9,'李里',178
insert into #TT select 10,'王王',189
select sum(case when pice<50 then 1 else 0 end) '小于50',
sum(case when pice between 50 and 100 then 1 else 0 end) '大于50小于100',
sum(case when pice>100 then 1 else 0 end) '大于100'
from #TT