按状态统计的问题

cqq 2010-08-18 10:13:38
一个订单表,有一个字段 status 表示这个订单的处理状态,数字类型的,
0 新订单
1 已经被受理
2 退回
.......

现在,我想根据状态统计订单,得到如下的结果

状态 个数 百分比
新订单 10 50%
已经被受理 9 45%
.

我下面的语句能统计出来个数,统计不了百分比
SELECT STATUS ,COUNT(*) AS c FROM dbo.Orders
GROUP BY status
ORDER BY status
...全文
69 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
duanzhi1984 2010-08-18
  • 打赏
  • 举报
回复
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%
duanzhi1984 2010-08-18
  • 打赏
  • 举报
回复
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 状态
duanzhi1984 2010-08-18
  • 打赏
  • 举报
回复
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
SQLCenter 2010-08-18
  • 打赏
  • 举报
回复
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

27,579

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 应用实例
社区管理员
  • 应用实例社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧