PostgreSQL で FizzBuzz.
面接の際にどうぞ。(謎
with recursive fizzbuzz(i, result) as ( select 1, '1' union all select i + 1 ,case when (i + 1) % 15 = 0 then 'FizzBuzz' when (i + 1) % 5 = 0 then 'Buzz' when (i + 1) % 3= 0 then 'Buzz' else (i + 1)::text end from fizzbuzz where i < 100 ) select * from fizzbuzz;
BigQuery用に loop で書き直そうと思ったけどメンドクサイからやめたw
手軽にやるならこれか。
select i, case when mod(i, 15) = 0 then 'FizzBuzz' when mod(i, 5) = 0 then 'Buzz' when mod(i, 3) = 0 then 'Fizz' else cast(i as string) end as result from unnest(generate_array(1, 100)) as i;
※いつの間にか BigQuery にも Recursive 実装されてた。
https://cloud.google.com/bigquery/docs/reference/standard-sql/query-syntax#with_clause
MS SQL Server なら T-SQL でこうか。
一時テーブル使うとラクね。
declare @i int = 1; drop table if exists #temptbl; create table #temptbl(i int, result varchar(8)); while @i <= 100 begin insert into #temptbl select @i as i, case when @i % 15 = 0 then 'FizzBuzz' when @i % 5 = 0 then 'Buzz' when @i% 3 = 0 then 'Fizz' else convert(varchar(8), @i) end as result set @i = @i + 1 end select * from #temptbl;