Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Divide Sequential Records

Tags:

sql

ms-access

I have a table in MS Access like:

table

+-----+-----+-----+
| 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 |
+-----+-----+-----+

Now I want to read the values from the 3rd column, do some sort of manipulation to it and store them in to another table like:

summary

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

In another words, for summary.2nd this means:

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

divided by

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

Can someone give me a hint how this could be done?

Maybe VBA / ADO Recordset etc?

like image 693
Jbill Avatar asked Jan 24 '26 03:01

Jbill


1 Answers

One method is conditional aggregation:

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];
like image 122
Gordon Linoff Avatar answered Jan 26 '26 17:01

Gordon Linoff