I'm trying to generate a set of 10,000 records consisting of random occurrences of three values. I'm using ABS(CHECKSUM(NewId())) % 3 to get a random number between 0 and 2 (inclusive). The query works fine if I define the above expression in a WITH CTE, then use it in a subsequent CASE WHEN. However, when I define it directly in the CASE WHEN, I start getting NULLs. What is the reason for this?
WITH x AS (
SELECT n
FROM (VALUES (0), (1), (2), (3), (4), (5), (6), (7), (8), (9)) v(n)
),
y as (
SELECT ABS(CHECKSUM(NewId())) % 3 as r
FROM x ones, x tens, x hundreds, x thousands
)
SELECT r,
CASE r WHEN 0 THEN 'Foo' WHEN 1 THEN 'Bar' WHEN 2 THEN 'Baz' END,
CASE ABS(CHECKSUM(NewId())) % 3 WHEN 0 THEN 'Foo' WHEN 1 THEN 'Bar' WHEN 2 THEN 'Baz' END
FROM y
