From http://sqlfiddle.com/#!2/9635d2/1