I have been working on calculating user churn as part of section 6. Analyze real data with SQL.
It seems that if I try to called a temporary table multiple times, nothing appears in the output.
For example
WITH months1 AS
(SELECT
'2017-01-01' as first_day,
'2017-01-31' as last_day
UNION
SELECT
'2017-02-01' as first_day,
'2017-02-28' as last_day
FROM subscriptions
)
SELECT *
FROM months1;
SELECT first_day
FROM months1;
There is no output with those code. If I remove either
SELECT first_day
FROM months1;
or
SELECT *
FROM months1;
then there is an output. Is it only possible to call temporary table once? Is the only way to get both queries to write the following?
WITH months1 AS
(SELECT
'2017-01-01' as first_day,
'2017-01-31' as last_day
UNION
SELECT
'2017-02-01' as first_day,
'2017-02-28' as last_day
FROM subscriptions
)
SELECT *
FROM months1;
WITH months1 AS
(SELECT
'2017-01-01' as first_day,
'2017-01-31' as last_day
UNION
SELECT
'2017-02-01' as first_day,
'2017-02-28' as last_day
FROM subscriptions
)
SELECT first_day
FROM months1;
Is the right step to create a view I don’t want to write the same temporary table again and again?
Thanks