从表中存在的两个SQL填充字段

时间:2013-06-18 08:28:50

标签: sql select

我想在我的表中选择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.
你能告诉我什么吗?

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

2 个答案:

答案 0 :(得分:0)

你必须重复第一个和第三个条件,而新表格还不存在。

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)

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

您无法在查询中指明列别名