0

我想在我的表中选择 3 个字段:第一个和第二个是bool. 第三是(fist = 0 && second = 0)

我试过这个:

SELECT
CASE ..condition1..
    THEN 1
    ELSE 0
END
AS first,
CASE ..condition2..
    THEN 1
    ELSE 0
END
AS second,
CASE 
    WHEN newTable.first = 0 AND newTable.second = 0 
    THEN 1
    ELSE 0
END
AS third
INTO newTable
FROM oldTable

但我收到一个错误:

The multi-part identifier "newTable.first" could not be bound;
The multi-part identifier "newTable.second" could not be bound.

你能给我一些建议吗?

有没有办法填写此查询中的第三个字段?

4

2 回答 2

0

您必须重复第一个和第三个条件,并且 newtable 尚不存在。

SELECT
CASE ..condition1..
    THEN 1
    ELSE 0
END
AS first,
CASE ..condition2..
    THEN 1
    ELSE 0
END
AS second,
CASE 
    WHEN ..condition1.. OR ..condition2.. 
    THEN 1
    ELSE 0
END
AS third
INTO newTable
FROM oldTable

笔记:

newTable.first = 0 AND newTable.second = 0 

= NOT ..condition1.. AND  NOT ..condition2..

= ..condition1.. OR ..condition2.. (by applying de'morgans law)
于 2013-06-18T08:36:15.793 回答
-1

这是你的答案

select *,CASE WHEN tblTemo.first = 0 AND tblTemo.second = 0  THEN 1  ELSE 0 END from (

SELECT
CASE ..condition1..
    THEN 1
    ELSE 0
END
AS first,
CASE ..condition2..
    THEN 1
    ELSE 0
END
AS second
) as tblTemo
INTO newTable
FROM oldTable

您不能在查询中指导您的列别名

于 2013-06-18T08:37:36.553 回答