Tags
Complex SQL Challenges, Complex TSQL Challenge, Interesting Interview Questions, Interview Qs.SQL SERVER Questions, Interview questions on Joins, Interview Questions on SQL, InterviewQuestions, InterviewQuestions for SQL, Joins, Joins Interview questions, Joins Puzzle, Khowal, Learn complex SQL, Learn SQL, Learn T-SQL, Objective Puzzle, Pawan, Pawan Khowal, Pawan Kumar, Pawan Kumar Khowal, PL/SQL Challenges, puzzle sql developer, Puzzles, Queries for SQL Interview, SELECT Puzzle, SQL, SQL 2012, SQL 2014, SQL 2014 Interview Questions, SQL Challenge, SQL Challenges, SQL Interview Questions, SQL Joins, SQL pl/sql puzzles, SQL Puzzles, SQL Queries, SQL Quiz, SQL Server Database, SQL SERVER Interview questions, SQL Skills, SQL Sudoku, SQL Top clause, SQL Trikcy question, sql/database interview for puzzle sql developer, SQLSERVER, T SQL Puzzles, T-SQL Challenge, TOP Clause, Tough SQL Challenges, Tough SQL Puzzles, Tricky Questions, TSQL, TSQL Challenge, TSQL Challenges, TSQL Interview questions, TSQL Queries
T-SQL Query | [ The Consecutive Wins Puzzle – 2 ]
Original Puzzle URL – http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=23880
Puzzle Statement
- We have a table called GameResults.
- Here we have to find the winning streak of a player(s) that spans multiple events/dates. For example…the data below should show that player 1000 had a 4 game winning streak, player 2000 had a 5 game winning streak and player 3000 had a 3 game winning streak.
- Please check out the sample input and expected output for details.
Sample Input
TournamentId | PlayerId | GameDate | Result |
1 | 1000 | 01-01-2000 | W |
1 | 1000 | 01-01-2000 | L |
1 | 1000 | 01-01-2000 | W |
1 | 1000 | 01-01-2000 | W |
1 | 1000 | 01-01-2000 | L |
1 | 2000 | 01-01-2000 | L |
1 | 2000 | 01-01-2000 | W |
1 | 2000 | 01-01-2000 | L |
1 | 2000 | 01-01-2000 | L |
1 | 2000 | 01-01-2000 | W |
2 | 1000 | 01-02-2000 | W |
2 | 1000 | 01-02-2000 | W |
2 | 1000 | 01-02-2000 | W |
2 | 1000 | 01-02-2000 | W |
2 | 1000 | 01-02-2000 | L |
2 | 2000 | 01-02-2000 | W |
2 | 2000 | 01-02-2000 | W |
2 | 2000 | 01-02-2000 | W |
2 | 2000 | 01-02-2000 | W |
2 | 2000 | 01-02-2000 | L |
3 | 3000 | 15-02-2000 | L |
3 | 3000 | 15-02-2000 | L |
3 | 3000 | 15-02-2000 | W |
3 | 3000 | 15-02-2000 | W |
3 | 3000 | 15-02-2000 | W |
3 | 2000 | 15-02-2000 | L |
3 | 2000 | 15-02-2000 | L |
3 | 2000 | 15-02-2000 | W |
3 | 2000 | 15-02-2000 | W |
3 | 2000 | 15-02-2000 | W |
Expected Output
PlayerId | ConsecutiveWins |
1000 | 4 |
2000 | 5 |
3000 | 3 |
Rules/Restrictions
- The solution should be should use “SELECT” statement or “CTE”.
- Add your solution(s) in the comments section or send you solution(s) to pawankkmr@gmail.com
Script
Use the below script to generate the source table and fill them up with the sample data.
— CREATE TABLE [dbo].[GameResults]( [TournamentId] [int] NULL, [PlayerId] [int] NULL, [GameDate] [date] NULL, [Result] [varchar](5) NULL ) INSERT INTO GameResults (TournamentId, PlayerId, GameDate,Result) VALUES (1, 1000,'1/1/2000', 'W'), (1, 1000,'1/1/2000', 'L'), (1, 1000,'1/1/2000', 'W'), (1, 1000,'1/1/2000', 'W'), (1, 1000,'1/1/2000', 'L'), (1, 2000,'1/1/2000', 'L'), (1, 2000,'1/1/2000', 'W'), (1, 2000,'1/1/2000', 'L'), (1, 2000,'1/1/2000', 'L'), (1, 2000,'1/1/2000', 'W'), (2, 1000,'2/1/2000', 'W'), (2, 1000,'2/1/2000', 'W'), (2, 1000,'2/1/2000', 'W'), (2, 1000,'2/1/2000', 'W'), (2, 1000,'2/1/2000', 'L'), (2, 2000,'2/1/2000', 'W'), (2, 2000,'2/1/2000', 'W'), (2, 2000,'2/1/2000', 'W'), (2, 2000,'2/1/2000', 'W'), (2, 2000,'2/1/2000', 'L'), (3, 3000,'2/15/2000', 'L'), (3, 3000,'2/15/2000', 'L'), (3, 3000,'2/15/2000', 'W'), (3, 3000,'2/15/2000', 'W'), (3, 3000,'2/15/2000', 'W'), (3, 2000,'2/15/2000', 'L'), (3, 2000,'2/15/2000', 'L'), (3, 2000,'2/15/2000', 'W'), (3, 2000,'2/15/2000', 'W'), (3, 2000,'2/15/2000', 'W') -- |
Update May 6 | Solution 1
-- ;WITH CTE1 AS ( SELECT * , ROW_NUMBER() OVER (PARTITION BY PlayerId ORDER BY GameDate) rnk FROM GameResults ) ,CTE2 AS ( SELECT *, CASE WHEN Result = lag(Result) over(PARTITION BY PlayerId order by rnk) THEN 0 ELSE 1 END cols FROM CTE1 c2 ) ,CTE3 AS ( SELECT * , SUM(cols) OVER (PARTITION BY PlayerId ORDER BY rnk) grouper FROM CTE2 ) ,CTE4 AS ( SELECT PlayerId, grouper , COUNT(grouper) ConsecutiveWins FROM CTE3 GROUP BY PlayerId,grouper ) SELECT PlayerId , MAX(ConsecutiveWins) ConsecutiveWins FROM CTE4 GROUP BY PlayerId -- |
Add a comment if you have any other solution in mind. We all need to learn.
Keep Learning
;with cte as (
select *, ROW_NUMBER() over (order by tournamentid, playerid, result)- row_number() over (partition by tournamentid, playerid, result order by tournamentid, playerid, result) as t from GameResults )
, cte2 as (select *, ROW_NUMBER() over (partition by t order by t) as s from cte where result=’W’) select playerid, max(s) as consecutivewins from cte2 group by playerid
LikeLike