如何在Select Sql语句中使用自定义列

时间:2014-03-17 20:03:46

标签: sql sql-server

我有一个选择sql命令,如下:

SELECT Title,G ,K ,M,
 CASE 
     WHEN (tblMain.G <> 0 AND tblMain.K <> 0 AND tblMain.M = 0) then (select sum(price) from tblCustomer where tblCustomer.G = tblMain.G AND tblCustomer.K = tblMain.K)
     WHEN (tblMain.G <> 0 AND tblMain.K = 0 AND tblMain.M = 0) then (select sum(price) from tblCustomer where tblCustomer.G = tblMain.G)
     ELSE 0 
 END AS B1,
 CASE 
     WHEN (tblMain.G <> 0 tblMain.K <> 0 AND tblMain.M = 0) then (select sum(price) from tblCustomer where tblCustomer.G = tblMain.G AND tblCustomer.K = tblMain.K)
     WHEN (tblMain.G <> 0 AND tblMain.K = 0 AND tblMain.M = 0) then (select sum(price) from tblCustomer where tblCustomer.G = tblMain.G)
     else 0 
 END AS S1,
 (S1 - B1) AS Result
FROM tblMain

但我的问题是,&#34;(S1 - B1)AS结果&#34;声明不起作用并让我犯错:

Invalid column name 'S1'.
Invalid column name 'B1'.

如何解决这个问题?

3 个答案:

答案 0 :(得分:1)

with SubQuery as
(

    SELECT Title,G ,K ,M,
     CASE 
         WHEN (tblMain.G <> 0 AND tblMain.K <> 0 AND tblMain.M = 0) then (select sum(price) from tblCustomer where tblCustomer.G = tblMain.G AND tblCustomer.K = tblMain.K)
         WHEN (tblMain.G <> 0 AND tblMain.K = 0 AND tblMain.M = 0) then (select sum(price) from tblCustomer where tblCustomer.G = tblMain.G)
         ELSE 0 
     END AS B1,
     CASE 
         WHEN (tblMain.G <> 0 tblMain.K <> 0 AND tblMain.M = 0) then (select sum(price) from tblCustomer where tblCustomer.G = tblMain.G AND tblCustomer.K = tblMain.K)
         WHEN (tblMain.G <> 0 AND tblMain.K = 0 AND tblMain.M = 0) then (select sum(price) from tblCustomer where tblCustomer.G = tblMain.G)
         else 0 
     END AS S1
    FROM tblMain
)
select e.*,  (e.S1 - e.B1) AS Result  from SubQuery e

答案 1 :(得分:0)

您不能以这种方式重用别名。您可以使用子查询:

SELECT Title,G ,K ,M, B1, S1, (S1 - B1) AS Result FROM

(SELECT Title,G ,K ,M,
 CASE 
     WHEN (tblMain.G <> 0 AND tblMain.K <> 0 AND tblMain.M = 0) then (select sum(price) from tblCustomer where tblCustomer.G = tblMain.G AND tblCustomer.K = tblMain.K)
     WHEN (tblMain.G <> 0 AND tblMain.K = 0 AND tblMain.M = 0) then (select sum(price) from tblCustomer where tblCustomer.G = tblMain.G)
     ELSE 0 
 END AS B1,
 CASE 
     WHEN (tblMain.G <> 0 tblMain.K <> 0 AND tblMain.M = 0) then (select sum(price) from tblCustomer where tblCustomer.G = tblMain.G AND tblCustomer.K = tblMain.K)
     WHEN (tblMain.G <> 0 AND tblMain.K = 0 AND tblMain.M = 0) then (select sum(price) from tblCustomer where tblCustomer.G = tblMain.G)
     else 0 
 END AS S1
FROM tblMain) TMP

答案 2 :(得分:0)

由于您似乎不需要中间值,因此您可以直接计算结果。

SELECT Title,G ,K ,M,
 CASE 
     WHEN (tblMain.G <> 0 tblMain.K <> 0 AND tblMain.M = 0) then (select sum(price) from tblCustomer where tblCustomer.G = tblMain.G AND tblCustomer.K = tblMain.K)
     WHEN (tblMain.G <> 0 AND tblMain.K = 0 AND tblMain.M = 0) then (select sum(price) from tblCustomer where tblCustomer.G = tblMain.G)
     else 0 
 END -  CASE 
     WHEN (tblMain.G <> 0 AND tblMain.K <> 0 AND tblMain.M = 0) then (select sum(price) from tblCustomer where tblCustomer.G = tblMain.G AND tblCustomer.K = tblMain.K)
     WHEN (tblMain.G <> 0 AND tblMain.K = 0 AND tblMain.M = 0) then (select sum(price) from tblCustomer where tblCustomer.G = tblMain.G)
     ELSE 0 
 END AS Result
FROM tblMain