问题。列出产品名称,交付数量以及在PRODUCT和ORDERLINE表格中交付的所有产品的销售价格总额和小计。按升序产品名称顺序列出。
我的代码:
SELECT
p.PRODNAME AS "PRODUCT NAME",
ol.QTYDELIVERED AS "QUANTITY DELIVERED",
ol.SALEPRICE AS "SALE PRICE" from ORDERLINE ol
INNER JOIN PRODUCT p ON
ol.PRODID = p.PRODID
ORDER BY p.PRODNAME ASC;
答案 0 :(得分:1)
你可以使用它(使用CTE
)
with subtotal as
(
SELECT p.PRODID,
p.PRODNAME AS "PRODUCT NAME",
ol.QTYDELIVERED AS "QUANTITY DELIVERED",
ol.SALEPRICE * ol.QTYDELIVERED AS "SALE PRICE"
from ORDERLINE ol
INNER JOIN PRODUCT p
ON ol.PRODID = p.PRODID
ORDER BY p.PRODNAME ASC
)
select *
from (select * from subtotal
union all
select PRODID, "PRODUCT NAME", sum("QUANTITY DELIVERED"),
sum("SALE PRICE")
from subtotal
group by PRODID, "PRODUCT NAME"
)
order by "PRODUCT NAME", PRODID, "QUANTITY DELIVERED";
您应该包括PRODID来计算组中的总数,因为PRODNAME可能不是唯一的。