行上的SQL SUM和计算不起作用

时间:2018-04-10 12:21:08

标签: sql sql-server sum

我有以下数据库图。 E-R Diagram 我想根据客户在特定日期范围内创建发票。

我的SQL:

SELECT 
    Treatment.Treatment_Date, 
    Salutation.Value as Title,
    Customer.Firstname,
    Customer.Lastname,
    Address.Value as Street, 
    Postal.Postal, 
    Postal.City, 
    Patient.Name as AnimalName,
    TreatmentType.Type as Treatment,
    Treatment.Price,
    Tax.Value as Tax,
    SUM(Treatment.Price) as TotalExTax,
    SUM(Treatment.Price * Tax.Value / 100) as TotalTax,
    SUM(Treatment.Price) + (SUM(Treatment.Price * Tax.Value / 100)) as TotalPrice

FROM Treatment
INNER JOIN Patient ON Treatment.Patient_FK = Patient.ID
INNER JOIN AnimalType ON Patient.Type_FK = AnimalType.ID
INNER JOIN Customer ON Patient.Owner_FK = Customer.ID
INNER JOIN TreatmentType ON Treatment.TreatmentType_FK = TreatmentType.ID
INNER JOIN Salutation ON Customer.Salutation_FK = Salutation.ID
INNER JOIN Address ON Customer.Address_FK = Address.ID
INNER JOIN Postal ON Address.Postal_FK = Postal.Postal
INNER JOIN Tax ON Treatment.Tax_FK = Tax.ID

WHERE Customer.ID = 151 AND Treatment.Treatment_Date between '11/12/2014 00:00:00 AM' and '12/12/2014 12:00:00 PM'

我的问题在于我的SUM。 TotalExTax,应该计算所有"治疗的总价格" TotalTax,应该给我总税(25%) TotalPrice,应该给我包含税的价格。

... BUT 它似乎给我随机错误.. 现在它只是说:

Error 8120: Column 'Treatment.Treatment_Date' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.

我已经搜索了这个问题,据我所知,这是正确的。

5 个答案:

答案 0 :(得分:1)

当您进行这样的聚合时,您需要有一个GROUP BY,您可以将其放在WHERE子句下面

    guard let dictRes = result as? [String : Any] else {
        print("No data")
        return
    }

    guard let data = dictRes["data"] as? [Any] else {
        return
    }

    for value in data {
        let dictData = value as? [String : Any]
        print("addressDescription : \(dictData!["addressDescription"])")
        print("addressLine1 : \(dictData!["addressLine1"])")
        print("addressLine2 : \(dictData!["addressLine2"])")
        print("addressLine3 : \(dictData!["addressLine3"])")
    }

答案 1 :(得分:1)

SELECT 
    Treatment.Treatment_Date, 
    Salutation.Value as Title,
    Customer.Firstname,
    Customer.Lastname,
    Address.Value as Street, 
    Postal.Postal, 
    Postal.City, 
    Patient.Name as AnimalName,
    TreatmentType.Type as Treatment,
    Treatment.Price,
    Tax.Value as Tax,
    SUM(Treatment.Price) as TotalExTax,
    SUM(Treatment.Price * Tax.Value / 100) as TotalTax,
    SUM(Treatment.Price) + (SUM(Treatment.Price * Tax.Value / 100)) as TotalPrice

FROM Treatment
INNER JOIN Patient ON Treatment.Patient_FK = Patient.ID
INNER JOIN AnimalType ON Patient.Type_FK = AnimalType.ID
INNER JOIN Customer ON Patient.Owner_FK = Customer.ID
INNER JOIN TreatmentType ON Treatment.TreatmentType_FK = TreatmentType.ID
INNER JOIN Salutation ON Customer.Salutation_FK = Salutation.ID
INNER JOIN Address ON Customer.Address_FK = Address.ID
INNER JOIN Postal ON Address.Postal_FK = Postal.Postal
INNER JOIN Tax ON Treatment.Tax_FK = Tax.ID

WHERE Customer.ID = 151 AND Treatment.Treatment_Date between '11/12/2014 00:00:00 AM' and '12/12/2014 12:00:00 PM'
group by Treatment.Treatment_Date, 
    Salutation.Value,
    Customer.Firstname,
    Customer.Lastname,
    Address.Value, 
    Postal.Postal, 
    Postal.City, 
    Patient.Name,
    TreatmentType.Type,
    Treatment.Price,
    Tax.Value

答案 2 :(得分:0)

SUM是一个与GROUP BY子句配对的聚合。如果您还没有描述哪些列不应该被挤压,则无法对某些列求和。

添加一个GROUP BY子句,其中包含不应聚合的所有列,您可以继续使用。

答案 3 :(得分:0)

添加分组
/** Exception for poming only applies if no special date is set **/

  if (window.id_product && window.id_product == 49) {
deliveryDate = new Date(
  year,
  month,
  deliveryDay + 5,
  9,
  0,
  0
);

nextDate = new Date(
  year,
  month,
  nextDay,
  9,
  0,
  0);

/** Everything else **/
  }

答案 4 :(得分:0)

获取客户ID,其中包含您对临时表所需的值的总和 根据需要将临时表与其余表格连接起来。

groupby

来自custTreat的SELECT *