SQL在单个查询中使用多个条件求和

SQL在单个查询中使用多个条件求和,sql,sql-server,laravel-5,sum,Sql,Sql Server,Laravel 5,Sum,在处理Laravel应用程序时,我需要从第二个数据库检索数据,这就是为什么我有这样一个特殊的数据库: 数据库::连接“sqlsrv2”。 在这个数据库中,有一个表customerAccount和一个表Opportunity。 我们想知道我们每个月为一个精确的年份和一个精确的客户家庭创造了多少机会,赢了还是输了。 我可以通过一些foreach检索数据,并对每个新的、赢得的或失去的案例机会进行查询,但最终对每个客户和每个月进行的查询超过12700次 所以我想到了SQL SUM,我可以在一个looon

在处理Laravel应用程序时,我需要从第二个数据库检索数据,这就是为什么我有这样一个特殊的数据库: 数据库::连接“sqlsrv2”。 在这个数据库中,有一个表customerAccount和一个表Opportunity。 我们想知道我们每个月为一个精确的年份和一个精确的客户家庭创造了多少机会,赢了还是输了。 我可以通过一些foreach检索数据,并对每个新的、赢得的或失去的案例机会进行查询,但最终对每个客户和每个月进行的查询超过12700次

所以我想到了SQL SUM,我可以在一个looong查询中创建3列new、win、lost。然后,我将对所选年度的每个月重复查询。 问题是,我不记得怎么做了,我也找不到任何帮助,因为我真的不知道要搜索什么

以下是我写查询的错误尝试:[编辑以遵循评论中的建议]

$clients = DB::connection('sqlsrv2')->table('customerAccount')
        ->select(DB::connection('sqlsrv2')->raw(
            "customerAccount.Name, 
  SUM(case when opportunity.customerAccountId = customerAccount.Id
  and YEAR(opportunity.OpportunityDate) = $year
  and MONTH(opportunity.OpportunityDate) = $month
  then 1
  else 0
  end) as nbOppNew,
  SUM(case when opportunity.customerAccountId = customerAccount.Id
  and YEAR(opportunity.OpportunityCloseDate) = $year
  and MONTH(opportunity.OpportunityCloseDate) = $month
  and StageProbability = 99
  then 1
  else 0
  end) as nbOppWon,
  SUM(case when opportunity.customerAccountId = customerAccount.Id
  and YEAR(opportunity.OpportunityCloseDate) = $year
  and MONTH(opportunity.OpportunityCloseDate) = $month
  and StageProbability <= 1
  then 1
  else 0
  end) as nbOppLost
  FROM
  customerAccount
  INNER JOIN opportunity ON customerAccount.Id = opportunity.customerAccountId
  WHERE
  MainInvoicingAddress_CountryIsoCode = 'ES'
  and familyId = '$family'
  GROUP BY customerAccount.Name"
        ))
        ->get();
Laravel:SQLSTATE[42000]:[Microsoft][SQL Server Native Client 11.0][SQL Server]在“from”附近生成的错误语法不正确


我真正想得到的是一个带有客户机名称的数组,每个数组都有一个名为nbOppNew的第二个条目,其中包含总和值。第三个是nbOppWon,第四个是nbOppLost。

您的错误可能是由于第一列select语句中的单引号引起的。删除引号,则不会发生错误

       "SELECT customerAccount.Name,
  SUM(case when opportunity.customerAccountId = customerAccount.Id
  and YEAR(opportunity.OpportunityDate) = $year
  and MONTH(opportunity.OpportunityDate) = $month
  then opportunity.OpportunityDate

还可以使用“选择输入”分组方式中的非聚合列名称。您正在根据id进行分组。如果需要根据id进行分组,请在select语句中使用id。

您的错误可能是由于第一列select语句中的单引号引起的。删除引号,则不会发生错误

       "SELECT customerAccount.Name,
  SUM(case when opportunity.customerAccountId = customerAccount.Id
  and YEAR(opportunity.OpportunityDate) = $year
  and MONTH(opportunity.OpportunityDate) = $month
  then opportunity.OpportunityDate

还可以使用“选择输入”分组方式中的非聚合列名称。您正在基于id进行分组。如果需要基于id进行分组,请在select语句中使用id。

删除customerAccount.Name的单个引号,并使用ES值的单个引号

     "SELECT customerAccount.Name,
      SUM(case when opportunity.customerAccountId = customerAccount.Id
      and YEAR(opportunity.OpportunityDate) = $year
      and MONTH(opportunity.OpportunityDate) = $month
      then opportunity.OpportunityDate
      else 0
      end) as nbOppNew,
      SUM(case when opportunity.customerAccountId = customerAccount.Id
      and YEAR(opportunity.OpportunityCloseDate) = $year
      and MONTH(opportunity.OpportunityCloseDate) = $month
      and StageProbability = 99
      then opportunity.OpportunityDate
      else 0
      end) as nbOppWon,
      SUM(case when opportunity.customerAccountId = customerAccount.Id
      and YEAR(opportunity.OpportunityCloseDate) = $year
      and MONTH(opportunity.OpportunityCloseDate) = $month
      and StageProbability <= 1
      then opportunity.OpportunityCloseDate
      else 0
      end) as nbOppLost
      FROM
      customerAccount
      INNER JOIN opportunity ON customerAccount.Id = opportunity.customerAccountId
      WHERE
      MainInvoicingAddress_CountryIsoCode = 'ES'
      and familyId = $family
      GROUP BY customerAccount.Name"

建议按customerAccount.Name使用group

删除customerAccount.Name的单个引号,并使用ES值的单个引号

     "SELECT customerAccount.Name,
      SUM(case when opportunity.customerAccountId = customerAccount.Id
      and YEAR(opportunity.OpportunityDate) = $year
      and MONTH(opportunity.OpportunityDate) = $month
      then opportunity.OpportunityDate
      else 0
      end) as nbOppNew,
      SUM(case when opportunity.customerAccountId = customerAccount.Id
      and YEAR(opportunity.OpportunityCloseDate) = $year
      and MONTH(opportunity.OpportunityCloseDate) = $month
      and StageProbability = 99
      then opportunity.OpportunityDate
      else 0
      end) as nbOppWon,
      SUM(case when opportunity.customerAccountId = customerAccount.Id
      and YEAR(opportunity.OpportunityCloseDate) = $year
      and MONTH(opportunity.OpportunityCloseDate) = $month
      and StageProbability <= 1
      then opportunity.OpportunityCloseDate
      else 0
      end) as nbOppLost
      FROM
      customerAccount
      INNER JOIN opportunity ON customerAccount.Id = opportunity.customerAccountId
      WHERE
      MainInvoicingAddress_CountryIsoCode = 'ES'
      and familyId = $family
      GROUP BY customerAccount.Name"

根据建议,以编写查询的方式使用group by CUSTOMERCOUNT.Name

,实际上没有语法错误。所以我认为这和查询中单引号的转义有关。但是查询本身执行得很好

但是,问题很少:

日期总和值是可疑的,很可能应该重新处理,因为不允许对日期进行总和 GROUP BY是按ID创建的,而您尝试选择Name
按照您编写查询的方式,没有实际的语法错误。所以我认为这和查询中单引号的转义有关。但是查询本身执行得很好

但是,问题很少:

日期总和值是可疑的,很可能应该重新处理,因为不允许对日期进行总和 GROUP BY是按ID创建的,而您尝试选择Name
好的,找到窍门了。Laravel查询生成器会在原始查询之后自动添加“from”语句。因此,我添加了一个双精度的“from”,以及我的join和where条件。我改为使用生成器的方法,并在中转换了我的查询:

$clients = DB::connection('sqlsrv2')->table('customerAccount')
        ->select(DB::connection('sqlsrv2')->raw(
            "customerAccount.Name, 
  SUM(case when opportunity.customerAccountId = customerAccount.Id
  and YEAR(opportunity.OpportunityDate) = $year
  and MONTH(opportunity.OpportunityDate) = $month
  then 1
  else 0
  end) as nbOppNew,
  SUM(case when opportunity.customerAccountId = customerAccount.Id
  and YEAR(opportunity.OpportunityCloseDate) = $year
  and MONTH(opportunity.OpportunityCloseDate) = $month
  and StageProbability = 99
  then 1
  else 0
  end) as nbOppWon,
  SUM(case when opportunity.customerAccountId = customerAccount.Id
  and YEAR(opportunity.OpportunityCloseDate) = $year
  and MONTH(opportunity.OpportunityCloseDate) = $month
  and StageProbability <= 1
  then 1
  else 0
  end) as nbOppLost"
        ))
            ->join('opportunity', 'customerAccount.Id', '=', 'opportunity.customerAccountId')
            ->where('MainInvoicingAddress_CountryIsoCode', 'ES')
            ->where('familyId', $family)
            ->groupBy('customerAccount.Name')
        ->get();

而且它有效!谢谢大家。

好的,找到窍门了。Laravel查询生成器会在原始查询之后自动添加“from”语句。因此,我添加了一个双精度的“from”,以及我的join和where条件。我改为使用生成器的方法,并在中转换了我的查询:

$clients = DB::connection('sqlsrv2')->table('customerAccount')
        ->select(DB::connection('sqlsrv2')->raw(
            "customerAccount.Name, 
  SUM(case when opportunity.customerAccountId = customerAccount.Id
  and YEAR(opportunity.OpportunityDate) = $year
  and MONTH(opportunity.OpportunityDate) = $month
  then 1
  else 0
  end) as nbOppNew,
  SUM(case when opportunity.customerAccountId = customerAccount.Id
  and YEAR(opportunity.OpportunityCloseDate) = $year
  and MONTH(opportunity.OpportunityCloseDate) = $month
  and StageProbability = 99
  then 1
  else 0
  end) as nbOppWon,
  SUM(case when opportunity.customerAccountId = customerAccount.Id
  and YEAR(opportunity.OpportunityCloseDate) = $year
  and MONTH(opportunity.OpportunityCloseDate) = $month
  and StageProbability <= 1
  then 1
  else 0
  end) as nbOppLost"
        ))
            ->join('opportunity', 'customerAccount.Id', '=', 'opportunity.customerAccountId')
            ->where('MainInvoicingAddress_CountryIsoCode', 'ES')
            ->where('familyId', $family)
            ->groupBy('customerAccount.Name')
        ->get();

而且它有效!谢谢大家。

您希望如何使用datethen opportunity.opportunity CloseDate值进行求和?我不想,我想求和案例发生的时间。我是否应该将then opportunity.opportunity日期替换为then 1?是的。您应该用值1I替换日期。我不确定raw方法是否支持将PHP变量作为SQL语句的一部分。将$year更改为“$year”$month和$family,或者使用占位符使用参数化查询。您希望如何使用datethen opportunity.OpportunityCloseDate值进行求和?我不想,我想求和案例发生的时间。我是否应该将then opportunity.opportunity日期替换为then 1?是的。您应该用值1I替换日期。我不确定raw方法是否支持将PHP变量作为SQL语句的一部分。将$year更改为“$year”$month和$family,或者使用占位符使用参数化查询。谢谢,我用这些建议编辑了我的问题,很遗憾我仍然有错误。谢谢,我用这些建议编辑了我的问题,很遗憾我仍然有错误。谢谢,既然你提到了,我看到$family没有被传递,所以我也在它周围添加了单引号:“$family”每个字符串变量必须用单引号括起来。。显然,我无法想象您使用的变量是字符串还是数字。。但是如果
db中的值是一个数字,不要使用单引号..谢谢,既然您提到了它,我看到$family没有传递,所以我也在它周围添加了单引号:“$family”每个字符串变量必须用单引号括起来。。显然,我无法想象您使用的变量是字符串还是数字。。但如果db中的值是一个数字,则不要使用单引号。。