在大多数数据库中,您会使用 case 语句:
select (case when value >= 40 then '40+'
when value >= 30 then '30+'
when value >= 20 then '20+'
when value > 10 then '10+'
end)
不幸的是,MS-Access 不支持这种情况,因此您必须iif
改用:
最简单的方法是将表达式放在子查询中:
select valuegroup, sum(win)
from (select t.*,
iif(value > 40, '40+',
iif(value > 30, '30+',
iif(value > 20, '20+',
iif(value > 10, '10+', '<10'
)))) as valuegroup
from t
) t
group by valuegroup
要获得运行总和,您需要进行自我加入。很傻,但是 Access 没有窗口功能:
select valuegroup, sum(cumwin)
from (select t.*, sum(t2.win) as cumwin,
iif(t.value > 40, '40+',
iift.(value > 30, '30+',
iif(t.value > 20, '20+',
iif(t.value > 10, '10+', '<10'
)))) as valuegroup
from t join
t t2
on t.value <= t2.value
group by t.value
) t
group by valuegroup
如果 MS Access 不允许您valuegroup
输入group by
外部查询,那么您必须输入整个表达式。