划分顺序记录



我在MS Access中有一个表格,如下所示:

桌子

+-----+-----+-----+
| 1st | 2nd | 3rd |
+-----+-----+-----+
| A   |   1 | 100 |
| A   |   2 | 200 |
| A   |   3 | 300 |
| B   |   1 | 100 |
| B   |   2 | 200 |
| B   |   3 | 300 |
| C   |   1 | 100 |
| C   |   2 | 200 |
| C   |   3 | 300 |
+-----+-----+-----+

现在我想从第 3 列读取值,对其进行某种操作并将它们存储到另一个表中,例如:

总结

+-----+---------+---------+
| 1st |   2nd   |   3rd   |
+-----+---------+---------+
| A   | 100/200 | 200/300 |
| B   | 100/200 | 200/300 |
| C   | 100/200 | 200/300 |
+-----+---------+---------+

换句话说,对于summary.2nd来说,这意味着:

select table.3rd FROM table where table.1st = A AND table.2nd = 1

除以

select table.3rd FROM table where table.1st = A AND table.2nd = 3

有人可以给我一个提示如何做到这一点吗?

也许是VBA/ADO记录集等?

一种方法是条件聚合:

select [1st],
       max(iif([2nd] = 1, [3rd], null)) / max(iif([2nd] = 2, [3rd], null)) as [2nd],
       max(iif([2nd] = 2, [3rd], null)) / max(iif([2nd] = 3, [3rd], null)) as [3rd]
from t
group by [1st];

试试这个SQL

INSERT INTO Summary 
SELECT DISTINCT a.[1st], 
                a.[3rd] / b.[3rd] AS [2nd], 
                a.[3rd] / c.[3rd] AS [3rd] 
FROM   ((tbl AS a 
         INNER JOIN tbl AS b 
                 ON a.[1st] = b.[1st]) 
        INNER JOIN tbl AS c 
                ON a.[1st] = c.[1st] ) 
WHERE  a.[2nd] = 1 
       AND b.[2nd] = 2 
       AND c.[2nd] = 3 

下面是另一种选择,使用计算的连接条件:

select 
    t1.[1st], 
    t1.[3rd]/t2.[3rd] as [2nd], 
    t2.[3rd]/t3.[3rd] as [3rd]
from 
    (
        [table] t1 inner join [table] t2 
        on t1.[1st] = t2.[1st] and t1.[2nd] = t2.[2nd]-1
    )
    inner join [table] t3 
    on t1.[1st] = t3.[1st] and t1.[2nd] = t3.[2nd]-2

由于2nd列值 1、2 和 3 不是硬编码的,因此这适用于 2nd 列中值依次相差 1 的任何三个整数。

[table]更改为表的名称。

最新更新