Tags

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


SQL Puzzle | The Pattern Puzzle #2 ( Fun with SQL )

Puzzle Statement

In this puzzle you got to print following pattern without using loop or cursor. You are not going to get this kind of thing in your projects. This is only for fun with SQL. It was originally asked at – http://seankriegler.com/just-fun/

The patterns we are looking for are –

Pawan Khowal - The Pattern Puzzle

Pawan Khowal – The Pattern Puzzle

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

SOLUTION 1 | USING Numbers Table & SELECT

--


SELECT CONCAT(REPLICATE('_', 20 - number), Vals) Pattern
FROM
	(
		SELECT DISTINCT REPLICATE(number,number) Vals, number
		FROM MASTER..SPT_VALUES
		WHERE number > 0 and number < 21
	)a
ORDER BY number DESC


--

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

Enjoy !!! Keep Learning

Pawan Khowal 

Http://MSBISkills.com

Advertisements