1 | set search_path = project;
|
---|
2 |
|
---|
3 | drop materialized view if exists avg_time_diffs;
|
---|
4 | CREATE MATERIALIZED VIEW avg_time_diffs AS
|
---|
5 | WITH 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)
|
---|
24 | SELECT
|
---|
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
|
---|
29 | FROM time_differences
|
---|
30 | GROUP BY start_station_id,
|
---|
31 | end_station_id
|
---|
32 | ORDER BY start_station_id,
|
---|
33 | end_station_id;
|
---|
34 |
|
---|
35 | create unique index avg_time_diffs_index on avg_time_diffs (id);
|
---|
36 |
|
---|
37 | drop materialized view if exists kazna_po_linija;
|
---|
38 | create materialized view kazna_po_linija as
|
---|
39 | select row_number() over () as id, l.id as line_id, count(l.id)
|
---|
40 | from 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
|
---|
44 | where extract(year from kaz.date_created) = extract(year from now())
|
---|
45 | group by l.id
|
---|
46 | order by count(l.id) desc;
|
---|
47 |
|
---|
48 | create unique index index_kazna_po_linija on kazna_po_linija(id);
|
---|
49 |
|
---|
50 | drop materialized view if exists most_busy_part_of_the_day;
|
---|
51 | create materialized view most_busy_part_of_the_day as
|
---|
52 | select row_number() over () as id, id as linija_id, interval_1h, count(id || '-' || interval_1h) broj_pati
|
---|
53 | from (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
|
---|
60 | group by id, interval_1h
|
---|
61 | order by interval_1h desc;
|
---|
62 |
|
---|
63 | create unique index index_most_busy_part_of_the_day on most_busy_part_of_the_day(id);
|
---|
64 |
|
---|
65 | drop materialized view if exists commutes_by_line;
|
---|
66 | create materialized view commutes_by_line as
|
---|
67 | select row_number() over () as id, l.id as linija_id, count(v.id)
|
---|
68 | from 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
|
---|
71 | where v.start_date between date_trunc('year', now()) and date_trunc('month', date_trunc('year', now())+ interval '7 months')
|
---|
72 | group by l.id;
|
---|
73 |
|
---|
74 |
|
---|
75 | drop materialized view if exists number_passengers_per_line_and_station;
|
---|
76 | create materialized view number_passengers_per_line_and_station as
|
---|
77 | select row_number() over () as id, p.id as station_id, l.id as line_id, count(p.id)
|
---|
78 | from 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
|
---|
83 | where 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))
|
---|
84 | group by p.id, l.id
|
---|
85 | order by count(p.id) desc;
|
---|
86 |
|
---|
87 |
|
---|
88 | drop materialized view if exists ticket_sales;
|
---|
89 | create materialized view ticket_sales as
|
---|
90 | select row_number() over () as id, filtered.tip_id, filtered.interval_1h, filtered.broj_pati
|
---|
91 | from (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
|
---|
102 | where 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 |
|
---|
121 | drop materialized view if exists total_income;
|
---|
122 | create materialized view total_income as
|
---|
123 | WITH 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 | )
|
---|
144 | SELECT
|
---|
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
|
---|
150 | FROM
|
---|
151 | kazna_sum k
|
---|
152 | FULL OUTER JOIN bilet_sum b ON k.year = b.year
|
---|
153 | ORDER BY
|
---|
154 | year desc;
|
---|
155 |
|
---|
156 | create unique index index_total_income on total_income(id); |
---|