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%
请问如何实现
7 个解决方案
#1
;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%
-------------------------
*/
#2
现在的问题是,这个表实际上是我关联两个表查出来的,数据有点多。有没有方法只查询一次表得到结果
#3
你不能一次性把问题说完嘛?玩我们啊!把具体问题贴出来
#4
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
#5
;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 行受影响)
*/
#6
--在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 行受影响)
*/
#7
--在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上能执行
#1
;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%
-------------------------
*/
#2
;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%
-------------------------
*/
现在的问题是,这个表实际上是我关联两个表查出来的,数据有点多。有没有方法只查询一次表得到结果
#3
;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%
-------------------------
*/
现在的问题是,这个表实际上是我关联两个表查出来的,数据有点多。有没有方法只查询一次表得到结果
你不能一次性把问题说完嘛?玩我们啊!把具体问题贴出来
#4
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
#5
;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 行受影响)
*/
#6
--在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 行受影响)
*/
#7
--在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%