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]; 

这个查询给了我错误:-

列“TBL_PMC.pd_name_of_project”在选择列表中无效,因为它既不包含在聚合函数中,也不包含在 GROUP BY 子句中。

4

4 回答 4

0

您收到该错误的原因是,当您执行 SUM() 函数时,您必须按返回的任何列进行分组。

于 2012-09-10T08:48:28.483 回答
0

由于pmc.[month]是 group by 子句中列出的查询中唯一的列名,因此它是唯一可能出现的列名,而无需在列列表中与聚合函数一起使用。很难说你想用你的查询做什么,从它的外观来看,分组可能不是这个方法。

于 2012-09-10T08:48:42.333 回答
0

使用 GROUP 语句,您可以使用 SELECT 中包含在 GROUP BY 中或具有聚合函数的列。- 正如错误消息所说。

在这种情况下,您可以尝试使用 SUM...OVER (PARTITION BY ...) 子句。检查MSDN

因此,删除 Group By 行并像这样更改您的总和:

SUM(pmc.salary_allowance) OVER(PARTITION BY pmc.[month])

于 2012-09-10T10:08:02.890 回答
0

您必须对 select 语句中的所有列使用 MIN()、MAX()、AVG() 之类的聚合函数,除了 pmc.[month] 列,因为它在 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];
于 2012-09-10T08:51:34.560 回答