-2

可能重复:
Sql 查询抛出错误

我正在尝试查询按部门检索记录并按月份显示,我必须按月汇总工资津贴,并按月显示。

这是我正在尝试的查询:-

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.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 Division'
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 
    --and pmc.unit_id=@unit_id;
group by  
    pmc.[month]

我有以下错误:- 列“TBL_PMC.pd_name_of_project”在选择列表中无效,因为它不包含在聚合函数或 GROUP BY 子句中。我不想在所有列上使用聚合函数......我必须总结几列!

4

4 回答 4

2

你可能不想,但你必须这样做。或将列放在该GROUP BY部分中。就像之前的回答告诉你的

于 2012-09-10T10:05:37.100 回答
0

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

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

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

SUM(pmc.salary_allowance) OVER(PARTITION BY pmc.[month])
于 2012-09-10T10:06:59.490 回答
0

将字段放在 aGROUP BY中是必需的。您也许可以使用以下方法来查询数据:

select  pmc1.[month] as 'Month', 
    pmc2.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',
    pmc2.pd_project_type as 'Project Type',
    pmc2.accepted_tender_cost as 'Accepted Tender Cost',
    pmc2.work_order_date as 'Work Order Date',
    pmc2.tender_period_months as 'Tender Period',
    pmc2.project_completion_date as 'Project Completion Date',
    pmc2.per_pmc_charges as '% Of PMC Charges',
    pmc2.total_pmc_charges_scheme as 'Total PMC amount   of the Scheme',
    pmc2.bill_amount_certified_upto_previous_month as 'Bill amount certified upto previous Month',     
    pmc2.total_PMC_charges_upto_previous_month as 'Total PMC charges  upto previous Month',
    pmc2.receipt_PMC_charges_upto_previous_month as 'Receipt of PMC Charges upto previous Month',
    pmc2.balance_of_PMC_charges_upto_previous_month as 'Balance of PMC charges upto previous Month',
    pmc2.bill_amount_certified_current_month as 'Bill amount certified During Current Month',
    pmc2.PMC_charges_for_current_month as ' PMC charges  During Current Month',
    pmc2.receipt_PMC_charges_current_month as 'Receipt of PMC Charges During Current Monthh',
    pmc2.balance_of_PMC_charges_current_month as 'Balance of PMC charges During Current Month',
    pmc1.salary_allowance as 'Salary & Allowance Division'
FROM
(
    SELECT [month] as 'Month',
        SUM(pmc.salary_allowance) salary_allowance
    FROM TBL_PMC
    GROUP BY [month]
) pmc1 
INNER JOIN TBL_PMC pmc2
    ON pmc1.[month] = pmc2.[month]
INNER JOIN TBL_DIV 
    ON TBL_DIV.ID = pmc2.DIV_ID
LEFT OUTER JOIN TBL_PMC_UNIT 
    ON TBL_PMC_UNIT.ID=pmc2.UNIT_ID  
WHERE pmc2.div_id= 17 
    --and pmc.unit_id=@unit_id;

这会将您SUM()放在子查询中,然后您将只在子查询中使用几个字段,然后再次从表中获取您GROUP BY想要的剩余字段。TBL_PMCJOIN

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

试试这样:

select a.*,b.[Salary & Allowance Division] from 
(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.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'
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 
    --and pmc.unit_id=@unit_id;
group by  
    pmc.[month]
) a left join 

(select  
       pmc.[month] as 'Month', 
       SUM(pmc.salary_allowance) as 'Salary & Allowance Division'
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 
    --and pmc.unit_id=@unit_id;
group by  
    pmc.[month]) b

on a.month = b.month
于 2012-09-10T10:08:56.303 回答