我刚刚计算了一个月内的工作日数并将其乘以8并将其存储为BaseHours
。它是一个SP,它返回许多列,包括BaseHours
。现在对于其他列我想在同一个SP中使用此BaseHours
,但是我收到错误。我正在使用SQL Server。
无效的列名'basehours'
(case when f.employmenttype = 3 then
(case when c.paqtyq > BaseHours then
(c.paqtyq + (c.paqtyq - BaseHours)) * 0.5
else
c.paqtyq
end)
else
BaseHours
end) as bhours
答案 0 :(得分:1)
我假设您实际上并未在现有表上创建计算列,而是将计算列作为结果集的一部分返回?
如果是这样,您需要存储结果集,直到完成结果集后才返回。
-- Create and store the result set
SELECT
...
INTO
#temp
FROM
...
-- Do your checks
???
-- Return the result set
SELECT
*
FROM
#temp
编辑我还在猜测,但我想我可能会理解你的问题......
您无法在select语句的其他位置重复使用别名列,因此无法正常工作......
SELECT
value * 1.15 AS price,
price * 0.95 AS discounted_price
FROM
stock_table
相反,你需要分两步完成......
SELECT
price,
price * 0.95 AS discounted_price
FROM
(
SELECT
value * 1.15 AS price
FROM
stock_table
)
AS prices
或者使用CTE,我觉得它更整洁......
WITH
prices AS
(
SELECT
value * 1.15 AS price
FROM
stock_table
)
SELECT
price,
price * 0.95 AS discounted_price
FROM
prices