1

桌子:

Value | Win
 12      3
 22      4
 44      5
 12      11

sum(Win)在 get和 order by的那一刻Value,它返回。所以都是以价值取胜。

Value | Win
 12      14
 22      4
 44      5

现在我不想按顺序订购Value,我想这样做:

返回所有奖金,其中 value 大于 10, 20, 30,40 :

所以回报应该是:

Value | Win
 10+      23
 20+      9
 30+      5
 40+      5

这件事可能吗?建议?

4

3 回答 3

2

分组表可能有一些优点,例如:

组表

这将允许您构建:

组表2

使用:

SELECT Groups.GroupName, ta.Value
FROM Groups, ta
WHERE ta.Value Between [low] And [high]

甚至

WHERE ta.Value >= [low] 

并最终确定:

SELECT GroupName, Count([Value]) , Sum(Win)
FROM (SELECT Groups.GroupName, ta.Value, ta.Win
FROM Groups, ta
WHERE ta.Value >= [low]) q
GROUP BY GroupName

一个优点是您可以通过编辑表格非常轻松地添加和删除组。

于 2013-01-13T17:17:48.723 回答
2
SELECT '10+' AS v,
SUM(IIF (myvalue>10, win)) AS w
FROM mytable

UNION ALL

SELECT '20+' AS v,
SUM(IIF (myvalue>20, win)) AS w
FROM mytable

UNION ALL

SELECT '30+' AS v,
SUM(IIF (myvalue>30, win)) AS w
FROM mytable

UNION ALL

SELECT '40+' AS v,
SUM(IIF (myvalue>40, win)) AS w
FROM mytable

注意:我使用的字段名称与问题中使用的名称略有不同......

于 2013-01-13T17:35:02.737 回答
1

在大多数数据库中,您会使用 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外部查询,那么您必须输入整个表达式。

于 2013-01-13T16:25:12.463 回答