source: bus-n-go-pavel-216049/bus-n-go-backend/src/main/resources/db/migration/R__materialized_views.sql

Last change on this file was baf4cc4, checked in by ppaunovski <paunovskipavel@…>, 3 months ago

split group project and individual project into two separate folders

  • Property mode set to 100644
File size: 7.3 KB
Line 
1set search_path = project;
2
3drop materialized view if exists avg_time_diffs;
4CREATE MATERIALIZED VIEW avg_time_diffs AS
5WITH ordered_instanca AS (SELECT ilpl.instanca_na_linija_id,
6 ilpl.postojka_na_linija_id,
7 pnl.reden_broj,
8 ilpl.timestamp
9 FROM project.instanca_na_linija_postojka_na_linija ilpl
10 JOIN
11 project.postojka_na_linija pnl ON ilpl.postojka_na_linija_id = pnl.id
12 ORDER BY ilpl.instanca_na_linija_id,
13 pnl.reden_broj),
14 time_differences AS (SELECT o1.instanca_na_linija_id,
15 o1.postojka_na_linija_id AS start_station_id,
16 o1.timestamp AS start_time,
17 o2.postojka_na_linija_id AS end_station_id,
18 o2.timestamp AS end_time,
19 EXTRACT(EPOCH FROM (o2.timestamp - o1.timestamp)) AS time_diff
20 FROM ordered_instanca o1
21 JOIN
22 ordered_instanca o2 ON o1.instanca_na_linija_id = o2.instanca_na_linija_id
23 WHERE o1.reden_broj + 1 = o2.reden_broj)
24SELECT
25 row_number() over () as id,
26 start_station_id,
27 end_station_id,
28 AVG(time_diff)::double precision AS avg_time_diff_seconds
29FROM time_differences
30GROUP BY start_station_id,
31 end_station_id
32ORDER BY start_station_id,
33 end_station_id;
34
35create unique index avg_time_diffs_index on avg_time_diffs (id);
36
37drop materialized view if exists kazna_po_linija;
38create materialized view kazna_po_linija as
39select row_number() over () as id, l.id as line_id, count(l.id)
40from project.linija l
41 left join project.instanca_na_linija inl on l.id = inl.linija_id
42 left join project.kontrola k on inl.id = k.instanca_na_linija_id
43 left join project.kazna kaz on k.id = kaz.kontrola_id
44where extract(year from kaz.date_created) = extract(year from now())
45group by l.id
46order by count(l.id) desc;
47
48create unique index index_kazna_po_linija on kazna_po_linija(id);
49
50drop materialized view if exists most_busy_part_of_the_day;
51create materialized view most_busy_part_of_the_day as
52select row_number() over () as id, id as linija_id, interval_1h, count(id || '-' || interval_1h) broj_pati
53from (select li.id,
54 (to_char(extract(hour from v.start_date), '00') || ':00 -' || to_char(extract(hour from (v.start_date
55 + '1 hours'::interval))::int, '00') || ':00') as interval_1h
56 from project.linija li
57 left join project.instanca_na_linija inl on li.id = inl.linija_id
58 left join project.vozenje v on inl.id = v.instanca_na_linija_id
59 where v.start_date between (make_date(extract(year from (now()))::int, 1, 1)) and (make_date(extract(year from (now()))::int, 12, 31))) as hours
60group by id, interval_1h
61order by interval_1h desc;
62
63create unique index index_most_busy_part_of_the_day on most_busy_part_of_the_day(id);
64
65drop materialized view if exists commutes_by_line;
66create materialized view commutes_by_line as
67select row_number() over () as id, l.id as linija_id, count(v.id)
68from project.linija l
69 left join project.instanca_na_linija inl on l.id = inl.linija_id
70 left join project.vozenje v on inl.id = v.instanca_na_linija_id
71where v.start_date between date_trunc('year', now()) and date_trunc('month', date_trunc('year', now())+ interval '7 months')
72group by l.id;
73
74
75drop materialized view if exists number_passengers_per_line_and_station;
76create materialized view number_passengers_per_line_and_station as
77select row_number() over () as id, p.id as station_id, l.id as line_id, count(p.id)
78from project.postojka p
79 join project.postojka_na_linija pnl on p.id = pnl.postojka_id
80 join project.vozenje v on pnl.id = v.postojka_na_linija_start_id
81 join project.patnik pat on v.patnik_id = pat.id
82 join project.linija l on pnl.linija_id = l.id
83where v.start_date between (make_date(extract(year from (now() - '1 years'::interval))::int, 4, 1)) and (make_date(extract(year from (now() - '1 years'::interval))::int, 6, 30))
84group by p.id, l.id
85order by count(p.id) desc;
86
87
88drop materialized view if exists ticket_sales;
89create materialized view ticket_sales as
90select row_number() over () as id, filtered.tip_id, filtered.interval_1h, filtered.broj_pati
91from (select filter.tip_id, interval_1h, count(tip_id || '-' || interval_1h) broj_pati
92 from (select *,
93 (to_char(extract(hour from b.datum_kupuvanje), '00') || ':00 -' ||
94 to_char(extract(hour from (b.datum_kupuvanje + '1 hours'::interval))::int, '00') ||
95 ':00') as interval_1h
96 from project.tipbilet t
97 left join project.bilet b on t.id = b.tip_id
98-- where b.datum_kupuvanje between (make_date(extract(year from (now() - '1 years'::interval))::int, 1, 1)) and (make_date(extract(year from (now() - '1 years'::interval))::int, 12, 31))
99
100 ) as filter
101 group by filter.tip_id, interval_1h) filtered
102where broj_pati = (select max(filtered.broj_pati)
103 from (select filter.tip_id, interval_1h, count(tip_id || '-' || interval_1h) broj_pati
104 from (select *,
105 (to_char(extract(hour from b.datum_kupuvanje), '00') || ':00 -' ||
106 to_char(extract(hour from (b.datum_kupuvanje + '1 hours'::interval))::int,
107 '00') ||
108 ':00') as interval_1h
109 from project.tipbilet t
110 left join project.bilet b on t.id = b.tip_id
111-- where b.datum_kupuvanje between (make_date(extract(year from (now() - '1 years'::interval))::int, 1, 1)) and (make_date(extract(year from (now() - '1 years'::interval))::int, 12, 31))
112
113 ) as filter
114 group by filter.tip_id, interval_1h) filtered)
115 and tip_id in (select id
116 from tipbilet
117 where ime in ('Hourly', 'Daily'));
118
119
120
121drop materialized view if exists total_income;
122create materialized view total_income as
123WITH kazna_sum AS (
124 SELECT
125 EXTRACT(YEAR FROM k.date_payed) AS year,
126 SUM(k.iznos) AS sum_kz
127 FROM
128 project.kazna k
129 WHERE
130 k.plateno = TRUE
131 GROUP BY
132 EXTRACT(YEAR FROM k.date_payed)
133),
134 bilet_sum AS (
135 SELECT
136 EXTRACT(YEAR FROM b.datum_kupuvanje) AS year,
137 SUM(tb.cena) AS sum_b
138 FROM
139 project.bilet b
140 JOIN tipbilet tb ON tb.id = b.tip_id
141 GROUP BY
142 EXTRACT(YEAR FROM b.datum_kupuvanje)
143 )
144SELECT
145 row_number() over () as id,
146 COALESCE(k.year, b.year)::text AS year,
147 COALESCE(sum_kz, 0) as fines_income,
148 COALESCE(sum_b, 0) as ticket_income,
149 COALESCE(sum_kz, 0) + COALESCE(sum_b, 0) AS total_income
150FROM
151 kazna_sum k
152 FULL OUTER JOIN bilet_sum b ON k.year = b.year
153ORDER BY
154 year desc;
155
156create unique index index_total_income on total_income(id);
Note: See TracBrowser for help on using the repository browser.