Sorry if the question has already been asked. I couldn't think of the words for what I was trying to do but I have done some Google foo before asking.
In the code below I'm trying to UNION onto the select query a Summary line. In the first Column of the Summary row I wanted the text Summary. I'm pretty sure I have done it this way before in the past but for the life of me can't get it to work as postgres just thinks it's an invalid int.
Any help would be greatly appreciated.
CREATE OR REPLACE VIEW OrderHistory
AS SELECT OrderLine.ShopOrderID, Book.Title, OrderLine.UnitSellingPrice, OrderLine.Quantity,
SUM(Quantity*UnitSellingPrice) AS total
FROM OrderLine
INNER JOIN Book
ON OrderLine.BookID = Book.BookID
WHERE OrderLine.BookID = 2
GROUP BY OrderLine.ShopOrderID, Book.Title, OrderLine.Quantity, OrderLine.UnitSellingPrice;
SELECT * FROM OrderHistory
UNION SELECT 'Summary', NULL, NULL, SUM(Quantity), SUM(Quantity*UnitSellingPrice)
FROM OrderHistory
ORDER BY total;
If this code it terrible as it is I apologies as I'm a bit of an SQL noob trying to learn at the moment so there could be a million and one ways of doing this more efficiently.
EDIT: Sorry. Should probably also have pointed out it's this line I am having trouble with :)
UNION SELECT 'Summary', NULL, NULL, SUM(Quantity), SUM(Quantity*UnitSellingPrice)