使用联接对 Sql 列求和

Sum Sql column with join

我正在尝试这段代码:

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'; 

我遇到错误:

Msg 8118, Level 16, State 1, Line 1 Column 'C.CustomerNum' is invalid in the select list because it is not contained in an aggregate function and there is no GROUP BY clause.

错误信息一目了然。您可以这样做(select 仅聚合而没有分组依据):

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

或这个(select 聚合和列,如果列在分组依据中):

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 没有分组依据的聚合和列):

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。试试这个:

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

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

分组

当您有聚合(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

您不能在不使用 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

你也可以用。

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 列表。

@User6927546

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

select employee.empId ,employee.Name ,sum(w.workcode) 来自员工 join 在 employee.workId=w.id where employee.Name='anoop'

上工作

我得到的结果集为: empId 名称 sum(w.workcode) 1 阿努普 200