2015-09-06 285 views
0

因此,我在處理某些SQL編碼時遇到了問題。 我有看起來有點像這樣的數據表:SQL - 基於其他單元格值計算單元格值

ID TimeID IndicatorID  Score 
1  111   45    20 
1  111   46    14 
1  111   47    83 
1  111   48    91 
1  112   45    20 
1  112   46    14 
1  112   47    83 
1  112   48    91 
2  111   45    25 
2  111   46    12 
2  111   47    70 
2  111   48    82 
2  112   45    25 
2  112   46    12 
2  112   47    70 
2  112   48    82 

我要添加包含指示器46,用於指示240和241的值,其中所述分數指示符240是用於指示器45 /得分得分新行和類似地,指標241的得分是指標47的得分/指標48的得分。這必須針對每個ID的每個TimeID完成。

整個表格很大,因爲每個TimeID的ID,TimeID和每個TimeID的IndicatorID很大。

回答

0

假設你的要求爲表示,所有的IndicatorID值是硬編碼這可以通過一些簡單的子查詢和一個簡單的INSERT語句來完成:

insert into your_table 
with yt as (
    select * from your_table where IndicatorID in (45,46,47,48) 
    ) 
    , yt45 as (select * from yt where IndicatorID = 45) 
    , yt46 as (select * from yt where IndicatorID = 46) 
    , yt47 as (select * from yt where IndicatorID = 47) 
    , yt48 as (select * from yt where IndicatorID = 48) 
select yt45.id 
     , yt45.timeID 
     , 240 as IndicatorID 
     , yt45.score/yt46.score as score 
from yt45 
    join yt46 
    on yt45.id = yt46.id 
     and yt45.timeID = yt46.timeID 
union all 
select yt47.id 
     , yt47.timeID 
     , 240 as IndicatorID 
     , yt47.score/yt48.score as score 
from yt47 
    join yt48 
    on yt47.id = yt48.id 
     and yt47.timeID = yt48.timeID 
/
0

這可以使用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 | 
+0

沒有什麼可以 「迎刃而解」 使用MODEL子句:) – APC

相關問題