34,576
社区成员
发帖
与我相关
我的任务
分享
declare @t1 table(customerid int,companyid varchar(10))
insert into @t1 select 12,'YA'
insert into @t1 select 13,'YA'
insert into @t1 select 14,'YA'
insert into @t1 select 15,'YC'
insert into @t1 select 16,'YD'
declare @t2 table(companyid int,Disabled int)
insert into @t2 select 12,0
insert into @t2 select 14,1
insert into @t2 select 15,1
select
起用数量=sum(case Disabled when 1 then 1 else 0 end),
总数=count(1),
所占比例=ltrim(cast(sum(case Disabled when 1 then 1 else 0 end)*100.00/count(1) as numeric(5,2)))+'%',
分公司=a.companyid
from @t1 a,@t2 b
where a.customerid=b.companyid
group by a.companyid
select
cast(Disabled as int) as 启用数量,--bit时要转换
count(1) as 总数,
ltrim(str(cast(Disabled as decimal(18,5))/count(1),18,2))+'%' as 所占比例,
b.CompanyId as 分公司
from
t1 a
join
t2 b on a.CustomerID=b.CustomerID
group by b.CompanyId