如何限制AVG或SUM计算的值
How Should I do to limit the value calculated by AVG or SUM
我想让AMY的结果值在300以内。
example
270 -> 270
300 -> 300
350 -> 300
我该如何修改呢?
是否可以用case when in 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
您可以使用 least()
:
least(sum(case when type = 'A' then cost else -cost end) * 0.08, 300) as AMY
如果sum()
超过300,则return的值为300
。如果你想强加一个最小值,你可以使用 greatest()
。因此,这将确保该值介于 -300 和 300 之间:
greatest(least(sum(case when type = 'A' then cost else -cost end) * 0.08, 300), -300) as AMY
我想让AMY的结果值在300以内。
example
270 -> 270
300 -> 300
350 -> 300
我该如何修改呢? 是否可以用case when in 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
您可以使用 least()
:
least(sum(case when type = 'A' then cost else -cost end) * 0.08, 300) as AMY
如果sum()
超过300,则return的值为300
。如果你想强加一个最小值,你可以使用 greatest()
。因此,这将确保该值介于 -300 和 300 之间:
greatest(least(sum(case when type = 'A' then cost else -cost end) * 0.08, 300), -300) as AMY