CREATE TABLE Product (
id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(255) NOT NULL,
price DECIMAL(10, 2) NOT NULL,
properties JSON NOT NULL
);
INSERT INTO Product (name, price, properties) VALUES
('Margherita', 8.99, JSON_OBJECT('diameter', 30, 'weight', 500, 'calories', 800)),
('Pepperoni', 9.99, JSON_OBJECT('diameter', 35, 'weight', 600, 'calories', 900)),
('Hawaiian', 10.99, JSON_OBJECT('diameter', 32, 'weight', 550, 'calories', 850));
CREATE TABLE Warehouse (
id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
product_id INT UNSIGNED,
count INT UNSIGNED DEFAULT 0,
FOREIGN KEY (product_id) REFERENCES Product(id)
);
INSERT INTO Warehouse (product_id, count) VALUES
(1, 5),
(2, 6),
(3, 7);
CREATE TABLE Client (
id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(255) NOT NULL
);
INSERT INTO Client (name) VALUES
('John Doe'),
('Jane Smith'),
('Alice Johnson');
CREATE TABLE `Order` (
id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
warehouse_id INT UNSIGNED,
dt DATETIME NOT NULL,
client_id INT UNSIGNED NOT NULL,
FOREIGN KEY (warehouse_id) REFERENCES Warehouse(id),
FOREIGN KEY (client_id) REFERENCES Client(id)
);
INSERT INTO `Order` (warehouse_id, dt, client_id) VALUES
(1, '2024-08-17 10:00:00', 1),
(2, '2024-08-17 11:00:00', 2),
(3, '2024-08-17 12:00:00', 3);
CREATE TABLE Session (
id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
client_id INT UNSIGNED,
dt_start DATETIME NOT NULL,
dt_close DATETIME NOT NULL,
FOREIGN KEY (client_id) REFERENCES Client(id)
);
INSERT INTO Session (client_id, dt_start, dt_close) VALUES
(1, '2024-08-17 09:00:00', '2024-08-17 10:00:00'),
(2, '2024-08-17 10:30:00', '2024-08-17 11:30:00'),
(3, '2024-08-17 12:00:00', '2024-08-17 13:00:00');
SELECT
P.name AS pizza_name,
JSON_EXTRACT(P.properties, '$.diameter') / 2 AS radius,
COUNT(O.id) / (SELECT COUNT(*) FROM Client) * 100 AS conversion_rate
FROM
Product P
JOIN
Warehouse W ON P.id = W.product_id
JOIN
`Order` O ON W.id = O.warehouse_id
JOIN
Client C ON O.client_id = C.id
WHERE
O.dt BETWEEN '2024-08-17 00:00:00' AND '2024-08-17 23:59:59'
GROUP BY
P.name, radius;
+------------+--------+-----------------+
| pizza_name | radius | conversion_rate |
+------------+--------+-----------------+
| Margherita | 15 | 33.3333 |
| Pepperoni | 17.5 | 33.3333 |
| Hawaiian | 16 | 33.3333 |
+------------+--------+-----------------+
3 rows in set (0.01 sec)