2015-09-06 293 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

相关问题