我有这个查询计算p值,但我想重用它来获得重要性。我不确定我是否可以重复使用该数据,或者我是否必须复制该部分。
这是我的查询
CASE WHEN value1 > 1
AND 1.0 - IF(
p_value1 IS NULL,
1.0,
p_value1
) >= 0.8 THEN p_value1 WHEN value1 <= 1
AND 1.0 - IF(
p_value2 [1.03] is NULL,
1.0,
p_value2 [1.03]
) >= 0.8 THEN p_value2 [1.03] END AS p_value,
...
FROM table1
WHERE dt = '2018-01-01'
正在做的是检查value1是否大于1并且p_value1大于0.8然后打印p_value2否则如果value1小于1则从p_value2 [1.03]获取值并将其打印为p_value。
现在我要展示的下一个专栏是1 - p_value
,所以我可以获得重要性。但我不想复制下一列的整个CASE
。它看起来会像这样。
p_value, significance (1-p_value)
0.03, 0.97
0.02, 0.98
答案 0 :(得分:1)
您的代码更简单:
SELECT (CASE WHEN value1 > 1 AND 1.0 - COALESCE(p_value1, 1.0) >= 0.8
THEN p_value1
WHEN value1 <= 1 AND 1.0 - COALESCE(p_value2, 1.0) > 0.8
THEN p_value2
END) AS p_value,
...
FROM table1
WHERE dt = '2018-01-01'
您不能在同一p_value
(或大多数其他条款)中的任何后续表达式中使用SELECT
。您可以使用CTE或子查询:
SELECT t.*, 1 - p_value
FROM (SELECT (CASE WHEN value1 > 1 AND 1.0 - COALESCE(p_value1, 1.0) >= 0.8
THEN p_value1
WHEN value1 <= 1 AND 1.0 - COALESCE(p_value2, 1.0) > 0.8
THEN p_value2
END) AS p_value,
...
FROM table1
WHERE dt = '2018-01-01'
) t