Skip to content

Commit 6898ab8

Browse files
committedJun 9, 2024
new code added and changes
1 parent 1598de5 commit 6898ab8

File tree

2 files changed

+96
-34
lines changed

2 files changed

+96
-34
lines changed
 

‎consequitive.sql

+13-15
Original file line numberDiff line numberDiff line change
@@ -12,26 +12,24 @@ WITH cte AS (
1212
LAST_VALUE(model) OVER (PARTITION BY category ORDER BY price DESC RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) AS least_expensive_product_name
1313
FROM products
1414
)
15-
SELECT
16-
category,
17-
model,
18-
price,
19-
expensive_product_name,
20-
least_expensive_product_name
21-
FROM cte;
15+
SELECT * FROM cte;
16+
2217

2318
-- Query to find users who have logged in consecutively 3 or more times
2419

2520
SELECT DISTINCT repeated_names
2621
FROM (
27-
SELECT *,
28-
-- Check if the current user_name is the same as the next two user_name values
29-
CASE
30-
WHEN user_name = LEAD(user_name) OVER (ORDER BY login_id)
31-
AND user_name = LEAD(user_name, 2) OVER (ORDER BY login_id)
32-
THEN user_name
33-
ELSE NULL
34-
END AS repeated_names
22+
SELECT
23+
*,
24+
-- Check if the current user_name is the same as the next two user_name values
25+
CASE
26+
WHEN
27+
user_name = LEAD(user_name) OVER (ORDER BY login_id) AND
28+
user_name = LEAD(user_name, 2) OVER (ORDER BY login_id)
29+
THEN
30+
user_name
31+
END AS repeated_names
32+
3533
FROM login_details
3634
) AS x
3735
-- Filter out rows where repeated_names is not null

‎median.sql

+83-19
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,84 @@
1-
-- median (odd set of numbers) = ((n+1)/2)th term
2-
-- median (even set of numbers) = ((n/2)th term + ((n/2)+1)th term)/2
3-
4-
with cte as (
5-
select student_id, sat_writing, rank() over(order by sat_writing) rn
6-
from sat_scores
7-
),
8-
cte2 as (
9-
select
10-
case
11-
when
12-
(count(*) % 2) = 0 then (count(*) / 2) + (count(*) / 2) + 1 -- even median
13-
else
14-
(count(*) + 1 ) / 2 end as cn -- odd median
15-
from cte
1+
/*
2+
Median (odd set of numbers) = ((n+1)/2)th term
3+
4+
Median (even set of numbers) = ((n/2)th term + ((n/2)+1)th term)/2
5+
6+
The median is also known as the 50th percentile
7+
8+
9+
*/
10+
11+
-- Using Window Function
12+
13+
14+
-- This is more accurate one in fraction
15+
16+
17+
SELECT
18+
PERCENTILE_CONT(0.5) WITHIN GROUP (ORDER BY column_name) AS median
19+
FROM
20+
table_name;
21+
22+
23+
-- One more way to calculate median is by using the following query:
24+
25+
SELECT
26+
percentile_disc(0.5) WITHIN GROUP (
27+
ORDER BY temperature)
28+
FROM city_data;
29+
30+
-- Because the query used percentile_disc(), the result is a value that exists in the dataset.
31+
32+
33+
-- Calculating Multiple Percentiles
34+
35+
36+
SELECT
37+
device_id,
38+
percentile_cont(0.25) WITHIN GROUP(
39+
ORDER BY
40+
humidity) AS percentile_25,
41+
percentile_cont(0.50) WITHIN GROUP(
42+
ORDER BY
43+
humidity) AS percentile_50,
44+
percentile_cont(0.75) WITHIN GROUP(
45+
ORDER BY
46+
humidity) AS percentile_75,
47+
percentile_cont(0.95) WITHIN GROUP(
48+
ORDER BY
49+
humidity) AS percentile_95
50+
FROM
51+
conditions
52+
GROUP BY
53+
device_id;
54+
55+
56+
-- Calculating a Series of Percentiles
57+
58+
SELECT
59+
city,
60+
percentile,
61+
percentile_cont(p) WITHIN GROUP (
62+
ORDER BY
63+
temperature)
64+
FROM
65+
city_data,
66+
generate_series(0.01, 1, 0.01) AS percentile
67+
GROUP BY
68+
city, percentile;
69+
70+
71+
-- Without Window Function
72+
73+
WITH get_median AS (
74+
SELECT
75+
y, row_number() OVER(ORDER BY y ASC) AS rn_asc,
76+
COUNT(*) OVER() AS ct
77+
FROM dataset
1678
)
17-
select cte.student_id, cte2.cn
18-
from cte, cte2
19-
where cte.rn=cte2.cn
20-
order by cte.student_id
79+
SELECT
80+
AVG(y) AS median
81+
FROM
82+
get_median
83+
WHERE
84+
rn_asc BETWEEN ct/2.0 AND ct / 2.0 + 1;

0 commit comments

Comments
 (0)
Please sign in to comment.