2012-09-10 4 views
-2

가능한 중복 :
Sql Query throwing error쿼리 오류

내가 개월 분열과에 따라 표시에 따라 기록을 검색하는 쿼리를 시도하고 내가 요약해야하는 현명한 급여 수당 월, 또한 달 현명한 그들을 보여줍니다. 여기

쿼리 메신저하려고 : -

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 [email protected]_id; 
group by 
    pmc.[month] 

임 다음과 같은 오류를 가지고 : - 그것은 집계 함수 또는 중 하나에 포함되지 않기 때문에 열 'TBL_PMC.pd_name_of_project'는 선택 목록에서 유효하지 않습니다 GROUP BY 절. 모든 열에 집계 함수를 사용하고 싶지 않습니다 ... 몇 열을 요약해야합니다 !!

답변

0

GROUP 문을 사용하면 GROUP BY에 포함되거나 집계 함수가있는 SELECT의 열을 사용할 수 있습니다. - 오류 메시지가 말한대로.

이 경우 SUM...OVER (PARTITION BY ...) 절을 사용할 수 있습니다. MSDN을 확인하십시오.

그래서 선으로 그룹을 삭제하고 다음과 같이 귀하의 합을 변경

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

파티션을 .....하지만 데이터로 오는 6 월 한 달이 3 개 기록 .... 3 개 행이있는 경우 일이 ...입니다 : –

2

원하지 않을 수도 있지만 꼭해야합니다. 또는 열을 GROUP BY 섹션에 놓으십시오. 이전 대답과 마찬가지로

0

을 요구하는 GROUP BY에 필드를 배치. 데이터 쿼리 불구하고 다음을 사용할 수 있습니다 :이 서브 쿼리 내에서 SUM()을 배치

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 [email protected]_id; 

을 그리고 당신은 하위 쿼리 만 GROUP BY 몇 가지 필드는 다음 나머지를 얻을 것이다 원하는 필드를 TBL_PMCJOIN 다시 테이블. 좋은 작품으로

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 [email protected]_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 [email protected]_id; 
group by 
    pmc.[month]) b 

on a.month = b.month 
0

는 다음과 같이 시도 급여 수당의 합계도 3 회 표시됩니다. [동일한 수의 행이 표시 될 때마다 동일한 수의 행이 표시됩니다.] 이 문제를 해결하기위한 아이디어. 한 달에 한 번만 합계를 표시하고 싶습니다.