CSDL Otck
CSDL Otck
Thông tin
Họ và tên Đào Ngọc Tân
MSSV 23021690
Tuần 10
Bài làm
Bài 1
SELECT
line.productLine productLine,
line.textDescription textDescription,
line.htmlDescription htmlDescription,
line.image image,
coalesce(no.TienHangTon, 0) TienHangTon
FROM
(SELECT
productLine,
SUM(quantityInStock * buyPrice) TienHangTon
FROM products
GROUP BY productline) no
RIGHT JOIN
productlines line
USING (productline);
Bài 2
(SELECT
pl.productLine productLine,
pl.textDescription textDescription,
SUM(o.quantityOrdered * o.priceEach) AS totalSold,
'Worst buy' AS note
FROM
products p
LEFT JOIN orderdetails o
ON p.productCode = o.productCode
LEFT JOIN productlines pl
ON p.productLine = pl.productLine
GROUP BY pl.productLine, pl.textDescription
ORDER BY totalSold ASC
LIMIT 1)
UNION ALL
(SELECT
pl.productLine productLine,
pl.textDescription textDescription,
SUM(o.quantityOrdered * o.priceEach) AS totalSold,
'Best buy' AS note
FROM
products p
LEFT JOIN orderdetails o
ON p.productCode = o.productCode
LEFT JOIN productlines pl
ON p.productLine = pl.productLine
GROUP BY pl.productLine, pl.textDescription
ORDER BY totalSold DESC
LIMIT 1)
Bài 3
SELECT
r.customerNumber,
r.customerName,
(r.TongTienHang - r.TongThanhToan) TienNo
FROM
(
SELECT
c.*,
( SELECT SUM(amount)
FROM payments p
WHERE p.customerNumber = c.customerNumber
) AS TongThanhToan,
Bài 4
SELECT
productCode,
SUM(quantityOrdered) SoLuongHoanKho
FROM orderdetails
WHERE orderNumber in
( SELECT
orderNumber
FROM orders
WHERE status = 'Cancelled'
)
GROUP BY productCode
Bài 5
SET SQL_SAFE_UPDATES = 0;
UPDATE orders
SET comments = 'first order'
WHERE orderDate LIKE CONCAT((SELECT left(MIN(orderDate), 7) FROM
orders), '%');
SET SQL_SAFE_UPDATES = 1;