-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGrowth of Airbnb_LAG
25 lines (21 loc) · 1.18 KB
/
Growth of Airbnb_LAG
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
Growth of Airbnb
Estimate the growth of Airbnb each year using the number of hosts registered as the growth metric. The rate of growth is calculated by taking ((number of hosts registered in the current year - number of hosts registered in the previous year)/the number of hosts registered in the previous year)*100.
Output the year, number of hosts in the current year, number of hosts in the previous year. and the rate of growth. Round the rate of growth. Round the rate of growth to the nearest percent and order the result in the ascending order based on the year.
Assume that the dataset consists conly of unique hosts, meaning there are no duplicate hosts listed.
SELECT year,
current_year_host,
prev_year_host,
round(((curent_year_host - prev_year_host)/(cast(prev_year_host AS numeric)))*100) estimated_growth
FROM
(SELECT year,
current_year_host,
LAG(current_year_host, 1) over (ORDER BY year) AS prev_year_host
FROM
(SELECT extract(year
FROM host_since::date) AS year,
count(id) current_year_host
FROM airbnb_search_details
WHERE host_since IS NOT NULL
GROUP BY extract(year
FROM host_since::date)
ORDER BY year) t1) t2