我面临一个问题,写一个SQL查询以获得结果在%模式下,我很熟悉SUM()
和COUNT()
SQL Server的功能,但面临问题来实现逻辑内查询我想导致以下表格: -是否有任何内置函数来计算在SQL Server中的百分比
UserName--- % of AccepectResult---- % of RejectResult
我的表结构是这样的有两列Name
(用户名)和Result
:
NAME Result
---------------
USer1 A
USer1 A
USer1 A
USer1 R
USer1 R
USer1 A
USer2 A
USer2 A
USer2 A
USer2 A
USer2 R
A - Accepted Result
R - Rejected Result
我想写这个查询像这样..
select * into #t1 from
(
select UserName , count(Result) as Acc
from Test where result = 'A'
group by UserName
) as tab1
select * into #t2 from
(
select UserName , count(Result) as Rej
from Test where result = 'R'
group by UserName
) as tab2
select #t1.UserName ,
#t1.Acc ,
#t2.Rej ,
(#t1.Acc)*100/(#t1.Acc + #t2.Rej) as AccPercentage,
(#t2.Rej)*100/(#t1.Acc + #t2.Rej) as RejPercentage
from #t1
inner join #t2 on #t1.UserName = #t2.UserName
drop table #t1
drop table #t2
是否有任何其他方式来编写此查询和任何内置函数来计算SQL Server中的百分比?