【问题标题】:How Should I do to limit the value calculated by AVG or SUM [closed]我应该如何限制由 AVG 或 SUM 计算的值 [关闭]
【发布时间】:2020-12-04 06:13:25
【问题描述】:

我想让 AMY 的结果值在 300 以内。

example
270 -> 270
300 -> 300
350 -> 300 

我应该如何修改它? 是否可以在Case when中使用case when?

select 
no, sum(case when type = 'A' then cost else -cost end) * 0.08 as AMY 
from MIKE
where 
MIKE.type in ('A', 'B') 
and exists (select 1 from users u where u.no = c.otaku_no)
group by no

【问题讨论】:

  • 请用 one 数据库标记您的问题,这是您真正使用的数据库。
  • 我正在使用 Oracle db..

标签: sql oracle11g


【解决方案1】:

你可以使用least():

   least(sum(case when type = 'A' then cost else -cost end) * 0.08, 300) as AMY 

如果sum() 超过300,则返回值为300。如果您想施加最低限度,可以使用greatest()。因此,这将确保该值介于 -300 和 300 之间:

   greatest(least(sum(case when type = 'A' then cost else -cost end) * 0.08, 300), -300) as AMY 

【讨论】:

  • 太棒了,谢谢。
猜你喜欢
  • 2013-03-17
  • 1970-01-01
  • 1970-01-01
  • 2023-03-19
  • 2016-06-19
  • 1970-01-01
  • 2020-01-31
  • 2019-09-06
  • 1970-01-01
相关资源
最近更新 更多