Tags
Complex SQL Challenges, complex sql statement(puzzle), Complex TSQL Challenge, Divide rows into two columns, 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, last non null puzzle, Learn complex SQL, Learn SQL, Learn T-SQL, Objective Puzzle, Pawan, Pawan Khowal, Pawan Kumar, Pawan Kumar Khowal, PL/SQL Challenges, Prev Value puzzle, Previous value puzzle, 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, T-SQL Query | [ The Complex Week Puzzle ], The Biggest Gap Puzzle, The Gap Puzzle, The Tree Puzzle, TOP Clause, Tough SQL Challenges, Tough SQL Puzzles, Tricky Questions, TSQL, TSQL Challenge, TSQL Challenges, TSQL Interview questions, TSQL Queries, Week puzzle
T-SQL Query | [ Change 0 to 1 and 1 to 0 Puzzle ]
Puzzle Description
1. We have a table called ZeroOne.
2. Here we need to show 1 instead of 0 and 0 instead of 1.
3. Please check out the sample input and expected output for details.
Sample Input
Id |
0 |
1 |
0 |
1 |
0 |
0 |
Expected Output
Id | Ids |
0 | 1 |
1 | 0 |
0 | 1 |
1 | 0 |
0 | 1 |
0 | 1 |
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 ZeroOne ( Id INT ) GO INSERT INTO ZeroOne VALUES (0),(1),(0),(1),(0),(0) --
Update June 4 | Multiple Solutions – Pawan Kumar Khowal
-- SELECT *, CASE ID WHEN 0 THEN 1 ELSE 0 END Ids FROM ZeroOne SELECT *, (Id - 1) * -1 Ids FROM ZeroOne SELECT *, 1 - Id Ids FROM ZeroOne SELECT *, IIF(ID=0,1,0) Ids FROM ZeroOne SELECT Id, (Id+1/2 -1) * -1 Ids FROM ZeroOne DECLARE @t AS INT = 1 SELECT Id, CHOOSE(@t,1,0) Ids FROM ZeroOne --
Add a comment if you have any other solution in mind. We all need to learn.
Keep Learning
Http://MSBISkills.com
Pawan Kumar Khowal
select id, id^1 ids from ZeroOne
LikeLike