逐列乘以"而不是单组组功能"错误

时间:2013-02-26 16:01:46

标签: sql oracle multiplying

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

我想要做的是多次使用3_point_attempts * 1.5和3_point_scored * 3的值,但是这样做会收到“非单组组功能”错误。

请检查this dbfiddle

有人能告诉我这里的错误吗?

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:1)

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

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

请参阅SQL Fiddle with Demo

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

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 with Demo

上面将给出返回的每行的总值。如果您希望它基于matchidteamid,那么您可以使用:

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 with Demo