Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A golubev/hw 5 #1508

Open
wants to merge 4 commits into
base: AGolubev/master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added hw-3/UML.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
47 changes: 47 additions & 0 deletions hw-3/ddl.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
CREATE TABLE Halls (
id_hall tinyint NOT NULL,
name varchar(255) NOT NULL,
PRIMARY KEY (id_hall)
);

CREATE TABLE Rows (
id_row smallint NOT NULL,
hall_id tinyint NOT NULL,
number tinyint NOT NULL,
seats_in_row tinyint NOT NULL,
PRIMARY KEY (id_rows),
FOREIGN KEY (hall_id) REFERENCES Halls(id_hall)
);

CREATE TABLE Seats (
id_seat smallint NOT NULL,
row_id smallint NOT NULL,
number tinyint NOT NULL,
PRIMARY KEY (id_seat),
FOREIGN KEY (row_id) REFERENCES Rows(id_row)
);

CREATE TABLE Movies (
id_movie int NOT NULL,
name varchar(255) NOT NULL,
PRIMARY KEY (id_movie)
);

CREATE TABLE Sessions (
id_session int NOT NULL,
hall_id tinyint NOT NULL,
movie_id int NOT NULL,
price int NOT NULL,
PRIMARY KEY (id_session),
FOREIGN KEY (hall_id) REFERENCES Halls(id_hall),
FOREIGN KEY (movie_id) REFERENCES Movies(id_movie)
);

CREATE TABLE Tickets (
id_ticket int NOT NULL,
session_id int NOT NULL,
seat_id smallint NOT NULL,
price int NOT NULL,
PRIMARY KEY (id_ticket),
FOREIGN KEY (session_id) REFERENCES Sessions(id_session)
);
14 changes: 14 additions & 0 deletions hw-3/select.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
SELECT
s.movie_id,
sum(t.price) AS movie_profit
FROM
tickets AS t
LEFT JOIN
sessions AS s
ON
t.session_id = s.id
GROUP BY
s.movie_id
ORDER BY
movie_tickets DESC
LIMIT 1;
85 changes: 85 additions & 0 deletions hw-4/ddl.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
CREATE TABLE tMovies (
id SERIAL PRIMARY KEY,
name varchar(140) NOT NULL UNIQUE
);

CREATE TABLE tMovieAttrs (
id SERIAL PRIMARY KEY,
type_id int NOT NULL REFERENCES tMovieAttrTypes(id),
name varchar(140) NOT NULL
);

CREATE TABLE tMovieAttrTypes (
id SERIAL PRIMARY KEY,
name varchar(50) NOT NULL UNIQUE
);

CREATE TABLE tMovieAttrValues (
id SERIAL PRIMARY KEY,
movie_id int NOT NULL REFERENCES tMovies(id),
attr_id int NOT NULL REFERENCES tMovieAttrs(id),
val_text text,
val_float float(24),
val_int int,
val_date date
);


CREATE VIEW viewMovies(id, name, attr_type, attr_name, attr_value) AS
SELECT
tMovie.id AS id,
tMovie.name AS name,
tMovieAttrTypes.name AS attr_type,
tMoviesAttrs.name AS attr_name,
COALESCE(
tMovieAttrValues.val_float::text,
tMovieAttrValues.val_int::text,
tMovieAttrValues.val_data::text,
tMovieAttrValues.val_text
) AS attr_value
FROM
tMovies
LEFT JOIN tMovieAttrValues ON tMovies.id = tMovieAttrValues.movie_id
LEFT JOIN tMovieAttrs ON tMovieAttrValues.attr_id = tMovieAttrs.id
LEFT JOIN tMovieAttrTypes ON tMovieAttrs.type_id = tMovieAttrTypes.id
ORDER BY
tMovie.id, tMovieAttrs.name;

CREATE VIEW viewMoviesTasks(movie_name, today_tasks, 20_days_tasks) AS
WITH
today_tasks AS (
SELECT
tMovieAttrValues.movie_id
tMovieAttrs.name as attr_name
FROM
tMovieAttrValues
LEFT JOIN tMovieAttrs ON tMovieAttrValues.attr_id = tMovieAttrs.id
WHERE
tMovieAttrValues.val_date = CURRENT_DATE
ORDER BY
tMovieAttrValues.movie_id DESC
),
20_days_tasks AS (
SELECT
tMovieAttrValues.movie_id
tMovieAttrs.name as attr_name
FROM
tMovieAttrValues
LEFT JOIN tMovieAttrs ON tMovieAttrValues.attr_id = tMovieAttrs.id
WHERE
tMovieAttrValues.val_date >= CURRENT_DATE
AND tMovieAttrValues.val_date <= (CURRENT_DATE + interval '20 days')
ORDER BY
tMovieAttrValues.movie_id DESC
)
SELECT
tMovies.name AS movie_name,
today_tasks.attr_name AS today_tasks,
20_days_tasks.attr_name AS 20_days_tasks
FROM
tMovies
LEFT JOIN today_tasks ON tMovies.id = today_tasks.movie_id
LEFT JOIN 20_days_tasks ON tMovies.id = 20_days_tasks.movie_id
ORDER BY
tMovies.name;

27 changes: 27 additions & 0 deletions hw-4/dml.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
INSERT INTO tMovies (name)
VALUES
('Властелин колец. Братство кольца'),
('Властелин колец. Две крепости'),
('Властелин колец. Возвращение короля');

INSERT INTO tMovieAttrTypes ('name')
VALUES
('INTEGER'),
('FLOAT'),
('TEXT'),
('DATE');

INSERT INTO tMovieAttrs (name, type_id)
VALUES
('Базовая стоимость билета', '1'),
('Рейтинг по мнению Ассоциации Киноманов','2'),
('Отзывы зрителей', '3'),
('Премьера в России', '4');

INSERT INTO tMovieAttrValues (movie_id, attr_id, val_text, val_float, val_int, val_date)
VALUES
(1, 1, null, null, 300, null),
(1, 2, null, 9.7, null, null),
(1, 3, 'Cool!', null, null, null),
(1, 4, null, null, null, '2002-02-07');

Loading