求sql语句,实现显示的每条记录后面都加一列,显示其数量占总数的百分比

iailp4728 2014-05-19 04:04:40
select type ,sum(value) as value  from (
select 'a'as type,1 as value
union all select 'a',2
union all select 'b',1
union all select 'b',3
union all select 'c',5 ) a
group by type
------------
type value
a 3
b 4
c 5
------------
但是我想显示的是这样:
type value sum bl
a 3 12 25%
b 4 12 33.3%
c 5 12 41.67%
请问如何实现
...全文
857 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
iailp4728 2014-05-22
  • 打赏
  • 举报
回复
引用 6 楼 DBAXMMDS 的回复:

--在sqlserver2012 下有用的语句
select distinct type ,sum(a.value) over( partition by type )as value,sum(a.value) over() as rn
,cast(cast(sum(a.value) over( partition by type )*1.0/sum(a.value) over() *100 as decimal(9,2)) as varchar)+'%'
from (
select 'a'as type,1 as value
union all select 'a',2
union all select 'b',1
union all select 'b',3
union all select 'c',5  ) a



--结果展示
/*
type value       rn          
---- ----------- ----------- -------------------------------
a    3           12          25.00%
b    4           12          33.33%
c    5           12          41.67%

(3 行受影响)


*/
谢谢,虽然2000下不能运行,不过我试了下,在2008上能执行
极品老土豆 2014-05-20
  • 打赏
  • 举报
回复

--在sqlserver2012 下有用的语句
select distinct type ,sum(a.value) over( partition by type )as value,sum(a.value) over() as rn
,cast(cast(sum(a.value) over( partition by type )*1.0/sum(a.value) over() *100 as decimal(9,2)) as varchar)+'%'
from (
select 'a'as type,1 as value
union all select 'a',2
union all select 'b',1
union all select 'b',3
union all select 'c',5  ) a



--结果展示
/*
type value       rn          
---- ----------- ----------- -------------------------------
a    3           12          25.00%
b    4           12          33.33%
c    5           12          41.67%

(3 行受影响)


*/
jiajiaren 2014-05-20
  • 打赏
  • 举报
回复
;with t
as(
select 'a'as type,1 as value
union all select 'a',2
union all select 'b',1
union all select 'b',3
union all select 'c',5  
)

SELECT  type,value,SumValue AS [sum],CONVERT(VARCHAR,ROUND(CONVERT(FLOAT,Value)/CONVERT(FLOAT,SumValue)*100,2))+'%' AS lr  FROM
(
SELECT  type,SUM(Value) Value,(SELECT SUM(value) FROM t) SumValue FROM t GROUP BY type

)a

 
 /*
 type value       sum         lr
---- ----------- ----------- -------------------------------
a    3           12          25%
b    4           12          33.33%
c    5           12          41.67%

(3 行受影响)

 */
 
exception92 2014-05-20
  • 打赏
  • 举报
回复

with temp as(select * from Table11)
select Name,count(Name) 数量,CONVERT(decimal(18,2), COUNT(Name)*100.0/(select COUNT(Name) from temp)) 所占百分比 from temp group by Name,Content
  • 打赏
  • 举报
回复
引用 2 楼 u012900934 的回复:
[quote=引用 1 楼 TravyLee 的回复:]
;with t
as(
select 'a'as type,1 as value
union all select 'a',2
union all select 'b',1
union all select 'b',3
union all select 'c',5  
)
select type,ltrim(SUM(value)*100.0/(select SUM(value) from t))+'%' as per from t
group by type
/*
-------------------------
type	per
a	25.000000000000%
b	33.333333333333%
c	41.666666666666%
-------------------------
*/
现在的问题是,这个表实际上是我关联两个表查出来的,数据有点多。有没有方法只查询一次表得到结果 [/quote] 你不能一次性把问题说完嘛?玩我们啊!把具体问题贴出来
iailp4728 2014-05-19
  • 打赏
  • 举报
回复
引用 1 楼 TravyLee 的回复:
;with t
as(
select 'a'as type,1 as value
union all select 'a',2
union all select 'b',1
union all select 'b',3
union all select 'c',5  
)
select type,ltrim(SUM(value)*100.0/(select SUM(value) from t))+'%' as per from t
group by type
/*
-------------------------
type	per
a	25.000000000000%
b	33.333333333333%
c	41.666666666666%
-------------------------
*/
现在的问题是,这个表实际上是我关联两个表查出来的,数据有点多。有没有方法只查询一次表得到结果
  • 打赏
  • 举报
回复
;with t
as(
select 'a'as type,1 as value
union all select 'a',2
union all select 'b',1
union all select 'b',3
union all select 'c',5  
)
select type,ltrim(SUM(value)*100.0/(select SUM(value) from t))+'%' as per from t
group by type
/*
-------------------------
type	per
a	25.000000000000%
b	33.333333333333%
c	41.666666666666%
-------------------------
*/

34,598

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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