-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path08-postgresql-features.sql
More file actions
96 lines (86 loc) · 2.45 KB
/
Copy path08-postgresql-features.sql
File metadata and controls
96 lines (86 loc) · 2.45 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
-- Lesson 08: PostgreSQL-Specific Query Features
-- Goal: learn practical PostgreSQL features using superstore and periode data.
-- 1. RETURNING gives back rows changed by INSERT.
DROP TABLE IF EXISTS lesson_order_reviews;
CREATE TABLE lesson_order_reviews (
review_id INTEGER GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
row_id INTEGER NOT NULL UNIQUE,
order_id VARCHAR(50) NOT NULL,
product_name TEXT NOT NULL,
review_status TEXT NOT NULL,
reviewed_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
);
INSERT INTO lesson_order_reviews (row_id, order_id, product_name, review_status)
SELECT
row_id,
order_id,
product_name,
'needs-review' AS review_status
FROM superstore
WHERE profit < 0
ORDER BY profit
LIMIT 1
RETURNING review_id, row_id, order_id, review_status, reviewed_at;
-- 2. ON CONFLICT supports upsert workflows.
INSERT INTO lesson_order_reviews (row_id, order_id, product_name, review_status)
SELECT
row_id,
order_id,
product_name,
'reviewed' AS review_status
FROM superstore
WHERE profit < 0
ORDER BY profit
LIMIT 1
ON CONFLICT (row_id)
DO UPDATE SET
review_status = EXCLUDED.review_status,
reviewed_at = CURRENT_TIMESTAMP
RETURNING review_id, row_id, order_id, review_status;
-- 3. generate_series() is useful for test data and calendars.
SELECT
generated_day::DATE AS generated_day
FROM generate_series(
DATE '2017-05-01',
DATE '2017-05-07',
INTERVAL '1 day'
) AS generated_day;
-- 4. FILTER lets you calculate multiple aggregates in one pass.
SELECT
COUNT(*) AS total_orders,
COUNT(*) FILTER (WHERE profit < 0) AS loss_orders,
SUM(sales) FILTER (WHERE region = 'West') AS west_sales
FROM superstore;
-- 5. DISTINCT ON keeps one row per grouping with custom ordering.
SELECT DISTINCT ON (customer_id)
customer_id,
customer_name,
order_date,
sales
FROM superstore
ORDER BY customer_id, order_date DESC, sales DESC;
-- 6. ILIKE performs case-insensitive pattern matching.
SELECT
customer_name,
city,
state
FROM superstore
WHERE customer_name ILIKE '%smith%'
ORDER BY customer_name;
-- 7. ANY compares a value against an array.
SELECT
order_id,
region,
category,
sales
FROM superstore
WHERE region = ANY (ARRAY['West', 'East'])
ORDER BY sales DESC
LIMIT 10;
-- 8. unnest() expands arrays into rows.
SELECT
product_id,
unnest(ARRAY[category, sub_category, region]) AS product_attribute
FROM superstore
ORDER BY product_id, product_attribute
LIMIT 30;