Tags
Complex SQL Challenges, Complex TSQL Challenge, Interview Qs.SQL SERVER Questions, Interview Questions on SQL, InterviewQuestions, InterviewQuestions for SQL, Learn complex SQL, Learn SQL, Learn T-SQL, PL/SQL Challenges, Puzzles, Queries for SQL Interview, SQL, SQL 2012, SQL 2014, SQL 2014 Interview Questions, SQL Challenge, SQL Challenges, SQL pl/sql puzzles, SQL Puzzles, SQL Server 2012 Analysis Services, SQL Server Data Tools, SQL SERVER Interview questions, SQL SERVER Puzzles, SQL SERVER2005/2008, SQL Sudoku, SQLSERVER, T SQL Puzzles, T-SQL Challenge, Tabular Model, Tough SQL Challenges, Tough SQL Puzzles, TSQL, TSQL Challenge, TSQL Challenges, TSQL Interview questions, TSQL Queries
T-SQL Query | [ Matching data between rows and columns ]
Original puzzle link – http://beyondrelational.com/blogs/tc/archive/2009/10/19/TSQL-Challenge-15-matching-data-between-rows-and-columns.aspx
The puzzle is simple. In this puzzle we were required to find the first and last IDs for consecutive rows with same values of Send and Ack states. Please check out the sample input and expected output for details.
Sample Input
Rower
Row |
100 |
104 |
101 |
99 |
77 |
20 |
10 |
Cols
Col |
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
Expected output
ROW | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
10 | X | X | X | ||||||
20 | X | X | X | X | |||||
77 | X | X | |||||||
99 | X | X | X | ||||||
100 | X | X | X | X | |||||
101 | X | ||||||||
104 | X | X | X | X |
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 Cols (Col INT); INSERT INTO Cols VALUES (1); INSERT INTO Cols VALUES (2); INSERT INTO Cols VALUES (3); INSERT INTO Cols VALUES (4); INSERT INTO Cols VALUES (5); INSERT INTO Cols VALUES (6); INSERT INTO Cols VALUES (7); INSERT INTO Cols VALUES (8); INSERT INTO Cols VALUES (9); CREATE TABLE Rower (Row INT); INSERT INTO Rower VALUES (100); INSERT INTO Rower VALUES (104); INSERT INTO Rower VALUES (101); INSERT INTO Rower VALUES (99); INSERT INTO Rower VALUES (77); INSERT INTO Rower VALUES (20); INSERT INTO Rower VALUES (10); ---- |
UPDATE – 20-Apr-2015 – Solution 1
-- DECLARE @colsPivot AS VARCHAR(MAX) ='',@colsPivotIN AS VARCHAR(MAX) ='' SELECT @colsPivot = STUFF((SELECT ',' + QUOTENAME(Col) FROM cols FOR XML PATH(''), TYPE).value('.', 'NVARCHAR(MAX)'),1,1,'') SELECT @colsPivotIN = STUFF((SELECT ',' + ' CASE WHEN Row % ' + QUOTENAME(Col) + ' = 0 THEN ''X'' ELSE '''' END ' + QUOTENAME(Col) FROM cols FOR XML PATH(''), TYPE).value('.', 'NVARCHAR(MAX)'),1,1,'') DECLARE @ExStr AS VARCHAR(MAX) = ' ;WITH CTE AS ( select *,ROW_NUMBER() over (order by (select null )) rnk from cols ) ,CTE1 AS ( SELECT * FROM CTE PIVOT ( max(rnk) FOR col in (' + @colsPivot + ') ) pvt ) SELECT ROW , ' + @colsPivotIN + ' from Rower r,CTE1 ORDER BY r.Row' EXEC (@ExStr) -- |
Add a comment if you have any other solution in mind. We all need to learn.
Keep Learning
select row,
(case when pp1.row%pp1.[1]=0 then ‘X’ else ” end)[1]
,(case when pp1.row%pp1.[2]=0 then ‘X’ else ” end)[2]
,(case when pp1.row%pp1.[3]=0 then ‘X’ else ” end)[3]
,(case when pp1.row%pp1.[4]=0 then ‘X’ else ” end)[4]
,(case when pp1.row%pp1.[5]=0 then ‘X’ else ” end)[5]
,(case when pp1.row%pp1.[6]=0 then ‘X’ else ” end)[6]
,(case when pp1.row%pp1.[7]=0 then ‘X’ else ” end)[7]
,(case when pp1.row%pp1.[8]=0 then ‘X’ else ” end)[8]
,(case when pp1.row%pp1.[9]=0 then ‘X’ else ” end)[9] from
(select row,1[1],2[2],3[3],4[4],5[5],6[6],7[7],8[8],9[9]
from (
select [row],[1],[2],[3],[4],[5],[6],[7],[8],[9] from
(Select row,cl.col from
(select row,ROW_NUMBER() over (order by row)[colid] from Rower)a
right join Cols cl on a.colid=cl.Col)b
pivot
(
avg(col)
for [col] in([1],[2],[3],[4],[5],[6],[7],[8],[9])
)as p)pp)pp1 where row is not null
LikeLike
Good one !
LikeLike
;WIth Cter3 AS (
SELECT Row, Col, CASE WHEN Row%col=0 THEN ‘X’ ELSE ” END AS res
FROM Rower R ,Cols C
)
SELECT row, [1],[2],[3],[4],[5],[6],[7],[8],[9]
FROM CTEr3
PIVOT ( min(res) FOR Col IN ([1],[2],[3],[4],[5],[6],[7],[8],[9])) as pvt;
LikeLiked by 1 person