计算滚动预算

时间:2019-03-14 20:27:10

标签: sql google-bigquery

我有一张桌子可以用来准备我的幻想棒球选秀。看起来像这样(这仅适用于前24行。之后的24行是针对Team 2,然后针对Team 3,依此类推):

+--------+--------------------+--------+------+
|  team  |        name        | salary | year |
+--------+--------------------+--------+------+
| Team 1 | Corey Kluber       |     47 | 2018 |
| Team 1 | Manny Machado      |     46 | 2018 |
| Team 1 | Rhys Hoskins       |     28 | 2018 |
| Team 1 | Jose Quintana      |     27 | 2018 |
| Team 1 | Buster Posey       |     27 | 2018 |
| Team 1 | Edwin Encarnacion  |     22 | 2018 |
| Team 1 | Khris Davis        |     16 | 2018 |
| Team 1 | Marcus Stroman     |     14 | 2018 |
| Team 1 | Ian Kinsler        |      7 | 2018 |
| Team 1 | Felipe Vazquez     |      6 | 2018 |
| Team 1 | Jeff Samardzija    |      4 | 2018 |
| Team 1 | Michael Wacha      |      2 | 2018 |
| Team 1 | Ender Inciarte     |      2 | 2018 |
| Team 1 | Andrelton Simmons  |      2 | 2018 |
| Team 1 | Adam Wainwright    |      1 | 2018 |
| Team 1 | Matt Moore         |      1 | 2018 |
| Team 1 | Mike Foltynewicz   |      1 | 2018 |
| Team 1 | Cam Bedrosian      |      1 | 2018 |
| Team 1 | Joe Musgrove       |      1 | 2018 |
| Team 1 | Aaron Altherr      |      1 | 2018 |
| Team 1 | Trevor Story       |      1 | 2018 |
| Team 1 | Marcus Semien      |      1 | 2018 |
| Team 1 | Ryon Healy         |      1 | 2018 |
| Team 1 | Tim Beckham        |      1 | 2018 |
+--------+--------------------+--------+------+

每个小组以260美元的预算开始草稿。我希望看到某些团队过去的花费方式。因此,其预期输出如下所示:

+--------+--------------------+--------+------+----------------+
|  team  |        name        | salary | year | rolling_budget |
+--------+--------------------+--------+------+----------------+
| Team 1 | Corey Kluber       |     47 | 2018 |            213 |
| Team 1 | Manny Machado      |     46 | 2018 |            167 |
| Team 1 | Rhys Hoskins       |     28 | 2018 |            139 |
| Team 1 | Jose Quintana      |     27 | 2018 |            112 |
| Team 1 | Buster Posey       |     27 | 2018 |             85 |
| Team 1 | Edwin Encarnacion  |     22 | 2018 |             63 |
| Team 1 | Khris Davis        |     16 | 2018 |             47 |
| Team 1 | Marcus Stroman     |     14 | 2018 |             33 |
| Team 1 | Ian Kinsler        |      7 | 2018 |             26 |
| Team 1 | Felipe Vazquez     |      6 | 2018 |             20 |
| Team 1 | Jeff Samardzija    |      4 | 2018 |             16 |
| Team 1 | Michael Wacha      |      2 | 2018 |             14 |
| Team 1 | Ender Inciarte     |      2 | 2018 |             12 |
| Team 1 | Andrelton Simmons  |      2 | 2018 |             10 |
| Team 1 | Adam Wainwright    |      1 | 2018 |              9 |
| Team 1 | Matt Moore         |      1 | 2018 |              8 |
| Team 1 | Mike Foltynewicz   |      1 | 2018 |              7 |
| Team 1 | Cam Bedrosian      |      1 | 2018 |              6 |
| Team 1 | Joe Musgrove       |      1 | 2018 |              5 |
| Team 1 | Aaron Altherr      |      1 | 2018 |              4 |
| Team 1 | Trevor Story       |      1 | 2018 |              3 |
| Team 1 | Marcus Semien      |      1 | 2018 |              2 |
| Team 1 | Ryon Healy         |      1 | 2018 |              1 |
| Team 1 | Tim Beckham        |      1 | 2018 |              0 |
+--------+--------------------+--------+------+----------------+

我将如何到达?

编辑:运行戈登查询后:

select t.*,
       260 - sum(salary) over (partition by team, year order by salary desc) as rolling_budget
from t;

它接近了,但仍然没有做到相当,因为结果看起来像这样(rolling_budget代表了上面查询的结果{{1} }应该是这样:

correct_budget

为什么一旦达到10就会给我零?

2 个答案:

答案 0 :(得分:0)

您可以使用累计金额来获取总支出:

select t.*,
       sum(salary) over (partition by team, year order by salary desc, name) as running_salary
from t;

如果预算是固定的,那么您只需使用减法即可:

select t.*,
       260 - sum(salary) over (partition by team, year order by salary desc, name) as rolling_budget
from t;

答案 1 :(得分:0)

以下是用于BigQuery标准SQL

#standardSQL
SELECT team, name, salary, year,
  IFNULL(
    SUM(salary) 
    OVER(PARTITION BY team, year ORDER BY salary, name ROWS BETWEEN UNBOUNDED PRECEDING AND 1 PRECEDING)
  , 0) AS running_salary
FROM `project.dataset.table`
ORDER BY team, year DESC, salary DESC, name DESC  

如果要应用于您的示例数据-结果为

Row team    name                salary      year    running_salary   
1   Team 1  Corey Kluber        47          2018    213  
2   Team 1  Manny Machado       46          2018    167  
3   Team 1  Rhys Hoskins        28          2018    139  
4   Team 1  Jose Quintana       27          2018    112  
5   Team 1  Buster Posey        27          2018    85   
6   Team 1  Edwin Encarnacion   22          2018    63   
7   Team 1  Khris Davis         16          2018    47   
8   Team 1  Marcus Stroman      14          2018    33   
9   Team 1  Ian Kinsler         7           2018    26   
10  Team 1  Felipe Vazquez      6           2018    20   
11  Team 1  Jeff Samardzija     4           2018    16   
12  Team 1  Michael Wacha       2           2018    14   
13  Team 1  Ender Inciarte      2           2018    12   
14  Team 1  Andrelton Simmons   2           2018    10   
15  Team 1  Trevor Story        1           2018    9    
16  Team 1  Tim Beckham         1           2018    8    
17  Team 1  Ryon Healy          1           2018    7    
18  Team 1  Mike Foltynewicz    1           2018    6    
19  Team 1  Matt Moore          1           2018    5    
20  Team 1  Marcus Semien       1           2018    4    
21  Team 1  Joe Musgrove        1           2018    3    
22  Team 1  Cam Bedrosian       1           2018    2    
23  Team 1  Adam Wainwright     1           2018    1    
24  Team 1  Aaron Altherr       1           2018    0