我创建了一个表格,记录单场比赛的篮球次数 3_point_attempts 和 3_points_scored。
我想要做的是多个 3_point_attempts*1.5 和 3_point_scored*3 的值,但是这样做时我收到“不是单组组函数”错误。
请检查这个dbfiddle
有人可以告诉我我在这里做错了什么吗?
我创建了一个表格,记录单场比赛的篮球次数 3_point_attempts 和 3_points_scored。
我想要做的是多个 3_point_attempts*1.5 和 3_point_scored*3 的值,但是这样做时我收到“不是单组组函数”错误。
请检查这个dbfiddle
有人可以告诉我我在这里做错了什么吗?
直接做乘法:
SELECT performanceid, matchid, teamid, twopattempts, twopattempts*2 AS SumPoints,
3_point_attempts*1.5, 3_point_scored*3
FROM Performance
WHERE TeamID = 1
您只需要在group by
尝试聚合行时使用。例如:
select teamid, sum(3_point_attempts*1.5), sum(3_point_scored*3_
from Performance
group by TeamID
您可以使用sum() over()
来获得结果:
SELECT performanceid,
matchid,
teamid,
twopattempts,
(SUM(twopattempts) over(partition by teamid))*2 AS SumPoints
FROM Performance
WHERE TeamID = 1
如果要添加三点尝试,则可以使用:
SELECT performanceid,
matchid,
teamid,
twopattempts,
SUM(twopattempts) over(partition by teamid)*2 AS SumPoints,
sum(THREEPATTEMPTS) over(partition by teamid)*1.5 as ThreePointAttempts,
sum(THREEPSCORED) over(partition by teamid)*3 as ThreePointScored
FROM Performance
WHERE TeamID = 1
以上将为您提供返回的每一行的总值。如果您希望它基于matchid
and teamid
,那么您可以使用:
SELECT MatchID,
teamid,
SUM(twopattempts)*2 AS SumPoints,
sum(THREEPATTEMPTS)*1.5 as ThreePointAttempts,
sum(THREEPSCORED)*3 as ThreePointScored
FROM Performance
WHERE TeamID = 1
group by teamid, MatchID