2012-09-10 97 views
0
SELECT pmc.[month]         AS 'Month', 
     pmc.pd_name_of_project       AS 'Name of Project', 
     tbl_div.name         AS 'Name of Advisory Services Division', 
     TBL_PMC_UNIT.UNIT_NAME       AS 'Name of Unit', 
     pmc.staff_engineers, 
     pmc.staff_clerical, 
     pmc.staff_peons, 
     pmc.pd_project_type       AS 'Project Type', 
     pmc.accepted_tender_cost      AS 'Accepted Tender Cost', 
     pmc.work_order_date       AS 'Work Order Date', 
     pmc.tender_period_months      AS 'Tender Period', 
     pmc.project_completion_date     AS 'Project Completion Date', 
     pmc.per_pmc_charges       AS '% Of PMC Charges', 
     pmc.total_pmc_charges_scheme     AS 'Total PMC amount of the Scheme', 
     pmc.bill_amount_certified_upto_previous_month AS 'Bill amount certified upto previous Month', 
     pmc.total_PMC_charges_upto_previous_month  AS 'Total PMC charges upto previous Month', 
     pmc.receipt_PMC_charges_upto_previous_month AS 'Receipt of PMC Charges upto previous Month', 
     pmc.balance_of_PMC_charges_upto_previous_month AS 'Balance of PMC charges upto previous Month', 
     pmc.bill_amount_certified_current_month  AS 'Bill amount certified During Current Month', 
     pmc.PMC_charges_for_current_month    AS ' PMC charges During Current Month', 
     pmc.receipt_PMC_charges_current_month   AS 'Receipt of PMC Charges During Current Monthh', 
     pmc.balance_of_PMC_charges_current_month  AS 'Balance of PMC charges During Current Month', 
     SUM(pmc.salary_allowance)      AS 'Salary Allowance' 
FROM TBL_PMC pmc 
     INNER JOIN TBL_DIV 
     ON TBL_DIV.ID = pmc.DIV_ID 
     LEFT OUTER JOIN TBL_PMC_UNIT 
     ON TBL_PMC_UNIT.ID = pmc.UNIT_ID 
WHERE pmc.div_id = 17 
GROUP BY pmc.[month]; 

這個查詢是給我的錯誤: -SQL查詢拋出錯誤

,因爲它不是在聚合函數或GROUP載列「TBL_PMC.pd_name_of_project」在選擇列表中無效BY子句。

+1

您的查詢是沒有意義的。你想做什麼? –

回答

0

您得到該錯誤的原因是,當您執行SUM()函數時,您必須按任何正在返回的列進行分組。

0

由於pmc.[month]是group by子句中列出的查詢中唯一的組合,因此它是列名稱中唯一可能出現的沒有使用聚合函數的列名。很難說出你想要用你的查詢來做什麼,通過它的外觀,分組可能不是這個方法。

0

,你必須使用聚合功能,如MIN(),MAX(),AVG()用於除PMC select語句中的所有列。[月],因爲它是在group by操作中使用的列

您的查詢應該是這樣的:

select pmc.[month] as 'Month', 
max(pmc.pd_name_of_project) as 'Name of Project', 
max(tbl_div.name) AS 'Name of Advisory Services Division', 
max(TBL_PMC_UNIT.UNIT_NAME) AS 'Name of Unit', 
......... 
......... 
SUM(pmc.salary_allowance) as 'Salary Allowance' 
FROM  TBL_PMC pmc 
INNER JOIN TBL_DIV 
ON   TBL_DIV.ID = pmc.DIV_ID 
LEFT OUTER JOIN TBL_PMC_UNIT 
ON   TBL_PMC_UNIT.ID=pmc.UNIT_ID 
WHERE  pmc.div_id= 17 
GROUP by pmc.[month]; 
0

大量可以在其中都包含在GROUP BY或具有聚合函數的SELECT中使用的列的GROUP語句。 - 如錯誤消息所述。

在這種情況下,您可以嘗試使用SUM ... OVER(PARTITION BY ...)子句。檢查MSDN

所以刪除該組一行,並改變你的總和是這樣的:(PARTITION BY PMC [月])

SUM(pmc.salary_allowance)OVER