数据:
+--------+------+------+------+------+
| WEEK_NO| Week1| Week2| Week3|Week4 |
+--------+------+------+------+------+
| 1 | 0.5 | 0.2 | 0.3 |0.4 |
| 2 | 0.5 | 0.2 | 0.3 |0.4 |
| 3 | 0.5 | 0.2 | 0.3 |0.4 |
| 4 | 0.5 | 0.2 | 0.3 |0.4 |
+--------+------+------+------+------+
期望的输出:
+--------+------+------+------+------+-------------+
| WEEK_NO| Week1| Week2| Week3|Week4 |COLUMN NEEDED|
+--------+------+------+------+------+-------------+
| 1 | 0.5 | 0.2 | 0.3 |0.4 | 0.5 |
| 2 | 0.5 | 0.2 | 0.3 |0.4 | 0.2 |
| 3 | 0.5 | 0.2 | 0.3 |0.4 | 0.3 |
| 4 | 0.5 | 0.2 | 0.3 |0.4 | 0.4 |
+--------+------+------+------+------+-------------+
答案 0 :(得分:1)
使用CASE
声明
SELECT CASE
WHEN WEEK_NO = 1 THEN Week1
WHEN WEEK_NO = 2 THEN Week2
WHEN WEEK_NO = 3 THEN Week3
WHEN WEEK_NO = 4 THEN Week4
END AS "COLUMN NEEDED"
FROM yourtable
或使用Decode
Select DECODE (WEEK_NO, 1, Week1,
2, Week2,
3, Week3,
4, Week4)
From yourtable
考虑更改表格结构
答案 1 :(得分:0)
原始表格行中的数据重复是非常可疑的。显然我们对这个结构的目的一无所知,但它看起来似乎是数据建模的失败。应用First Normal Form会给...
create table weeks (
week_no number primary key
, week_coeff number not null )
/
所以查询将是:
select week_no
, week_coeff as column_needed
from weeks
/
如果这看起来过于简单,请解释您正在建模的业务规则。
“但不知何故,数据出现在上述结构中,无法在源头进行更改。”
当然可以更改,它只是一个数据结构。但办公室政治是办公室政治。祝你好运,建立可怕的脆弱代码。