user4047100
user4047100

Reputation: 11

Dis aggregation in SQL

I have a table

Team           Matches       Won
A                10           2
B                20           4 
C                40           10

I want to convert this table into following

Team             Won/Lost         Won Lost Number
A                    Won              2
A                    Lost             8
B                    Won              4
B                    Lost             16
C                    Won              10
C                   Lost              30

Thanks in advance !

Upvotes: 0

Views: 28

Answers (1)

Mureinik
Mureinik

Reputation: 311998

In fact, there is no aggregation here - just a simple subtraction. I'd do this in two queries, one for wins and one for loses, and combine them with the union all operator:

SELECT    team, 'Won' AS "Won/Lost", won AS "Won/Lost Number"
FROM      my_table
UNION ALL
SELECT    team, 'Lost', matches - won
FROM      my_table
ORDER BY  1 ASC, 2 DESC

Upvotes: 1

Related Questions