1

添加到以前工作的查询后SUM(b.[Amount_withDiscount]),我收到选择查询的以下错误。inner join on tables现在我希望查询也返回相关表中该列的总和。我哪里出错了?

您尝试执行的查询不包含指定表达式 'p.[PFirstName] & ' ' & p.[PLastName]' 作为聚合函数的一部分。

工作查询

string sql = "SELECT p.[PFirstName]&' '&p.[PLastName] as [Patient Name],p.[Age],p.[BirthDate],p.[MobileNo]&' / '&p.[LandlineNo] as [Contact Number] FROM Patient_Registration p WHERE datepart('m',p.[RegDate])=@month AND datepart('yyyy',p.[RegDate])=@year AND p.DoctorID=" + drId;   

加总和内连接后查询

string sql = "SELECT p.[PFirstName]&' '&p.[PLastName] as [Patient Name],p.[Age],p.[BirthDate],p.[MobileNo]&' / '&p.[LandlineNo] as [Contact Number], SUM(b.[Amount_withDiscount]) as [Payable Amount] FROM Patient_Registration p INNER JOIN Bill_Master b on p.[PatientID]=b.[Patient_ID] WHERE datepart('m',p.[RegDate])=@month AND datepart('yyyy',p.[RegDate])=@year AND p.DoctorID=" + drId;     
4

1 回答 1

1

添加SUM()表达式时,您将查询从常规选择查询更改为聚合查询,该查询期望每个输出字段为

  • 聚合函数的一部分(SUM()MAX()等),或
  • 包含在 GROUP BY 子句中(查询中不存在)。

在这种情况下,只需一笔钱,您最好使用DSum()而不是SUM(),如

string sql = "SELECT p.[PFirstName]&' '&p.[PLastName] as [Patient Name],p.[Age],p.[BirthDate],p.[MobileNo]&' / '&p.[LandlineNo] as [Contact Number], DSum(\"Amount_withDiscount\",\"Bill_Master\",\"Patient_ID=\" & p.[PatientID]) as [Payable Amount] FROM Patient_Registration p WHERE datepart('m',p.[RegDate])=@month AND datepart('yyyy',p.[RegDate])=@year AND p.DoctorID=" + drId;     
于 2013-10-21T09:19:26.277 回答