Reputation: 11
I have the ff. structure:
+----+---------+---------+---------+---------+---------+--------+
| id | player1 | player2 | win | lose | LEAVE | STATUS |
+----+---------+---------+---------+---------+---------+--------+
| 1 | 151663 | 150000 | 151663 | 150000 | 0 | 1 |
| 2 | 150000 | 151663 | 150000 | 151663 | 0 | 1 |
| 3 | 151663 | 150000 | 151663 | 150000 | 0 | 1 |
+----+---------+---------+---------+---------+---------+--------+
How do I generate a query if I need a result like below?
+--------+-----+------+-------+
| player | win | lose | leave |
+--------+-----+------+-------+
| 150000 | 1 | 2 | 0 |
| 151663 | 2 | 1 | 0 |
+--------+-----+------+-------+
Upvotes: 1
Views: 66
Reputation: 169
SELECT Player,sum(win)win,sum(lose)lose,leave FROM(
SELECT win Player,1 as win,0 as lose,leave FROM @Table
UNION ALL
SELECT lose Player,0 as win,1 as lose,leave FROM @Table
)A group by Player,leave
Upvotes: 0
Reputation: 1269673
You can use union all
and aggregation:
select player, sum(win) as wins, sum(lose) as losses,
sum(leave) as leaves
from ((select win as player, 1 as win, 0 as lose, 0 as leave
from ff
where win <> 0
) union all
(select lose as player, 0 as win, 1 as lose, 0 as leave
from ff
where lose <> 0
) union all
(select leave as player, 0 as win, 0 as lose, 1 as leave
from ff
where leave <> 0
)
) wll
group by player;
Upvotes: 2