试试这个,
SELECT a.Order_NO,
a.Order_Date,
SUM(CASE WHEN c.Product_Category = 'Books' THEN b.Qty ELSE 0 END) totalBooks,
SUM(CASE WHEN c.Product_Category = 'Books' THEN b.Amount ELSE 0 END) totalBooks_Amount,
SUM(CASE WHEN c.Product_Category = 'Stationary' THEN b.Qty ELSE 0 END) totalStationary,
SUM(CASE WHEN c.Product_Category = 'Stationary' THEN b.Qty ELSE 0 END) totalStationary_Amount,
SUM(CASE WHEN c.Product_Category = 'Journals' THEN b.Qty ELSE 0 END) totalJournals,
SUM(CASE WHEN c.Product_Category = 'Journals' THEN b.Qty ELSE 0 END) totalJournals_Amount,
SUM(CASE WHEN c.Product_Category = 'Magazines' THEN b.Qty ELSE 0 END) totalMagazines,
SUM(CASE WHEN c.Product_Category = 'Magazines' THEN b.Qty ELSE 0 END) totalMagazines_Amount
FROM OrderMaster a
INNER JOIN OrderLines b
ON a.Order_ID = b.Order_ID
INNER JOIN ProductMaster c
ON b.Product_Code = c.Product_Code
GROUP BY a.Order_NO, a.Order_Date