27,579
社区成员
发帖
与我相关
我的任务
分享
create table #t(id int ,m varchar(10))
insert into #t
select 0 ,'新订单' union all
select 1 ,'已经被受理' union all
select 2 ,'退回'
Create table #tmp(订单号 int ,状态 int)
insert into #tmp
select 1,0 union all
select 2,0 union all
select 3,0 union all
select 4,1 union all
select 5,1
SELECT *From #t
select 状态,m, RTRIM(CAST(100.0*count(状态)/(select count(1) from #tmp) AS NUMERIC(12,2)))+'%' a
FROM #tmp JOIN #t ON id=[状态]
GROUP BY 状态,m
状态 m a
----------- ---------- ------------------------------------------
0 新订单 60.00%
1 已经被受理 40.00%
create table #t(id int ,m varchar(10))
insert into #t
select 0 ,'新订单' union all
select 1 ,'已经被受理' union all
select 2 ,'退回'
Create table #tmp(订单号 int ,状态 int)
insert into #tmp
select 1,0 union all
select 2,0 union all
select 3,0 union all
select 4,1 union all
select 5,1
select 状态, CAST(100*count(状态)/(select cast(count(1) as numeric(12,2)) from #tmp) AS VARCHAR(20)) +'%' FROM #tmp GROUP BY 状态
SELECT STATUS ,CAST(100*COUNT(*)/(select count(1) from dbo.Orders) AS VARCHAR(10))+'%' AS c FROM dbo.Orders
GROUP BY status
ORDER BY status
declare @total float
select @total = count(*) from dbo.Orders
SELECT STATUS ,COUNT(*), count(*)/@total AS c FROM dbo.Orders
GROUP BY status
ORDER BY status