在 SUM 函数中引用别名 - SQL 服务器
Reference an ALIAS in a SUM funciton - SQL Server
背景:
我正在尝试计算查询内的利润率,但我 运行 出错了。当我尝试在 SUM 函数中使用 select 语句时,我触发了一个错误:
Cannot perform an aggregate function on an expression containing an
aggregate or a subquery.
我知道这是由于在 SUM 函数中有一个 SELECT 查询引起的。从那里,我尝试引用 COGS 列的别名。我这样做时也收到错误消息:
Invalid column name 'COGS'.
在进一步处理查询之后,我想这可能是因为我在 SUM 函数中尝试了所有这些,所以我删除了它和 运行 查询。它返回了一些错误:
- Column 'tbl_invoice.subTotal' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
- Column 'tbl_invoice.tradeinAmount' is invalid in the select list because it is not contained in either an aggregate function or the
GROUP BY clause.
- Column 'tbl_invoice.subTotal' is invalid in the select list because it is not contained in either an aggregate function or the
GROUP BY clause.
- Column 'tbl_invoice.tradeinAmount' is invalid in the select list because it is not contained in either an aggregate function or the
GROUP BY clause.
- Column 'tbl_invoice.subTotal' is invalid in the select list because it is not contained in either an aggregate function or the
GROUP BY clause.
- Column 'tbl_invoice.tradeinAmount' is invalid in the select list because it is not contained in either an aggregate function or the
GROUP BY clause.
是否有其他方法可以在 SUM 函数中使用或引用我需要的值?
查询:
--Main query
SELECT
custID,
COUNT(custID) AS InvoiceNum,
--This is the column that has an alias
(SELECT cogs FROM #tempMarketing where #tempMarketing.custID = tbl_invoice.custID) as COGS,
--This is where I am trying to calculate the profit margin
SUM(((((subTotal + (-1 * tradeinAmount) - (SELECT cogs FROM #tempMarketing where #tempMarketing.custID = tbl_invoice.custID)))
/ (NULLIF(subTotal + (-1 * tradeinAmount),0))) *100)) as Profitmargin,
FROM tbl_invoice
group by custID
order by InvoiceNum desc;
您可以尝试以下查询,我为 cogs 列创建了一个通用 table 表达式:
WITH cte_base AS(
SELECT cogs FROM #tempMarketing where #tempMarketing.custID = tbl_invoice.custID
)
SELECT
custID,
COUNT(custID) AS InvoiceNum,
--This is the column that has an alias
cte_base.cogs as COGS,
--This is where I am trying to calculate the profit margin
SUM(((((subTotal + (-1 * tradeinAmount) - (cte_base.cogs)))
/ (NULLIF(subTotal + (-1 * tradeinAmount),0))) *100)) as Profitmargin,
FROM tbl_invoice
group by custID
order by InvoiceNum desc;
SELECT
a.custID,
COUNT(a.custID) AS InvoiceNum, case when a.custid=b.custid then b.cogs else 0 end
as COGS,
SUM(((((subTotal + (-1 * tradeinAmount) - case when a.custid=b.custid then b.cogs else 0 end))
/ (NULLIF(subTotal + (-1 * tradeinAmount),0))) *100)) as Profitmargin,
FROM tbl_invoice a
left join #tempMarketing b on a.custID =b.custid
group by a.custID,
case when a.custid=b.custid then b.cogs else null end
order by InvoiceNum desc;
背景:
我正在尝试计算查询内的利润率,但我 运行 出错了。当我尝试在 SUM 函数中使用 select 语句时,我触发了一个错误:
Cannot perform an aggregate function on an expression containing an
aggregate or a subquery.
我知道这是由于在 SUM 函数中有一个 SELECT 查询引起的。从那里,我尝试引用 COGS 列的别名。我这样做时也收到错误消息:
Invalid column name 'COGS'.
在进一步处理查询之后,我想这可能是因为我在 SUM 函数中尝试了所有这些,所以我删除了它和 运行 查询。它返回了一些错误:
- Column 'tbl_invoice.subTotal' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
- Column 'tbl_invoice.tradeinAmount' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
- Column 'tbl_invoice.subTotal' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
- Column 'tbl_invoice.tradeinAmount' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
- Column 'tbl_invoice.subTotal' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
- Column 'tbl_invoice.tradeinAmount' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
是否有其他方法可以在 SUM 函数中使用或引用我需要的值?
查询:
--Main query
SELECT
custID,
COUNT(custID) AS InvoiceNum,
--This is the column that has an alias
(SELECT cogs FROM #tempMarketing where #tempMarketing.custID = tbl_invoice.custID) as COGS,
--This is where I am trying to calculate the profit margin
SUM(((((subTotal + (-1 * tradeinAmount) - (SELECT cogs FROM #tempMarketing where #tempMarketing.custID = tbl_invoice.custID)))
/ (NULLIF(subTotal + (-1 * tradeinAmount),0))) *100)) as Profitmargin,
FROM tbl_invoice
group by custID
order by InvoiceNum desc;
您可以尝试以下查询,我为 cogs 列创建了一个通用 table 表达式:
WITH cte_base AS(
SELECT cogs FROM #tempMarketing where #tempMarketing.custID = tbl_invoice.custID
)
SELECT
custID,
COUNT(custID) AS InvoiceNum,
--This is the column that has an alias
cte_base.cogs as COGS,
--This is where I am trying to calculate the profit margin
SUM(((((subTotal + (-1 * tradeinAmount) - (cte_base.cogs)))
/ (NULLIF(subTotal + (-1 * tradeinAmount),0))) *100)) as Profitmargin,
FROM tbl_invoice
group by custID
order by InvoiceNum desc;
SELECT
a.custID,
COUNT(a.custID) AS InvoiceNum, case when a.custid=b.custid then b.cogs else 0 end
as COGS,
SUM(((((subTotal + (-1 * tradeinAmount) - case when a.custid=b.custid then b.cogs else 0 end))
/ (NULLIF(subTotal + (-1 * tradeinAmount),0))) *100)) as Profitmargin,
FROM tbl_invoice a
left join #tempMarketing b on a.custID =b.custid
group by a.custID,
case when a.custid=b.custid then b.cogs else null end
order by InvoiceNum desc;