我有一张桌子
ID HomeTeam AwayTeam FTHG FTAG WDL
1 Anderlecht Lokeren 2 3 W
2 Lokeren Bergen 2 1 W
3 Kortrijk Lokeren 3 3 D
4 Oud-Heverlee Lokeren 2 1 L
5 Lokeren Oostende 1 0 W
6 Charleroi Lokeren 2 1 L
7 Lokeren Gent 2 2 D
8 Club Brugge Lokeren 1 0 L
9 Lokeren Mechelen 4 0 W
10 Lokeren Club Brugge 0 3 L
我喜欢算上Lokeren的最后一连胜
例如,上一场比赛洛克伦失去了结果应该看起来像这样team wdl count
Lokeren L 1
答案 0 :(得分:0)
这个答案假定您使用sql server 2012(2008年对分析功能的支持较低)。
with sub as
(select x.*,
row_number() over(partition by wdl order by id) - row_number() over(order by id) as grp
from (select id, hometeam as team, wdl
from tbl
where hometeam = 'Lokeren'
union all
select id,
awayteam,
case when wdl = 'W' then 'L' else 'W' end
from tbl
where awayteam = 'Lokeren') x)
select team, wdl, count(*) as count
from sub
where grp = (select grp from sub where id = (select max(id) from sub))
group by team, wdl
您可以在此处看到小提琴演示:http://sqlfiddle.com/#!6/bc4d7/4/0
为了测试目的,我添加了第11局,他们也输了。