-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathWindow Functions.sql
More file actions
153 lines (125 loc) · 4.18 KB
/
Copy pathWindow Functions.sql
File metadata and controls
153 lines (125 loc) · 4.18 KB
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
-- Window Functions practice
--- AVG price using OVER
SELECT
id,
name,
neighbourhood_group,
AVG(price) OVER() -- adding "OVER" will show all columns, instead of a single column
FROM Airbnb_NYC;
--- Difference between AVG price vs price
SELECT
id,
name,
neighbourhood_group,
neighbourhood,
round(AVG(price) OVER(),2) as AVG_Price,
round(price - AVG(price) OVER(),2) as Price_AVG_Diff
FROM Airbnb_NYC;
-- PARTITION BY neighbourhood_group
SELECT
id,
name,
neighbourhood_group,
neighbourhood,
round(AVG(price) OVER(PARTITION BY neighbourhood_group),2) as AVG_Price_within_neighbourhood_group -- the "AVG" function now calculates the average by the neighbourhood_group using "PARTITION BY" instead of all rows
FROM Airbnb_NYC;
-- PARTITION BY neighbourhood_group and neighbourhood
SELECT
id,
name,
neighbourhood_group,
neighbourhood,
round(AVG(price) OVER(PARTITION BY neighbourhood_group),2) as AVG_Price_within_neighbourhood_group,
round(AVG(price) OVER(PARTITION BY neighbourhood_group,neighbourhood),2) as AVG_Price_within_neighbourhood_group_and_neighbourhood -- the "AVG" function now calculates the average by the neighbourhood_group and neighbourhood
FROM Airbnb_NYC;
-- PARTITION BY neighbourhood_group and neighbourhood, and the diff between price and neighbourhood
SELECT
id,
name,
neighbourhood_group,
neighbourhood,
round(AVG(price) OVER(PARTITION BY neighbourhood_group),2) as AVG_Price_within_neighbourhood_group,
round(AVG(price) OVER(PARTITION BY neighbourhood_group,neighbourhood),2) as AVG_Price_within_neighbourhood_group_and_neighbourhood,
round(price - AVG(price) OVER(PARTITION BY neighbourhood_group,neighbourhood),2) as group_and_neighbouhood_diff_vs_price -- diff between the listing price and the avg price of the neighbourhood_group and neighbourhood
FROM Airbnb_NYC;
-- ROW_NUMBER
SELECT
id,
name,
neighbourhood_group,
neighbourhood,
price,
row_number() OVER(ORDER by price DESC) as price_rank -- ranking all the rows by highest price
FROM Airbnb_NYC;
-- ROW_NUMBER and PARTITION by neighbourhood_group
SELECT
id,
name,
neighbourhood_group,
neighbourhood,
price,
row_number() OVER(ORDER by price DESC) as price_rank,
row_number() OVER(PARTITION by neighbourhood_group ORDER by price DESC) as neighbourhood_group_price_rank -- ranking within the neighbourhood_group
FROM Airbnb_NYC;
-- Top 3 by neighbourhood_group
SELECT
id,
name,
neighbourhood_group,
neighbourhood,
price,
row_number() OVER(ORDER by price DESC) as price_rank,
row_number() OVER(PARTITION by neighbourhood_group ORDER by price DESC) as neighbourhood_group_price_rank,
CASE
WHEN row_number() OVER(PARTITION by neighbourhood_group ORDER by price desc) <= 3 THEN 'yes'
ELSE 'no'
END as is_top3
FROM Airbnb_NYC;
-- Only showing Top 3 ROWS
SELECT * FROM (
SELECT
id,
name,
neighbourhood_group,
neighbourhood,
price,
row_number() OVER(ORDER by price DESC) as price_rank,
row_number() OVER(PARTITION by neighbourhood_group ORDER by price DESC) as neighbourhood_group_price_rank,
CASE
WHEN row_number() OVER(PARTITION by neighbourhood_group ORDER by price desc) <= 3 THEN 'yes'
ELSE 'no'
END as is_top3
FROM Airbnb_NYC
) aaa
WHERE is_top3 = 'yes'; -- same query as before, but only showing the rows where neighbourhood_group is in the top 3
-- RANK function
SELECT
id,
name,
neighbourhood_group,
neighbourhood,
price,
row_number() OVER(ORDER by price DESC) as price_rank,
rank() OVER(ORDER by price DESC) as price_rank_rank, --- RANK function will give the same rank to rows that have the same price value
row_number() OVER(PARTITION by neighbourhood_group ORDER by price DESC) as neighbourhood_group_price_rank,
rank() OVER(PARTITION by neighbourhood_group ORDER by price DESC) as neighbourhood_group_price_rank_rank
FROM Airbnb_NYC;
-- lag
SELECT
id,
name,
host_name,
last_review,
price,
lag(price, 1) OVER(PARTITION by host_name ORDER by last_review) as lag -- retrives the price from the previous last_review date (by 1 period)
FROM Airbnb_NYC;
-- lead
SELECT
id,
name,
host_name,
last_review,
price,
lead(price, 1) OVER(PARTITION by host_name ORDER by last_review) as lead -- retrives the price from the next last_review date (by 1 period)
FROM Airbnb_NYC;
-- by Nikhil B