2016-10-10 84 views
0

我想这样的代码:总和SQL列与加盟

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.

+0

你想SUM基于什么?该错误不言自明 – techspider

+0

用于在同一行中获得总分 – user6927546

+0

[列的原因在选择列表中无效,因为它未包含在聚合函数或GROUP BY子句中](http://堆栈溢出。com/questions/13999817/reason-for-column-is-invalid-in-the-select-list-because-it-is-not-contained-in-e) – techspider

回答

0

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

错误消息勾画出来。你可以这样做(仅选择骨料没有GROUP BY):

Select 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 
     , 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 

,但不是这个(选择骨料和没有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'; 
+0

没有工作的第二种情况,我也没有采取相同的错误消息 – user6927546

+0

我没有添加你的查询应该是什么。试图帮助解释错误信息。检查其他人的答案以获得复制/粘贴解决方案。 – Malk

0

当使用聚合函数,你需要使用一个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分组。

+0

谢谢你的工作 – user6927546

0

你不能没有使用GROUP BY使用聚合功能(在这种情况下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'; 

但是,也许你是被CustomerNum想将它们分组,CouponNameSurname,在这种情况下,你只需要添加:但是,你可以通过这个窗口化SUM()功能做a 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 
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列表。

0

@ User6927546

如果你正在使用MySQL比它应该工作,因为我有交叉检查相同,其工作的罚款。

选择employee.empId,employee.Name,总和从员工 (w.workcode)加入,工件W上employee.workId = w.id其中employee.Name = 'anoop'

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