I have a spark dataframe in Databricks. I am trying to run some sql query with Common Table Expressions (CTEs). Here is a first 10 rows of data
+----------+----------+------+---+---+---------+-----------------+
| data_date| user_id|region|sex|age|age_group|sum(duration_min)|
+----------+----------+------+---+---+---------+-----------------+
|2020-01-01|22600560aa| 1| 1| 28| 2| 0.0|
|2020-01-01|17148900ab| 6| 2| 60| 5| 1138.0|
|2020-01-01|21900230aa| 5| 1| 43| 4| 0.0|
|2020-01-01|35900050ac| 8| 1| 16| 1| 224.0|
|2020-01-01|22300280ad| 6| 2| 44| 4| 8.0|
|2020-01-02|19702160ac| 2| 2| 55| 5| 0.0|
|2020-02-02|17900020aa| 5| 2| 64| 5| 264.0|
|2020-02-02|16900120aa| 3| 1| 69| 6| 0.0|
|2020-02-02|11160900aa| 6| 2| 52| 5| 0.0|
|2020-03-02|16900290aa| 5| 1| 37| 3| 0.0|
+----------+----------+------+---+---+---------+-----------------+
Here I stored each user registration date in the regs CTE and then calculate the number of registrations per month. This chunk with CTE is working without any issue in Databricks
%sql
WITH regs AS (
SELECT
user_id,
MIN(data_date) AS reg_date
FROM df2
GROUP BY user_id)
SELECT
month(reg_date) AS reg_month,
COUNT(DISTINCT user_id) AS users
FROM regs
GROUP BY reg_month
ORDER BY reg_month ASC;
However when I add another CTE into my previous sql query, it returns an error ( I test this chunk in sql server and it was working fine). I couldn't figure it out why is not working in spark databricks.
%sql
WITH regs AS (
SELECT
user_id,
MIN(data_date) AS reg_date
FROM df2
GROUP BY user_id
),
regs_per_month AS (
SELECT
month(reg_date) AS reg_month,
COUNT(DISTINCT user_id) AS users
FROM regs
GROUP BY reg_month
)
SELECT
reg_month,
users,
LAG(users, 1) OVER (ORDER BY regs_per_month ASC) AS previous_users
FROM regs_per_month
ORDER BY reg_month ASC;
Here is the error message
Error in SQL statement: AnalysisException: cannot resolve '`regs_per_month`' given input columns: [regs_per_month.reg_month, regs_per_month.users]; line 20 pos 31;
'Sort ['reg_month ASC NULLS FIRST], true
