带有空值的求和“不起作用”在 SQL Server/Azure 中

发布于 2025-01-20 02:36:19 字数 1194 浏览 1 评论 0原文

我试图通过在AVG()函数中使用案例语句来平均一组列,并将其从分母中排除为零。我读到AVG()不包括无效结果,因此正在使用案例语句用null替换0。但是,这不是我在实践中发现的 - 请参见下面的代码。有人可以解释为什么会发生这种情况吗?如果您可以提出一项代码调整以实现我之后的目标,那也将是晶圆厂。谢谢。

with a as
(
select 0 t1, 3 t2 
)
, b as
(
select 6 t1, 0 t2
)
, c as -- building a small table containing test data.
(
select * from a
union all
select * from b
)
select sum(case when t2 = 0 then null else t2 end + case when t1 = 0 then null else t1 end) r1 
  , avg(case when t2 = 0 then null else t2 end + case when t1 = 0 then null else t1 end) r2
  , avg(t1) r3
from c

什么子查询C包含:

T1T2
03
60

我的查询的实际结果:

R1R2R3
NULL3

R2是我希望我的查询结果为:avg (3 + NULL,NULL + 6)= AVG(3,6)= 4.5

R1R2R3
94.53

I'm trying to average a set of columns and exclude rows with zero from the denominator by using a case statement within an AVG() function. I read that avg() excludes NULL results so am using the case statement to replace 0's with NULL's. However that's not what I find in practice - see code below. Can someone explain why this happens? If you can suggest a code tweak to achieve what I'm after that would also be fab. Thanks.

with a as
(
select 0 t1, 3 t2 
)
, b as
(
select 6 t1, 0 t2
)
, c as -- building a small table containing test data.
(
select * from a
union all
select * from b
)
select sum(case when t2 = 0 then null else t2 end + case when t1 = 0 then null else t1 end) r1 
  , avg(case when t2 = 0 then null else t2 end + case when t1 = 0 then null else t1 end) r2
  , avg(t1) r3
from c

What subquery c contains:

t1t2
03
60

The actual result of my query:

r1r2r3
NULLNULL3

Column r2 is what I would like the result of my query to be: avg(3 + null, null + 6) = avg(3, 6) = 4.5:

r1r2r3
94.53

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

凉墨 2025-01-27 02:36:19

您可以将它们过滤掉,而不是将 0 值设置为 null

with a as
(
select cast(0 as float) t1, cast(3 as float) t2 
)
, b as
(
select cast(6 as float) t1, cast(0 as float) t2
)
, c as -- building a small table containing test data.
(
select * from a where t1 > 0 or t2 > 0
union all
select * from b where t1 > 0 or t2 > 0
)
select sum(t2+t1) r1 
  , avg(t2+t1) r2
  , avg(t1) r3
from c;

输出:

| r1 | r2 | r3 |
|----+----+----|
| 9  | 4.5| 3  |

*了解如何将数字转换为 float 类型,以便捕获 r2 上的浮点。

Instead of setting the 0 values to null, you can filter them out somewhere in between:

with a as
(
select cast(0 as float) t1, cast(3 as float) t2 
)
, b as
(
select cast(6 as float) t1, cast(0 as float) t2
)
, c as -- building a small table containing test data.
(
select * from a where t1 > 0 or t2 > 0
union all
select * from b where t1 > 0 or t2 > 0
)
select sum(t2+t1) r1 
  , avg(t2+t1) r2
  , avg(t1) r3
from c;

Output:

| r1 | r2 | r3 |
|----+----+----|
| 9  | 4.5| 3  |

*See how I convert numbers to float type, in order to capture the floating point on r2.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文