0

我创建了一个表格,记录单场比赛的篮球次数 3_point_attempts 和 3_points_scored。

我想要做的是多个 3_point_attempts*1.5 和 3_point_scored*3 的值,但是这样做时我收到“不是单组组函数”错误。

请检查这个dbfiddle

有人可以告诉我我在这里做错了什么吗?

4

2 回答 2

1

直接做乘法:

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
于 2013-02-26T16:08:25.000 回答
1

您可以使用sum() over()来获得结果:

SELECT performanceid, 
  matchid, 
  teamid, 
  twopattempts, 
  (SUM(twopattempts) over(partition by teamid))*2  AS SumPoints
FROM Performance
WHERE TeamID = 1

请参阅带有演示的 SQL Fiddle

如果要添加三点尝试,则可以使用:

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

请参阅带有演示的 SQL Fiddle

以上将为您提供返回的每一行的总值。如果您希望它基于matchidand 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

请参阅带有演示的 SQL Fiddle

于 2013-02-26T16:13:01.020 回答