0% found this document useful (0 votes)
2 views

Câu 1

The document outlines the creation of a PostgreSQL procedure to generate data for student grades and subjects, including the creation of necessary tables. It also includes an optimized query to retrieve the top 10 students with the highest average scores and demonstrates two methods for transforming rows into columns. Additionally, it mentions the use of an index to enhance query performance and the option to use the crosstab function for advanced data manipulation.

Uploaded by

minhkhang040604
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Câu 1

The document outlines the creation of a PostgreSQL procedure to generate data for student grades and subjects, including the creation of necessary tables. It also includes an optimized query to retrieve the top 10 students with the highest average scores and demonstrates two methods for transforming rows into columns. Additionally, it mentions the use of an index to enhance query performance and the option to use the crosstab function for advanced data manipulation.

Uploaded by

minhkhang040604
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

-- Câu 1: Tạo procedure sinh dữ liệu trong PostgreSQL

Tạo bảng
CREATE TABLE mon_hoc (
mamh SERIAL PRIMARY KEY,
tenmh VARCHAR(100)
);

CREATE TABLE sinh_vien (


masv SERIAL PRIMARY KEY,
tensv VARCHAR(100)
);

CREATE TABLE diem_thi (


id SERIAL PRIMARY KEY,
masv INT REFERENCES sinh_vien(masv),
mamh INT REFERENCES mon_hoc(mamh),
diem FLOAT
);
Tạo PROCEDURE

CREATE OR REPLACE PROCEDURE sinh_du_lieu()


LANGUAGE plpgsql
AS $$
DECLARE
i INT;
BEGIN
INSERT INTO mon_hoc(tenmh) VALUES ('Toán'), ('Lý'), ('Hóa');
FOR i IN 1..5000000 LOOP
INSERT INTO sinh_vien(tensv) VALUES ('Nguyễn Văn A' || i);
END LOOP;
FOR i IN 1..5000000 LOOP
INSERT INTO diem_thi(masv, mamh, diem) VALUES
(i, 1, random() * 10),
(i, 2, random() * 10),
(i, 3, random() * 10);
END LOOP;
END;
$$;
Gọi procedure
CALL sinh_du_lieu();
Câu 2: Tối ưu hóa truy vấn lấy top 10 sinh viên có điểm trung bình cao nhất

--Tạo chỉ mục hỗ trợ


CREATE INDEX idx_diemthi_masv ON diem_thi(masv);

-- Truy vấn tối ưu


WITH DiemTB AS (
SELECT
masv,
AVG(diem) AS diemtrungbinh
FROM diem_thi
GROUP BY masv
)
SELECT
sv.masv,
sv.tensv,
d.diemtrungbinh
FROM DiemTB d
JOIN sinh_vien sv ON sv.masv = d.masv
ORDER BY d.diemtrungbinh DESC
LIMIT 10;
- Câu 3: Chuyển dòng thành cột trong PostgreSQL

-- Cách 1: Dùng CASE WHEN


SELECT
sv.masv,
sv.tensv,
MAX(CASE WHEN dt.mamh = 1 THEN dt.diem END) AS DiemMon1,
MAX(CASE WHEN dt.mamh = 2 THEN dt.diem END) AS DiemMon2,
MAX(CASE WHEN dt.mamh = 3 THEN dt.diem END) AS DiemMon3
FROM sinh_vien sv
JOIN diem_thi dt ON sv.masv = dt.masv
GROUP BY sv.masv, sv.tensv;

-- Cách 2: Dùng crosstab (nâng cao)


-- Yêu cầu extension:
-- CREATE EXTENSION IF NOT EXISTS tablefunc;

SELECT *
FROM crosstab(
$$SELECT sv.masv, mh.tenmh, dt.diem
FROM sinh_vien sv
JOIN diem_thi dt ON sv.masv = dt.masv
JOIN mon_hoc mh ON dt.mamh = mh.mamh
ORDER BY sv.masv, mh.tenmh$$
-- ) AS ct(masv INT, Toan FLOAT, Hoa FLOAT, Ly FLOAT);

You might also like