re: Fizz Buzz in Every Language VIEW POST

FULL DISCUSSION
 

Sql anybody?

DECLARE @i INT = 100;

;WITH E1(N) AS (SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL 
                SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL 
                SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1), -- 1*10^1 or 10 rows
      E2(N) AS (SELECT 1 FROM E1 a, E1 b),                                          -- 1*10^2 or 100 rows
      E4(N) AS (SELECT 1 FROM E2 a, E2 b),                                          -- 1*10^4 or 10,000 rows
      E8(N) AS (SELECT 1 FROM E4 a, E4 b),                                          -- 1*10^8 or 100,000,000 rows
cteTally(N) AS (SELECT TOP (@i) ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) FROM E8)

SELECT  CASE WHEN N % 15 = 0 THEN 'fizzbuzz'
             WHEN N % 5 = 0 THEN 'buzz'
             WHEN N % 3 = 0 THEN 'fizz'  
             ELSE CONVERT(VARCHAR(10),N) END
FROM    cteTally


`

 
code of conduct - report abuse