我在声明中创建了动态列。在我的动态专栏中,我使用了各种CASE语句。现在,我无法在WHERE子句中过滤我的动态列。
SELECT
item1,
item2,
CASE WHEN item1 = '1' THEN '10' END AS item3 FROM "Table1"
WHERE item3 = '10';
在我的动态列上使用过滤器时,上面的语句给出了错误。
答案 0 :(得分:1)
错误是因为在Where
之前对Select
子句进行了逻辑处理,因此您无法使用Alias
子句中Select
生成的Where
名称。
表名
后还有一个不需要的逗号您可以直接在CASE
子句
Where
SELECT item1,
item2,
CASE WHEN item = '1' THEN '10' END AS item3
FROM "Table1" --, unwanted comma
WHERE CASE WHEN item = '1' THEN '10' END = '10'
如果您有复杂/嵌套的CASE
语句
答案 1 :(得分:0)
首先,你需要引用一个表,
SELECT
item1,
item2,
CASE WHEN item = '1' THEN '10' END AS item3
FROM TABLE
其次,item3
不是原始列,因为where
子句在select
之前执行,这意味着where
执行时item3
}不存在。
You could do this way:
SELECT * FROM (
SELECT
item1,
item2,
CASE WHEN item = '1' THEN '10' END AS item3,
FROM TABLE ) as A
WHERE A.item3 = '10'
答案 2 :(得分:0)
将您的查询包装在派生表中:
select item1, item2, item3
from
(
SELECT
item1,
item2,
CASE WHEN item = '1' THEN '10' END AS item3
FROM tablename
) dt
WHERE item3 = '10';
答案 3 :(得分:0)
//with cte
with tmp as (
SELECT
item1,
item2,
CASE WHEN item = '1' THEN '10' END AS item3
FROM "Table1"
)
select * from tmp where item3='10'
//with subselect
select * from (
SELECT
item1,
item2,
CASE WHEN item = '1' THEN '10' END AS item3
FROM "Table1"
) tmp where item3='10'
//with reuse case
SELECT
item1,
item2,
CASE WHEN item = '1' THEN '10' END AS item3
FROM tablename where CASE WHEN item = '1' THEN '10' END ='10'