advanced databases

  Kiến thức lập trình

ou’ve been given a database for an online streaming service that first released in 2020. The database has a table customers with a column net_new that records net new monthly subscriptions to the streaming service broken down by tiers: free, standard, and premium.

Write a SELECT statement using windows functions to calculate two new columns from net_new:

total: this column should contain a running total count of customers partitioned by the three subscription tiers
year_over_year: for each month, this column should subtract the net_new value 12 months earlier from the current net_new value. This calculation should also be partitioned by level. Use the default LAG behavior for the first 12 months (2020), which will be null.
Your final SELECT query should have the columns year, month, level, total, and year_over_year in that order. Do not apply any additional order by statements, other than the ones needed within the window functions to properly perform the calculation.

SELECT year,
month,
level,
SUM(net_new) OVER(
PARTITION BY level
ORDER BY year, month
) AS ‘total’,
net_new – LAG(net_new, 12, 0) OVER(
PARTITION BY level
ORDER BY year, month
) AS ‘year_over_year’
FROM customers;

but i get this error
I’ve tried tons of responses but all ended with this error message.
** Not quite! Check that your columns are in the right order, and that you’ve used SUM and LAG with the correct window syntax.**

an answer tothe question

New contributor

Mahomed Sameer Yusuf is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

LEAVE A COMMENT