如何在 Select Sql 语句中使用自定义列
Posted
技术标签:
【中文标题】如何在 Select Sql 语句中使用自定义列【英文标题】:How Use Custom Column in Select Sql Statement 【发布时间】:2014-03-17 20:03:46 【问题描述】:我有一个 select 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
但我的问题是,“(S1 - B1) AS Result”语句不起作用并让我陷入错误:
Invalid column name 'S1'.
Invalid column name 'B1'.
如何解决这个问题?
【问题讨论】:
【参考方案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
【讨论】:
【参考方案2】:您不能以这种方式重复使用别名。您可以使用子查询:
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
【讨论】:
【参考方案3】:由于您似乎不需要中间值,您可以直接计算结果。
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
【讨论】:
以上是关于如何在 Select Sql 语句中使用自定义列的主要内容,如果未能解决你的问题,请参考以下文章
sql server 怎样用select语句调用自定义表值函数