SQL中的Dis聚合

时间:2014-09-16 15:56:38

标签: mysql aggregate-functions

我有一张桌子

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

我想将此表格转换为以下内容

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

提前致谢!

1 个答案:

答案 0 :(得分:1)

事实上,这里没有聚合 - 只是一个简单的减法。我会在两个查询中执行此操作,一个用于胜利,一个用于丢失,并将它们与union all运算符组合:

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