| 1 | -- 1. Приход по филм
|
|---|
| 2 | CREATE VIEW view_movie_revenue AS
|
|---|
| 3 | SELECT m.title,
|
|---|
| 4 | COUNT(t.ticket_id) AS total_tickets,
|
|---|
| 5 | SUM(t.price) AS total_revenue
|
|---|
| 6 | FROM MOVIE m
|
|---|
| 7 | JOIN SCREENING s ON s.movie_id = m.movie_id
|
|---|
| 8 | JOIN TICKET t ON t.screening_id = s.screening_id
|
|---|
| 9 | GROUP BY m.title
|
|---|
| 10 | ORDER BY total_revenue DESC;
|
|---|
| 11 |
|
|---|
| 12 | -- 2. Најгледан жанр
|
|---|
| 13 | CREATE VIEW view_most_watched_genre AS
|
|---|
| 14 | SELECT g.name AS genre,
|
|---|
| 15 | COUNT(t.ticket_id) AS total_tickets,
|
|---|
| 16 | SUM(t.price) AS total_revenue
|
|---|
| 17 | FROM GENRE g
|
|---|
| 18 | JOIN MOVIE_GENRE mg ON mg.genre_id = g.genre_id
|
|---|
| 19 | JOIN SCREENING s ON s.movie_id = mg.movie_id
|
|---|
| 20 | JOIN TICKET t ON t.screening_id = s.screening_id
|
|---|
| 21 | GROUP BY g.name
|
|---|
| 22 | ORDER BY total_tickets DESC;
|
|---|
| 23 |
|
|---|
| 24 | -- 3. Најактивни периоди по месец
|
|---|
| 25 | CREATE VIEW view_busiest_months AS
|
|---|
| 26 | SELECT EXTRACT(YEAR FROM s.date)::int AS year,
|
|---|
| 27 | EXTRACT(MONTH FROM s.date)::int AS month,
|
|---|
| 28 | COUNT(t.ticket_id) AS total_tickets,
|
|---|
| 29 | SUM(t.price) AS total_revenue
|
|---|
| 30 | FROM SCREENING s
|
|---|
| 31 | JOIN TICKET t ON t.screening_id = s.screening_id
|
|---|
| 32 | GROUP BY year, month
|
|---|
| 33 | ORDER BY year, month;
|
|---|
| 34 |
|
|---|
| 35 | -- 4. Приход по кино
|
|---|
| 36 | CREATE VIEW view_cinema_revenue AS
|
|---|
| 37 | SELECT c.name AS cinema,
|
|---|
| 38 | ci.name AS city,
|
|---|
| 39 | COUNT(t.ticket_id) AS total_tickets,
|
|---|
| 40 | SUM(t.price) AS total_revenue
|
|---|
| 41 | FROM CINEMA c
|
|---|
| 42 | JOIN CITY ci ON ci.city_id = c.city_id
|
|---|
| 43 | JOIN CINEMA_HALL ch ON ch.cinema_id = c.cinema_id
|
|---|
| 44 | JOIN SCREENING s ON s.cinemahall_id = ch.cinemahall_id
|
|---|
| 45 | JOIN TICKET t ON t.screening_id = s.screening_id
|
|---|
| 46 | GROUP BY c.name, ci.name
|
|---|
| 47 | ORDER BY total_revenue DESC;
|
|---|
| 48 |
|
|---|
| 49 | -- 5. Попуненост на сали
|
|---|
| 50 | CREATE VIEW view_hall_occupancy AS
|
|---|
| 51 | SELECT ch.description AS hall,
|
|---|
| 52 | c.name AS cinema,
|
|---|
| 53 | ch.capacity,
|
|---|
| 54 | COUNT(t.ticket_id) AS total_tickets_sold,
|
|---|
| 55 | ROUND((COUNT(t.ticket_id)::numeric / (ch.capacity * COUNT(DISTINCT s.screening_id)) * 100), 2) AS occupancy_percent
|
|---|
| 56 | FROM CINEMA_HALL ch
|
|---|
| 57 | JOIN CINEMA c ON c.cinema_id = ch.cinema_id
|
|---|
| 58 | JOIN SCREENING s ON s.cinemahall_id = ch.cinemahall_id
|
|---|
| 59 | JOIN TICKET t ON t.screening_id = s.screening_id
|
|---|
| 60 | GROUP BY ch.description, c.name, ch.capacity
|
|---|
| 61 | ORDER BY occupancy_percent DESC;
|
|---|
| 62 |
|
|---|
| 63 | -- 6. Најпродавани производи
|
|---|
| 64 | CREATE VIEW view_top_products AS
|
|---|
| 65 | SELECT p.name AS product,
|
|---|
| 66 | p.unit,
|
|---|
| 67 | p.price AS unit_price,
|
|---|
| 68 | SUM(pp.numbers) AS total_quantity_sold,
|
|---|
| 69 | SUM(pp.numbers * p.price) AS total_revenue
|
|---|
| 70 | FROM PRODUCT p
|
|---|
| 71 | JOIN PURCHASED_PRODUCT pp ON pp.product_id = p.product_id
|
|---|
| 72 | GROUP BY p.name, p.unit, p.price
|
|---|
| 73 | ORDER BY total_quantity_sold DESC;
|
|---|
| 74 |
|
|---|
| 75 | -- 7. Промоции vs приход
|
|---|
| 76 | CREATE VIEW view_promotion_impact AS
|
|---|
| 77 | SELECT pr.name AS promotion,
|
|---|
| 78 | pr.discount AS discount_percent,
|
|---|
| 79 | COUNT(t.ticket_id) AS total_tickets,
|
|---|
| 80 | SUM(t.price) AS revenue_with_promotion,
|
|---|
| 81 | ROUND(AVG(t.price)::numeric, 2) AS avg_ticket_price
|
|---|
| 82 | FROM PROMOTION pr
|
|---|
| 83 | JOIN SCREENING s ON s.screening_id = pr.screening_id
|
|---|
| 84 | JOIN TICKET t ON t.screening_id = s.screening_id
|
|---|
| 85 | GROUP BY pr.name, pr.discount
|
|---|
| 86 | ORDER BY total_tickets DESC;
|
|---|
| 87 |
|
|---|
| 88 | -- 8. Резервации по статус
|
|---|
| 89 | CREATE VIEW view_reservation_status AS
|
|---|
| 90 | SELECT status,
|
|---|
| 91 | COUNT(*) AS total_reservations,
|
|---|
| 92 | ROUND(COUNT(*) * 100.0 / SUM(COUNT(*)) OVER (), 2) AS percentage
|
|---|
| 93 | FROM RESERVATION
|
|---|
| 94 | GROUP BY status
|
|---|
| 95 | ORDER BY total_reservations DESC;
|
|---|
| 96 |
|
|---|
| 97 | -- 9. Популарни термини
|
|---|
| 98 | CREATE VIEW view_popular_time_slots AS
|
|---|
| 99 | SELECT s.time AS time_slot,
|
|---|
| 100 | COUNT(t.ticket_id) AS total_tickets,
|
|---|
| 101 | SUM(t.price) AS total_revenue,
|
|---|
| 102 | ROUND(AVG(t.price)::numeric, 2) AS avg_ticket_price
|
|---|
| 103 | FROM SCREENING s
|
|---|
| 104 | JOIN TICKET t ON t.screening_id = s.screening_id
|
|---|
| 105 | GROUP BY s.time
|
|---|
| 106 | ORDER BY total_tickets DESC; |
|---|