Küsimuste teemade nimekiri
CREATE TABLE Tellimus (tellimus_kood INTEGER NOT NULL,
klient_id INTEGER NOT NULL,
tellimuse_kp DATE NOT NULL,
CONSTRAINT pk_tellimus PRIMARY KEY (tellimus_kood));
CREATE TABLE Tellimuse_rida (tellimus_kood INTEGER NOT NULL,
toode_id INTEGER NOT NULL,
yhiku_hind INTEGER NOT NULL,
kogus INTEGER NOT NULL,
CONSTRAINT pk_tellimuse_rida PRIMARY KEY (tellimus_kood, toode_id),
CONSTRAINT fk_tellimuse_rida_tellimus FOREIGN KEY (tellimus_kood)
REFERENCES Tellimus (tellimus_kood) ON DELETE CASCADE);
INSERT INTO Tellimus (tellimus_kood, klient_id, tellimuse_kp)
VALUES (1, 1, '2023-10-08'),
(2, 1, '2023-10-09'),
(3, 2, '2023-10-09'),
(4, 1, '2023-09-09'),
(5, 2, '2023-09-11'),
(6, 2, '2023-09-15'),
(7, 3, '2023-09-15');
INSERT INTO Tellimuse_rida (tellimus_kood, toode_id, yhiku_hind, kogus)
VALUES (1, 1, 5, 5),
(1, 2, 3, 6),
(2, 1, 5, 4),
(2, 2, 3, 2),
(3, 3, 10, 2),
(4, 3, 10, 5),
(5, 1, 5, 1),
(5, 2, 3, 1),
(6, 5, 6, 7),
(7, 4, 5, 10);
WITH Tellimus_stats AS (SELECT extract(year FROM tellimuse_kp) AS aasta,
extract(month FROM tellimuse_kp) AS kuu,
klient_id, Sum(yhiku_hind*kogus) AS summa
FROM Tellimus INNER JOIN Tellimuse_rida USING (tellimus_kood)
GROUP BY aasta, kuu, klient_id)
SELECT aasta, kuu, klient_id, summa
FROM Tellimus_stats AS Ts
WHERE summa=(SELECT Max(summa) AS m
FROM Tellimus_stats AS Ts2
WHERE Ts.aasta=Ts2.aasta
AND Ts.kuu=Ts2.kuu);
klient_id, Sum(yhiku_hind*kogus) AS summa,
Rank() OVER (PARTITION BY extract(year FROM tellimuse_kp), extract(month FROM tellimuse_kp) ORDER BY Sum(yhiku_hind*kogus) DESC) AS asetus
FROM Tellimus_stats
WHERE asetus=1;
CREATE MATERIALIZED VIEW Tellimus_stats AS
SELECT extract(year FROM tellimuse_kp) AS aasta,
GROUP BY aasta, kuu, klient_id;
SELECT generator AS tellimus_kood,
floor(random() * (1_000 - 1 + 1) + 1)::int AS klient_id,
'2015-01-01'::date + floor(random() * (current_date - '2015-01-01' + 1) + 1)::int AS tellimuse_kp
FROM generate_series(1, 15_000) AS generator;
SELECT floor(random() * (1_000 - 1 + 1) + 1)::int AS tellimus_kood,
floor(random() * (2_000 - 1 + 1) + 1)::int AS toode_id,
floor(random() * (1_000 - 1 + 1) + 1)::int AS yhiku_hind,
floor(random() * (10_000 - 1 + 1) + 1)::int AS kogus
FROM generate_series(1, 100_000) AS generator
ON CONFLICT DO NOTHING;
DELETE FROM Tellimus WHERE NOT EXISTS (SELECT *
FROM Tellimuse_rida
WHERE Tellimus.tellimus_kood=Tellimuse_rida.tellimus_kood);
REFRESH MATERIALIZED VIEW Tellimus_stats;
ANALYZE;
EXPLAIN ANALYZE WITH Tellimus_stats AS (SELECT extract(year FROM tellimuse_kp) AS aasta,
TRUNCATE Tellimus CASCADE;
DELETE FROM Tellimus;
Hinda vastust: 1 2 3 4 5
Keskmine hinne : Pole veel hinnanguid!