Input data
player_id | team_ranking | price_money |
---|---|---|
131 | 9 | 100 |
289 | 9 | 100 |
83 | 9 | 100 |
236 | 8 | 200 |
154 | 8 | 200 |
230 | 7 | 300 |
72 | 7 | 300 |
200 | 6 | 400 |
174 | 6 | 400 |
326 | 6 | 400 |
261 | 6 | 400 |
60 | 5 | 500 |
181 | 5 | 500 |
387 | 5 | 500 |
34 | 4 | 600 |
144 | 4 | 600 |
377 | 3 | 700 |
222 | 3 | 700 |
112 | 3 | 700 |
16 | 2 | 800 |
36 | 2 | 800 |
299 | 1 | 1000 |
Output I want is :
player_id | team_ranking | price_money |
---|---|---|
131 | 9 | 0 |
289 | 9 | 0 |
83 | 9 | 0 |
236 | 8 | 100 |
154 | 8 | 100 |
230 | 7 | 200 |
72 | 7 | 200 |
200 | 6 | 300 |
174 | 6 | 300 |
326 | 6 | 300 |
261 | 6 | 300 |
60 | 5 | 400 |
181 | 5 | 400 |
387 | 5 | 400 |
34 | 4 | 500 |
144 | 4 | 500 |
377 | 3 | 600 |
222 | 3 | 600 |
112 | 3 | 600 |
16 | 2 | 700 |
36 | 2 | 700 |
299 | 1 | 800 |
However when I try to use lag. It seems like it is only moving 1 persons price money from each team down.
I am trying to use lag functions over each teams ranking and shift each teams price money down by 1.
How do I do this.I tried with below code and got output as below :
SELECT Player_id, Team_Ranking,
LAG(Price_Money, 1, 0) OVER (ORDER BY Team_Ranking desc) AS Price_Money FROM table;
Player_id | Team_Ranking | Price_Money |
---|---|---|
289 | 9 | 0 |
83 | 9 | 100 |
131 | 9 | 100 |
236 | 8 | 100 |
154 | 8 | 200 |
230 | 7 | 200 |
72 | 7 | 300 |
200 | 6 | 300 |
174 | 6 | 400 |
326 | 6 | 400 |
261 | 6 | 400 |
60 | 5 | 400 |
387 | 5 | 500 |
181 | 5 | 500 |
34 | 4 | 500 |
144 | 4 | 600 |
377 | 3 | 600 |
222 | 3 | 700 |
112 | 3 | 700 |
16 | 2 | 700 |
36 | 2 | 800 |
299 | 1 | 800 |
2
-
As mentioned in the comments above please do not use images for input and output data,rather use sample input and output in tabular markdown format. Also I am not sure whether you want mysql or oracle since both are tagged, I have used
mysql
as an example. -
As you want to apply lag on a team ranking but in your final table you have multiple columns of same team rank. To achieve this I have created a cte with team_ranking and price_money, used lag to get the desired result and joined it back to the final table
Fiddle
with distinct_team_ranking_money as
(
select
Team_Ranking,
LAG(Price_Money, 1, 0) OVER (ORDER BY Team_Ranking desc) AS price_money
from
(SELECT
distinct
Team_Ranking,
price_money from test
) as T
)
select test.player_id, test.team_ranking, distinct_team_ranking_money.price_money
from
test
left join distinct_team_ranking_money
on distinct_team_ranking_money.Team_Ranking = test.Team_Ranking ;
Output
player_id | team_ranking | price_money |
---|---|---|
131 | 9 | 0 |
289 | 9 | 0 |
83 | 9 | 0 |
236 | 8 | 100 |
154 | 8 | 100 |
230 | 7 | 200 |
72 | 7 | 200 |
200 | 6 | 300 |
174 | 6 | 300 |
326 | 6 | 300 |
261 | 6 | 300 |
60 | 5 | 400 |
181 | 5 | 400 |
387 | 5 | 400 |
34 | 4 | 500 |
144 | 4 | 500 |
377 | 3 | 600 |
222 | 3 | 600 |
112 | 3 | 600 |
16 | 2 | 700 |
36 | 2 | 700 |
299 | 1 | 800 |