Wednesday, January 6, 2016

A More Efficient 12 Days of Christmas

In a fit of nerdiness, I decided to re-write the classic Christmas carol "The Twelve Days of Christmas" in SQL:
SELECT (ROW_NUMBER() OVER (ORDER BY Xmas1.DayNum DESC)) * Xmas2.DayNum AS GiftQuantity,
Xmas2.DayNum,
Xmas2.GiftTitle
FROM TwelveDaysXmas Xmas1
INNER JOIN TwelveDaysXmas Xmas2
ON Xmas1.DayNum = Xmas2.DayNum

Results:
GiftQuantity DayNum   GiftTitle
12           12       Drummers Drumming
22           11       Pipers Piping
30           10       Lords a-leaping
36           9        Ladies Dancing
40           8        Maids a-milking
42           7        Swans a-swimming
42           6        Geese a-laying
40           5        Gold Rings
36           4        Calling Birds
30           3        French Hens
22           2        Turtle Doves
12           1        Partridge In Pear Tree
See? Much easier than singing 12 choruses!