我正在构建一些基本的 sql 查询,并且在“group By”子句中遇到问题。我在列标题上使用了别名,但在尝试“分组”我的结果时遇到了问题。我目前的代码是:-
SELECT
vwREP_GP_ContractProjectPurchaseCostCatID.PACOSTCATID AS COST_CAT_CODE,
vwREP_GP_ContractProjectPurchaseCostCatID.PACOSTCATNME AS COST_CAT_NAME,
vwREP_GP_ContractProjectPurchaseRpt.COST,
vwREP_GP_ContractProjectPurchaseRpt.ITEMDESC AS ITEM,
vwREP_GP_ContractProjectPurchaseRpt.CREDITOR,
vwREP_GP_ContractProjectPurchaseRpt.DOC_NUM AS DOCUMENT_NUMBER,
vwREP_GP_ContractProjectPurchaseRpt.DESCRIPTION,
vwREP_GP_ContractProjectPurchaseRpt.DOC_DATE AS DATE,
vwREP_GP_ContractProjectPurchaseRpt.PROJECT AS JOB_CODE_STAGE,
vwREP_GP_ContractProjectPurchaseRpt.COMPOUNDNAME AS CATEGORY_DESCRIPTION
FROM
vwREP_GP_ContractProjectPurchaseCostCatID
INNER JOIN
vwREP_GP_ContractProjectPurchaseRpt ON wREP_GP_ContractProjectPurchaseCostCatID.PACOSTCATID = vwREP_GP_ContractProjectPurchaseRpt.PACOSTCATID
SELECT CATEGORY_DESCRIPTION, SUM(COST) AS TotalCost FROM
(
SELECT COMPOUNDNAME AS CATEGORY_DESCRIPTION FROM wREP_GP_ContractProjectPurchaseRpt
) A
GROUP BY CATEGORY_DESCRIPTION
添加了额外的代码来处理使用别名列名后,我现在收到一个错误,即“COST”是无效列?我没有更改此列名??
我知道它明显在盯着我看,但我的大脑没有参与,所以如果有人能指出我哪里出错了,或者更简单的方法,那将是一个很大的帮助。
提前致谢。
(这一切都在 sql server management studio 2005 上完成)