這可以使用MODEL clause迎刃而解。
SQL Fiddle
select id, timeid, indicatorid, score
from myt
model return updated rows
partition by (id, timeid)
dimension by (indicatorid)
measures(score)
rules(
score[240] = score[45]/score[46],
score[241] = score[47]/score[48]
);
Results:
| ID | TIMEID | INDICATORID | SCORE |
|----|--------|-------------|--------------------|
| 2 | 111 | 241 | 0.8536585365853658 |
| 2 | 111 | 240 | 2.0833333333333335 |
| 1 | 112 | 241 | 0.9120879120879121 |
| 1 | 112 | 240 | 1.4285714285714286 |
| 2 | 112 | 241 | 0.8536585365853658 |
| 2 | 112 | 240 | 2.0833333333333335 |
| 1 | 111 | 241 | 0.9120879120879121 |
| 1 | 111 | 240 | 1.4285714285714286 |
insert into myt
select id, timeid, indicatorid, score
from myt
model return updated rows
partition by (id, timeid)
dimension by (indicatorid)
measures(score)
rules(
score[240] = score[45]/score[46],
score[241] = score[47]/score[48]
);
Results:
select id, timeid, indicatorid, score
from myt
Results:
| ID | TIMEID | INDICATORID | SCORE |
|----|--------|-------------|--------------------|
| 1 | 111 | 45 | 20 |
| 1 | 111 | 46 | 14 |
| 1 | 111 | 47 | 83 |
| 1 | 111 | 48 | 91 |
| 1 | 111 | 240 | 1.4285714285714286 |
| 1 | 111 | 241 | 0.9120879120879121 |
| 1 | 112 | 45 | 20 |
| 1 | 112 | 46 | 14 |
| 1 | 112 | 47 | 83 |
| 1 | 112 | 48 | 91 |
| 1 | 112 | 240 | 1.4285714285714286 |
| 1 | 112 | 241 | 0.9120879120879121 |
| 2 | 111 | 45 | 25 |
| 2 | 111 | 46 | 12 |
| 2 | 111 | 47 | 70 |
| 2 | 111 | 48 | 82 |
| 2 | 111 | 240 | 2.0833333333333335 |
| 2 | 111 | 241 | 0.8536585365853658 |
| 2 | 112 | 45 | 25 |
| 2 | 112 | 46 | 12 |
| 2 | 112 | 47 | 70 |
| 2 | 112 | 48 | 82 |
| 2 | 112 | 240 | 2.0833333333333335 |
| 2 | 112 | 241 | 0.8536585365853658 |
沒有什麼可以 「迎刃而解」 使用MODEL子句:) – APC