我正在尝试向列InvoiceTotal添加列别名'10%',但它声明列名'10%'不存在 如果我删除([10%] + InvoiceTotal)AS [加10%],结果将列为10%。 我做错了什么?
SELECT InvoiceTotal, (InvoiceTotal * .10) AS [10%], ([10%] + InvoiceTotal) AS [Plus 10%]
FROM Invoices
WHERE InvoiceTotal - PaymentTotal - CreditTotal > 500
ORDER BY InvoiceTotal DESC;
答案 0 :(得分:0)
在查询完成之前,别名不存在,并且您在该点之前尝试使用别名。
但修复很简单 - 只是不要使用别名。 : - )
SELECT
InvoiceTotal,
InvoiceTotal * .10 AS [10%],
InvoiceTotal + (InvoiceTotal * .10) AS [Plus 10%]
FROM
Invoices
WHERE
InvoiceTotal - PaymentTotal - CreditTotal > 500
ORDER BY
InvoiceTotal DESC;
答案 1 :(得分:0)
解决方案#1:
SELECT src.*, src.[10%], (src.[10%] + src.InvoiceTotal) AS [Plus 10%]
FROM
(
SELECT InvoiceTotal, (InvoiceTotal * .10) AS [10%]
FROM Invoices
WHERE InvoiceTotal - PaymentTotal - CreditTotal > 500
) src
ORDER BY src.InvoiceTotal DESC;
解决方案#2:
;WITH BaseQuery
AS
(
SELECT InvoiceTotal, (InvoiceTotal * .10) AS [10%]
FROM Invoices
WHERE InvoiceTotal - PaymentTotal - CreditTotal > 500
)
SELECT src.*, src.[10%], (src.[10%] + src.InvoiceTotal) AS [Plus 10%]
FROM BaseQuery src
ORDER BY src.InvoiceTotal DESC;
解决方案#3:
SELECT i.InvoiceTotal, x.[10%], (x.[10%] + i.InvoiceTotal) AS [Plus 10%]
FROM Invoices i
CROSS APPLY ( SELECT (i.InvoiceTotal * .10) AS [10%] ) x
WHERE i.InvoiceTotal - i.PaymentTotal - i.CreditTotal > 500
ORDER BY i.InvoiceTotal DESC;