计算帽子技巧的数量,即连续3胜(1胜0负)

时间:2015-04-24 09:31:37

标签: sql sql-server-2008

从结果中计算帽子技巧的数量。即连续3胜(这里Result 1胜1负,0胜亏)

MatchNo     Match_date  Name    Result
--------------------------------------
1           2015-04-08  Aditya  1
2           2015-04-09  Aditya  0
3           2015-04-10  Aditya  0
4           2015-04-11  Aditya  0
5           2015-04-11  Aditya  0
6           2015-04-12  Aditya  0
7           2015-04-12  Aditya  1
8           2015-04-13  Aditya  1
9           2015-04-14  Aditya  0
10          2015-04-15  Aditya  1
11          2015-04-16  Aditya  0
12          2015-04-17  Aditya  0
13          2015-04-18  Aditya  0
14          2015-04-18  Aditya  0
15          2015-04-19  Aditya  1
16          2015-04-19  Aditya  1
17          2015-04-20  Aditya  1
18          2015-04-21  Aditya  1
19          2015-04-22  Aditya  1
20          2015-04-22  Aditya  0
21          2015-04-23  Aditya  0

2 个答案:

答案 0 :(得分:2)

尽管我讨厌游标,但我想不出另一种在SQL Server 2008中轻松做到这一点的方法,所以你可以试试这个:

Declare @count int = 0,
        @Streak int = 0,
        @Hattrick int = 0
Declare foobar cursor forward_only for Select Streak from Tablename

Open foobar

fetch next from foobar into @Streak

While @@FETCH_STATUS = 0
Begin
    If @Streak = 1
        Set @count = @count + 1
    else
        Set @count = 0
    if @count = 3
        begin
           Set @Hattrick = @Hattrick + 1
           Set @count = 0
        end

    Fetch next from foobar into @Streak
End

Select @Hattrick

对于SQL Server的更高版本,我会使用Cross Apply和Lag的混合。遗憾的是,Lag仅在SQL Server 2014及更高版本中可用

SQL小提琴:http://www.sqlfiddle.com/#!6/27273/4/0

对于SQL Server 2014,请使用Cross Apply和LAG:

Select Sum(Hattrick) Hattricks

from

(Select Case when c.Streak = 1 and lag(c.Streak, 1,0) over (order by c.MatchNo) = 0 and lag(c.Streak, 2,0) over (order by c.MatchNo) = 0 then 1 else 0 end Hattrick

from @foobar f
Cross apply
(select case when f.Streak = 1 and Streak = 1 then 1 else 0 end Streak, MatchNo from @foobar where MatchNo = f.MatchNo - 1) b
Cross apply
(select case when b.Streak = 1 and Streak = 1 then 1 else 0 end Streak, MatchNo from @foobar where MatchNo = b.MatchNo - 1) c) Hat

SQL小提琴:http://www.sqlfiddle.com/#!6/a3a05/2/0

答案 1 :(得分:0)

没有游标的另一种方式可能是这样的。这考虑了6连续连胜作为2个帽子技巧。要将其视为单条纹,我们需要将最终SUM(Streak)更改为COUNT(Streak)

DECLARE @StreakTable Table (MatchNo int, Match_date date, Name varchar(20), Streak int)

Insert into @StreakTable values
(1, '2015-04-08', 'Aditya',1),
(2, '2015-04-09', 'Aditya',1),
(3, '2015-04-10', 'Aditya',0),
(4, '2015-04-11', 'Aditya',0),
(5, '2015-04-12', 'Aditya',1),
(6, '2015-04-13', 'Aditya',1),
(7, '2015-04-14', 'Aditya',1)

;WITH CTE AS 
(
    SELECT MatchNo,Match_date,Name,Streak,
    ROW_NUMBER() OVER(PARTITION BY Name ORDER BY Match_date) -
    ROW_NUMBER() OVER(PARTITION BY Name,streak ORDER BY Match_date)num
    FROM @StreakTable
), CountStreak as 
(
    SELECT Name,COUNT(*)/3 Streak
    FROM CTE
    WHERE Streak = 1
    GROUP BY num,Name
    HAVING  COUNT(*) >= 3
)
SELECT Name,SUM(Streak) as TotalStreak
FROM CountStreak
GROUP BY Name