【问题标题】:Sum Sql column with joinSum Sql 列与连接
【发布时间】:2016-10-11 03:30:24
【问题描述】:

我正在尝试这段代码:

Select  C.CustomerNum
      , C.Coupon
      , C.name
      , C.Surname
      , Sum(P.Points)
From    customers C
Join    Points P
        On P.CustomerNum = C.CustomerNum
Where   C.Coupon = 'xxx-xxx-xxx-x'; 

我收到错误:

消息 8118,级别 16,状态 1,第 1 行列 'C.CustomerNum' 无效 在选择列表中,因为它不包含在聚合中 函数,并且没有 GROUP BY 子句。

【问题讨论】:

标签: sql-server sql-server-2005


【解决方案1】:

你也可以使用。

SELECT C.CustomerNum,
       C.Coupon,
       C.name,
       C.Surname,
       P.Points
FROM   customers C
       INNER JOIN (SELECT Sum(Points) AS Points,
                                    CustomerNum
                   FROM   Points
                   GROUP  BY CustomerNum) P
         ON P.CustomerNum = C.CustomerNum
WHERE  C.Coupon = 'xxx-xxx-xxx-x'; 

为了避免必须将 customers 中的所有选定列添加到 GROUP BY 列表中。

【讨论】:

    【解决方案2】:

    当您有一个聚合(SUM、COUNT、AVERAGE)时,您需要按返回的所有非聚合列对查询进行分组。为此:

    Select  C.CustomerNum
          , C.Coupon
          , C.name
          , C.Surname
          , Sum(P.Points)
    From    customers C
    Join    Points P
            On P.CustomerNum = C.CustomerNum
    Where   C.Coupon = 'xxx-xxx-xxx-x'; 
    Group By C.CustomerNum
          , C.Coupon
          , C.name
          , C.Surname
    

    【讨论】:

      【解决方案3】:

      错误消息说明了这一点。您可以这样做(仅选择没有分组依据的聚合):

      Select  Sum(P.Points)
      From    customers C
      Join    Points P
              On P.CustomerNum = C.CustomerNum
      Where   C.Coupon = 'xxx-xxx-xxx-x'; 
      

      或者这个(如果列在分组依据中,则选择聚合和列):

      Select  C.CustomerNum
            , Sum(P.Points)
      From    customers C
      Join    Points P
              On P.CustomerNum = C.CustomerNum
      Where   C.Coupon = 'xxx-xxx-xxx-x'
      group by c.customernum
      

      但不是这个(选择没有分组依据的聚合和列):

      Select  C.CustomerNum
            , C.Coupon
            , C.name
            , C.Surname
            , Sum(P.Points)
      From    customers C
      Join    Points P
              On P.CustomerNum = C.CustomerNum
      Where   C.Coupon = 'xxx-xxx-xxx-x';  
      

      【讨论】:

      • 在第二种情况下不起作用,我也不会收到相同的错误消息
      • 我没有添加您的查询应该是什么。只是试图帮助解释错误消息。检查其他人的答案以获取复制/粘贴解决方案。
      【解决方案4】:

      使用聚合函数时,您需要使用GROUP BY。试试这个:

      Select  C.CustomerNum
            , C.Coupon
            , C.name
            , C.Surname
            , Sum(P.Points)
      From    customers C
      Join    Points P
              On P.CustomerNum = C.CustomerNum
      Where   C.Coupon = 'xxx-xxx-xxx-x'; 
      GROUP BY C.CustomerNum
            , C.Coupon
            , C.name
            , C.Surname
      

      现在,您可能想要缩小实际想要选择的范围。例如,最好只按 C.Surname 或 C.CustomerNum 分组

      【讨论】:

        【解决方案5】:

        如果不使用 GROUP BY,则不能将聚合函数(在本例中为 SUM())与其他非聚合列一起使用。但是,您可以通过将其设为窗口化的SUM() 函数来实现:

        Select  C.CustomerNum
              , C.Coupon
              , C.name
              , C.Surname
              , Sum(P.Points) Over (Partition By C.CustomerNum)
        From    customers C
        Join    Points P
                On P.CustomerNum = C.CustomerNum
        Where   C.Coupon = 'xxx-xxx-xxx-x'; 
        

        但也许您想按CustomerNumCouponNameSurname 对它们进行分组,在这种情况下,您只需添加一个GROUP BY

        Select  C.CustomerNum
              , C.Coupon
              , C.name
              , C.Surname
              , Sum(P.Points)
        From    customers C
        Join    Points P
                On P.CustomerNum = C.CustomerNum
        Where   C.Coupon = 'xxx-xxx-xxx-x'
        Group By C.CustomerNum, C.Coupon, C.Name, C.Surname
        

        【讨论】:

          【解决方案6】:

          @User6927546

          如果您使用的是 mysql,它应该可以工作,因为我已经交叉检查过相同的并且工作正常。

          select employee.empId ,employee.Name ,sum(w.workcode) from employee 加入工作 w on employee.workId=w.id where employee.Name='anoop'

          我得到的结果集为: empId 名称总和(w.workcode) 1 Anoop 200

          【讨论】:

            猜你喜欢
            • 2017-01-15
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2023-04-06
            • 1970-01-01
            • 2011-01-13
            • 1970-01-01
            • 2021-06-08
            相关资源
            最近更新 更多