Hi everyone. I have to write a query but couldn't figure out how I should be calculating one of the fields in my query. I would be really thankful if anyone could correct my mistake here.
// TABLES:
-- Data structure
CREATE TABLE IF NOT EXISTS player
(
player_id SERIAL PRIMARY KEY,
player_name VARCHAR(25) NOT NULL
);
CREATE TABLE IF NOT EXISTS session
(
session_id SERIAL PRIMARY KEY,
start_time TIMESTAMP,
end_time TIMESTAMP,
elapsed_time TIME
);
CREATE TABLE IF NOT EXISTS color
(
session_id INT NOT NULL,
player_id INT NOT NULL,
color VARCHAR(10) NOT NULL,
PRIMARY KEY (session_id, player_id),
FOREIGN KEY (session_id) REFERENCES session (session_id),
FOREIGN KEY (player_id) REFERENCES player (player_id)
);
CREATE TABLE IF NOT EXISTS result
(
result_id SERIAL PRIMARY KEY,
session_id INT NOT NULL,
winner_id INT NOT NULL,
FOREIGN KEY (session_id) REFERENCES session (session_id),
FOREIGN KEY (winner_id) REFERENCES player (player_id)
);
CREATE TABLE IF NOT EXISTS round
(
round_id SERIAL PRIMARY KEY,
start_time TIMESTAMP,
end_time TIMESTAMP,
elapsed_time TIME,
round_no INT NOT NULL,
session_id INT NOT NULL,
FOREIGN KEY (session_id) REFERENCES session (session_id)
);
CREATE TABLE IF NOT EXISTS turn
(
turn_id SERIAL PRIMARY KEY,
start_time TIMESTAMP,
end_time TIMESTAMP,
elapsed_time TIME,
turn_no INT, -- sequence no within the round it is associated with
round_id INT NOT NULL,
player_id INT NOT NULL,
FOREIGN KEY (round_id) REFERENCES round (round_id),
FOREIGN KEY (player_id) REFERENCES player (player_id)
);
CREATE TABLE IF NOT EXISTS movement
(
movement_id SERIAL PRIMARY KEY,
movement_type CHAR(1), -- 'P' for push, 'M' for move
start_time TIMESTAMP,
end_time TIMESTAMP,
elapsed_time TIME,
from_x INT NOT NULL,
from_y INT NOT NULL,
to_x INT NOT NULL,
to_y INT NOT NULL,
turn_id INT NOT NULL,
FOREIGN KEY (turn_id) REFERENCES turn (turn_id)
);
// INSERT STATEMENTS:
-- ChatGPT generated insert statements since normally it's inserted via java code:
-- Insert statements for Player table
INSERT INTO player (player_id, player_name) VALUES
(1, 'Player1'),
(2, 'Player2');
-- Insert statement for Session table
INSERT INTO session (session_id, start_time, end_time, elapsed_time) VALUES
(1, '2024-04-07 02:04:11.622800', '2024-04-07 02:11:26.622800', '00:07:15');
-- Insert statements for Color table
INSERT INTO color (session_id, player_id, color) VALUES
(1, 1, 'WHITE'),
(1, 2, 'BROWN');
-- Insert statement for Result table
INSERT INTO result (result_id, session_id, winner_id) VALUES
(1, 1, 1);
-- Insert statements for Round table
INSERT INTO round (round_id, start_time, end_time, elapsed_time, round_no, session_id) VALUES
(1, '2024-04-07 02:04:11.622800', '2024-04-07 02:06:11.622800', '00:02:00', 1, 1),
(2, '2024-04-07 02:06:11.622800', '2024-04-07 02:08:41.622800', '00:02:30', 2, 1),
(3, '2024-04-07 02:08:41.622800', '2024-04-07 02:11:26.622800', '00:02:45', 3, 1);
-- Insert statements for Turn table
INSERT INTO turn (turn_id, start_time, end_time, elapsed_time, turn_no, round_id, player_id) VALUES
(1, '2024-04-07 02:04:11.622800', '2024-04-07 02:05:11.622800', '00:01:00', 1, 1, 1),
(2, '2024-04-07 02:05:11.622800', '2024-04-07 02:06:11.622800', '00:01:00', 2, 1, 1),
(3, '2024-04-07 02:06:11.622800', '2024-04-07 02:07:11.622800', '00:01:00', 1, 2, 2),
(4, '2024-04-07 02:07:11.622800', '2024-04-07 02:08:41.622800', '00:01:30', 2, 2, 2),
(5, '2024-04-07 02:08:41.622800', '2024-04-07 02:09:56.622800', '00:01:15', 1, 3, 1),
(6, '2024-04-07 02:09:56.622800', '2024-04-07 02:11:26.622800', '00:01:30', 2, 3, 2);
-- Insert statements for Movement table
INSERT INTO movement (movement_id, movement_type, start_time, end_time, elapsed_time, from_x, from_y, to_x, to_y, turn_id) VALUES
(1, 'M', '2024-04-07 02:04:11.622800', '2024-04-07 02:04:31.622800', '00:00:20', 1, 2, 1, 3, 1),
(2, 'M', '2024-04-07 02:04:31.622800', '2024-04-07 02:04:51.622800', '00:00:20', 2, 2, 2, 3, 1),
(3, 'P', '2024-04-07 02:04:51.622800', '2024-04-07 02:05:11.622800', '00:00:20', 3, 4, 3, 5, 1),
(4, 'M', '2024-04-07 02:05:11.622800', '2024-04-07 02:05:41.622800', '00:00:30', 2, 5, 2, 4, 2),
(5, 'P', '2024-04-07 02:05:41.622800', '2024-04-07 02:06:11.622800', '00:00:30', 1, 5, 1, 4, 2),
(6, 'M', '2024-04-07 02:06:11.622800', '2024-04-07 02:06:41.622800', '00:00:30', 1, 2, 1, 3, 3),
(7, 'P', '2024-04-07 02:06:41.622800', '2024-04-07 02:07:11.622800', '00:00:30', 2, 2, 2, 3, 3),
(8, 'M', '2024-04-07 02:07:11.622800', '2024-04-07 02:07:41.622800', '00:00:30', 1, 5, 1, 4, 4),
(9, 'M', '2024-04-07 02:07:41.622800', '2024-04-07 02:08:11.622800', '00:00:30', 2, 5, 2, 4, 4),
(10, 'P', '2024-04-07 02:08:11.622800', '2024-04-07 02:08:41.622800', '00:00:30', 3, 5, 3, 4, 4),
(11, 'P', '2024-04-07 02:08:41.622800', '2024-04-07 02:09:56.622800', '00:01:15', 3, 2, 3, 3, 5),
(12, 'M', '2024-04-07 02:09:56.622800', '2024-04-07 02:10:41.622800', '00:00:45', 2, 6, 2, 5, 6),
(13, 'P', '2024-04-07 02:10:41.622800', '2024-04-07 02:11:26.622800', '00:00:45', 1, 6, 1, 5, 6);
-- The output I get:
--game_start_time|game_end_time|game_session_time|game_result|total_number_of_turns|average_duration_per_turn|player_time_played|average_movement_time|total_number_of_movements|player_color
--2024-04-07 02:04:11.622800|2024-04-07 02:11:26.622800|00:07:15|Win|6|62.5|00:03:15|32.5|6|WHITE
-- The output I want to get (ONLY THE COLUMN 'average_duration_per_turn' is not giving the desired result):
--2024-04-07 02:04:11.622800|2024-04-07 02:11:26.622800|00:07:15|Win|6|65|00:03:15|32.5|6|WHITE
-- Since the Player1 has played for three turns in the game session with id 1, I expect to see:
-- (turn1 (1 min) + turn2 (1 min) + turn3 (1min and 15 sec) => 3 min and 15 sec / (3 [total nr of turns he played]) ->1 min 5 sec == 65 seconds, but instead I get 62.5 ...