我有一个像这样的SQL表
year tour wins cuts money
1990 EUR 2 4 54,500
1990 USA 1 2 22,000
1991 EUR 4 2 154,000
1991 USA 2 1 245,000
and so on
我希望通过YEAR对结果进行分组,总结胜负 但是,我希望单独保留money列,并从每个YEAR行返回两个值,如moneyEUR和moneyUSA(基于TOUR列)
我目前正在做这样的SELECT
SELECT year, SUM(wins), SUM(cuts), money FROM players GROUP BY year
这个组很好,但只返回第一个货币价值。
有没有办法可以将每个货币价值作为单独的价值返回? 并且仍然每年补充其他值?
答案 0 :(得分:3)
如果您每年总共有2条记录,那么您可以
SELECT year,
SUM(wins),
SUM(cuts),
sum(case when tour = 'EUR' then money end) as moneyEUR,
sum(case when tour = 'USA' then money end) as moneyUSA
FROM players
GROUP BY year
答案 1 :(得分:0)
select t1.tour, t1.money, t1.year, t2.wins_sum, t2.cuts_sum
from players t1
join (
select year
sum(wins) wins_sum,
sum(cuts) cuts_sum
from players
group by year
) t2 on t1.year = t2.year