Tags
Complex SQL Challenges, complex sql statement(puzzle), 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 Grouping Puzzle ]
Puzzle Statement
The Puzzle is self explanatory. Please check out the sample input and expected output for details.
Sample Input
day | leva | levb |
18 | A | B |
19 | A | B |
20 | B | A |
21 | A | B |
22 | A | B |
23 | A | B |
Expected Output
FROM | TO | Leva | Levb |
18 | 19 | A | B |
20 | 20 | B | A |
21 | 23 | A | B |
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.
—
declare @tbl as table(day int,leva varchar(40),levb varchar(40)) insert into @tbl select 18 ,'A' ,'B' union all select 19, 'A', 'B' union all select 20, 'B', 'A' union all select 21, 'A', 'B' union all select 22 ,'A' ,'B' union all select 23, 'A', 'B' — |
Update May 6 | Solution 1
-- SELECT MIN(day) [FROM] , MAX(day) [TO] , MIN(Leva) Leva , MAX(Levb) Levb FROM ( SELECT * , DAY - ROW_NUMBER() OVER (PARTITION BY leva,levb ORDER BY %%Physloc%%) rnk FROM @tbl ) A GROUP BY rnk ORDER BY [FROM] -- |
Add a comment if you have any other solution in mind. We all need to learn.
Keep Learning
Http://MSBISkills.com
Pawan Kumar Khowal