รหัส SQL ต่อไปนี้แสดงข้อผิดพลาด [ซ้ำกัน]

    SELECT fp.Physician_Key,
       fp.Month,
       pd.DisplayName,
       hd.ProductName,
       SUM(AmtPaid) AS TotalCost
FROM F_ProgramCost_Fact fp
INNER JOIN D_HEALTHPLANDim hd ON hd.HealthPlan_Key = fp.HealthPlan_Key
INNER JOIN D_PHYSICIANDim pd ON fp.Physician_Key = fp.Physician_Key
INNER JOIN F_MemberPatient_FactLess mpf ON fp.MemberPatientFact_Key = mpf.MemberPatientFact_Key
GROUP BY fp.Physician_Key

การรับข้อผิดพลาดนี้ "คอลัมน์ 'F_ProgramCost_Fact.Month' ไม่ถูกต้องในรายการที่เลือก เนื่องจากไม่มีอยู่ในฟังก์ชันการรวมหรือส่วนคำสั่ง GROUP BY"

กรุณาช่วย! ขอบคุณ


person Saba Malik    schedule 18.08.2016    source แหล่งที่มา
comment
ในขณะที่คุณ SUMming คุณคาดหวังให้กลไก SQL จัดการกับค่าที่แตกต่างกันสำหรับคอลัมน์ F_ProgramCost_Fact.Month ได้อย่างไร แล้วคอลัมน์อื่นๆ ล่ะ? คุณต้องเพิ่มลงใน GROUP BY เพื่อที่คุณจะได้ SUM จากการรวมคอลัมน์อื่นๆ แต่ละคอลัมน์   -  person PauloASilva    schedule 18.08.2016


คำตอบ (1)


คุณต้องจัดกลุ่มตามคอลัมน์ทั้งหมด:

select fp.Physician_Key,fp.Month,pd.DisplayName,hd.ProductName,SUM(AmtPaid) as TotalCost
From F_ProgramCost_Fact fp
Inner Join D_HEALTHPLANDim hd
ON hd.HealthPlan_Key = fp.HealthPlan_Key
Inner join D_PHYSICIANDim pd
ON fp.Physician_Key = fp.Physician_Key 
Inner Join F_MemberPatient_FactLess mpf ON fp.MemberPatientFact_Key = mpf.MemberPatientFact_Key
Group By fp.Physician_Key,fp.Month,pd.DisplayName,hd.ProductName
person Mustapha Larhrouch    schedule 18.08.2016
comment
ขอบคุณที่ช่วยเหลือฉัน! - person Saba Malik; 18.08.2016