SQL - 根据其他单元格值计算单元格值

时间:2015-09-06 15:58:54

标签: sql oracle

所以我在处理一些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

我想添加包含指标240和241的值的新行,其中指标240的分数是指标45的分数/指标46的分数,并且类似地,指标241的分数是指标47的分数/指标的分数48.必须为每个ID的每个TimeID执行此操作。

完整表格很大,因为每个ID的ID数量,每个ID的TimeID和每个TimeID的IndicatorID都很大。

2 个答案:

答案 0 :(得分: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 
/

答案 1 :(得分: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 |