Consider the following table:
id title genre1 genre2 genre3 genre4 genre5
1 Movie1 Thriller Crime NULL NULL NULL
2 Movie2 Fantasy NULL NULL NULL NULL
3 Movie3 Political Philosophical Historical NULL NULL
4 Movie4 Science Fiction NULL NULL NULL NULL
When I unpivot genre1, genre2, ... the NULL values are removed, so that, for example, the rows for Movie1 would be:
id title genre original_column
1 Movie1 Thriller genre1
1 Movie1 Crime genre2
Is there a way to include NULL values, for example:
id title genre original_column
1 Movie1 Thriller genre1
1 Movie1 Crime genre2
1 Movie1 NULL genre3
1 Movie1 NULL genre4
1 Movie1 NULL genre5
I can SELECT ISNULL(genre1, '*'), ISNULL(genre2, '*') ..., but suppose I want the values to appear as NULL? Is there a better way than:
WITH
-- SELECT that unpivots table
AS unpivoted
SELECT unpivoted.id,
unpivoted.title,
,CASE unpivoted.genre WHEN '*' THEN NULL ELSE unpivoted.genre END
,unpivoted.original_transaction
FROM unpivoted
Also, I notice that the query that returns the source data to be unpivoted MUST have an alias. For example:
SELECT ... FROM
-- *must* give it an alias or it's a syntax error
(SELECT ... FROM) AS source
UNPIVOT(genre for original_column IN (genre1,genre2, genre3,genre4,genre5))
Does anyone know why? I have not found a way to refer to the alias anywhere in the query.