如何根据另一列查找一列的最后六行(动态)总和

时间:2015-02-11 06:42:30

标签: mysql sql

BallByBallID   Deliveries   RunsScored    BowlPlayerId        BatPlayerId

      109               0              1            127               4
      110             0.1              2            127               6
      111             0.2              3            127               6
      112             0.3              4            127               4
      113             0.4              6            127               4
      114             0.5              6            127               4
      230               0              1            162               4
      231             0.1              2            162               6
      232             0.2              3            162               6
      233             0.3              4            162               4
      234             0.4              5            162               4
      235             0.5              6            162               6
      236               1              1            169               4
      237             1.1              2            169               6
      238             1.2              3            169               6
      239             1.3              4            169               4
      240             1.4              5            169               4
      241             1.5              6            169               6

我有上述格式的数据。现在我想为最后插入的数据找到 RunsScored BowlPlayerId 总和(数据是动态的,后六个可能随时更改) BallByBallId 基于。 我试着用这样的方法找到解决方案.........

SELECT SUM(RunsScored) from (select BallByBallId from BallByBall ORDER BY BallByBallId DESC LIMIT 6);

总共有一些......

SELECT SUM(RunsScored) from (SELECT top 6 * from BallByBall ORDER BY BallByBallId DESC) A GROUP BY A.BowlPlayerID

1 个答案:

答案 0 :(得分:0)

SELECT SUM(RunsScored + BowlPlayerID)   来自BallByBall组的BallByBallId限制6;

http://sqlfiddle.com/#!2/f3ab78/10