Tags

, , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ,


SQL Puzzle | 2 Digit Puzzle

Here you need to write a query to get distinct digits from the table. Now here half data has been reversely inserted. e.g. For 21 we have inserted 12. So out of 12 and 21 we need 21 as a single row.

For details please check out the sample input and the expected output below.

Sample Inputs

Id
21
12
34
43
29
92

Expected Output

Id
21
43
92

Rules/Restrictions

  • The challenge is to do it with T-SQL statements only.
  • Reverse function is not allowed.
  • Add your solution(s) in the comments section or send you solution(s) to pawankkmr@gmail.com

Script | use below script to create table and insert some sample data


--



CREATE TABLE [2Digits]
(
	Id INT
)
GO

INSERT INTO [2Digits] VALUES
(21),
(12),
(34),
(43),
(29),
(92)



--

SOLUTION – 1


--

SELECT 
	DISTINCT CASE WHEN Id % 10 < (id / 10) THEN (id / 10)*10 + (Id % 10) ELSE (Id % 10 )*10+(id / 10) END Id 
FROM 
[2Digits]

--

Add a comment if you have a solution in mind. I would love to learn it. We all need to learn.

Enjoy !!! Keep Learning

Pawan Khowal

Http://MSBISkills.com

Advertisements