如何在sql中获得多个百分比值的平均值 [英] how to get average for multiple percentage values in sql

查看:718
本文介绍了如何在sql中获得多个百分比值的平均值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在sql查询中获得多个百分比值的平均值



我的查询结果是



94.58

96



和我的查询是:



 选择 Sum(案例 何时(b.test1<> b.test2 
OR (b.test1 IS NULL AND b.test2 IS < span class =code-keyword> NOT NULL
OR ( b.test1 IS NOT NULL AND b.test2 IS NULL ))个en 1 else 0 end 示例来自 table b
< pre lang = sql< /跨度>>
union

select ((Sum(a.counterid) * 1.0 / Sum(a.fieldcount))* 100) as sample2 来自 table2 a







请帮帮我。

解决方案





检查这个...



  SELECT  avg(样本)来自 

选择总和(案例 时(b.test1<> b.test2 $ b) $ b OR (b.test1 IS NULL AND b.test2 IS NOT < span class =code-keyword> NULL )
OR (b.test1 IS NOT NULL AND b.test2 IS NULL )) 1 else 0 end as 示例来自 b
union
选择((Sum(a.counterid)* 1.0 / Sum(a.fieldcount))* 100) as sample2 来自 table2 a
)x





希望这会对你有所帮助。



干杯


使用公用表格表格

<前郎= sql> WITH T(I) AS

选择 Sum( Case (b.test1< > b.test2
OR (b.test1 IS NULL AND b.test2 IS NOT NULL
(b.test1 IS NOT NULL AND b.test2 IS NULL ))然后 1 else 0 end 示例来自 table b
UNION
选择((Sum(a.counterid)* 1.0 / Sum(a.fieldcount))* 100) as sample2 来自 table2 a
}
SELECT AVG(I) FROM T


How to get average for multiple percentage values in sql query

my query results are

94.58
96

and my query is:

select Sum(Case when (b.test1 <> b.test2
      OR (b.test1 IS NULL AND b.test2 IS NOT NULL)
      OR (b.test1 IS NOT NULL AND b.test2 IS NULL)) then 1 else 0 end) as sample from table b
<pre lang="sql">
      union

select((Sum(a.counterid)*1.0/Sum(a.fieldcount))*100)as sample2 from table2 a




Please help me on this.

解决方案

Hi,

Check this...

SELECT avg(sample) from
(
select Sum(Case when (b.test1 <> b.test2
      OR (b.test1 IS NULL AND b.test2 IS NOT NULL)
      OR (b.test1 IS NOT NULL AND b.test2 IS NULL)) then 1 else 0 end) as sample from table b
      union
select((Sum(a.counterid)*1.0/Sum(a.fieldcount))*100)as sample2 from table2 a
) x



Hope this will help you.

Cheers


Using Common table expressions

WITH T(I) AS 
(
    select Sum(Case when (b.test1 <> b.test2
          OR (b.test1 IS NULL AND b.test2 IS NOT NULL)
          OR (b.test1 IS NOT NULL AND b.test2 IS NULL)) then 1 else 0 end) as sample from table b
    UNION
    select((Sum(a.counterid)*1.0/Sum(a.fieldcount))*100)as sample2 from table2 a
}
SELECT AVG(I) FROM T


这篇关于如何在sql中获得多个百分比值的平均值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆