Skip to content

Commit 14760e1

Browse files
committed
new codes
1 parent dd5b608 commit 14760e1

File tree

4 files changed

+92
-1
lines changed

4 files changed

+92
-1
lines changed

30_question/auto_Repair.sql

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
3+
CREATE TABLE auto_repair_data (
4+
client VARCHAR(2),
5+
auto VARCHAR(2),
6+
repair_date INTEGER,
7+
indicator VARCHAR(10),
8+
value VARCHAR(10)
9+
);
10+
11+
12+
INSERT INTO auto_repair_data (client, auto, repair_date, indicator, value) VALUES
13+
('c1', 'a1', 2022, 'level', 'good'),
14+
('c1', 'a1', 2022, 'velocity', '90'),
15+
('c1', 'a1', 2023, 'level', 'regular'),
16+
('c1', 'a1', 2023, 'velocity', '80'),
17+
('c1', 'a1', 2024, 'level', 'wrong'),
18+
('c1', 'a1', 2024, 'velocity', '70'),
19+
('c2', 'a1', 2022, 'level', 'good'),
20+
('c2', 'a1', 2022, 'velocity', '90'),
21+
('c2', 'a1', 2023, 'level', 'wrong'),
22+
('c2', 'a1', 2023, 'velocity', '50'),
23+
('c2', 'a2', 2024, 'level', 'good'),
24+
('c2', 'a2', 2024, 'velocity', 80);
25+
26+
27+
"50" "wrong" 1
28+
"70" "wrong" 1
29+
"80" "good" 1
30+
"80" "regular" 1
31+
"90" "good" 2
32+
33+
*/
34+
35+
36+
select
37+
velocity,
38+
coalesce (good, 0) as good,
39+
coalesce (wrong, 0) as wrong,
40+
coalesce (regular, 0) as regular
41+
from crosstab('select
42+
b.value as velocity, a.value as level, count(1) as value
43+
from
44+
auto_repair_data a
45+
join
46+
auto_repair_data b on a.client=b.client and a.auto=b.auto and a.repair_date=b.repair_date
47+
where
48+
a.indicator=''level'' and b.indicator=''velocity''
49+
group by 1,2
50+
order by 1,2'
51+
, 'select distinct value from auto_repair_data where indicator=''level'' order by value')
52+
as result(velocity varchar, good bigint, regular bigint, wrong bigint)

30_question/remove_outliers.sql

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
3+
PROBLEM STATEMENT: In the given input table, there are hotel ratings which are either too high or too low compared to the standard ratings the hotel receives each year. Write a query to identify and exclude these outlier records as shown in expected output below.
4+
Your output should follow the same order of records as shown.
5+
6+
CREATE TABLE hotel_ratings (
7+
hotel VARCHAR(50),
8+
year INTEGER,
9+
rating DECIMAL(2,1)
10+
);
11+
12+
13+
INSERT INTO hotel_ratings (hotel, year, rating) VALUES
14+
('Radisson Blu', 2020, 4.8),
15+
('Radisson Blu', 2021, 3.5),
16+
('Radisson Blu', 2022, 3.2),
17+
('Radisson Blu', 2023, 3.4),
18+
('InterContinental', 2020, 4.2),
19+
('InterContinental', 2021, 4.5),
20+
('InterContinental', 2022, 1.5),
21+
('InterContinental', 2023, 3.8);
22+
23+
*/
24+
25+
select *, row_number() over(partition by hotel order by rating) from hotel_ratings
26+
27+
select
28+
hotel, min(rating), max(rating),
29+
percentile_cont(0.1) within group(order by rating) "P10",
30+
percentile_cont(0.5) within group(order by rating) "P50",
31+
percentile_cont(0.75) within group(order by rating) "P75",
32+
percentile_cont(0.9) within group(order by rating) "P90",
33+
percentile_cont(0.99) within group(order by rating) "P99"
34+
from hotel_ratings group by hotel

Notes.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# By default (range between unbounded preceding and current row):
2+
- This specifies that the window frame includes all rows from the start of the partition up to and including the current row.
3+
4+
# Explicitly stated (range between unbounded preceding and unbounded following):
5+
- This specifies that the window frame includes all rows from the start of the partition up to the end of the partition, regardless of the current row.

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
# SQL-Learn
2-
PostgreSQL
2+
PostgreSQL

0 commit comments

Comments
 (0)