DBCK
DBCK
4,
SELECT
pl.productLine,
pl.textDescription,
sum(p.quantityInStock) AS totalInStock,
sum(
(select SUM(od.quantityOrdered)
FROM orderdetails od
WHERE od.productCode = p.productCode
)
) AS totalSold,
SUM(p.quantityInStock) + SUM(
(SELECT SUM(od.quantityOrdered)
FROM orderdetails od
WHERE od.productCode = p.productCode
)
) AS totalProducts
FROM
productlines pl
JOIN
products p ON p.productLine = pl.productLine
GROUP BY
pl.productLine, pl.textDescription
having
(SUM(
(SELECT sum(od.quantityOrdered)
FROM orderdetails od
WHERE od.productCode = p.productCode
)
) / nullif(sum(p.quantityInStock), 0)) >= 0.3;
5,
select customerName, (
select sum(quantityOrdered * priceEach)
from orderdetails
where orderNumber in (
select orderNumber
from orders
where customerNumber = customers.customerNumber
)
) totalBuy, (
select sum(amount)
from payments
where customerNumber = customers.customerNumber
) totalPay, (
select totalBuy - totalPay
) totalDebt, (
select totalDebt / totalBuy
) ratio
from customers
having ratio > 0.5
6,
select productName, (
select sum(quantityOrdered)
from orderdetails
where orderNumber in (
select orderNumber
from orders
where productCode = products.productCode and status = "cancelled"
)
) total
from products
having total is not null
order by total