In the query below I'm trying to select data but need it to be distinct on the columns Book.Title and the OrderDate. I have tried using DISTINCT ON but also get multiple results back which I will also post below. Sorry if the question is a bit trivial. I'm currently in the process of learning SQL so I'm a bit of a noob when it comes to what is probably something really obvious I have missed.
How could I change the query to get the results to be formatted as so:
month | title | quantity | total_value
-----------+---------------------------------------------+----------+-------------
February | Internet and World Wide Web: How to Program | 15 | 899.70
March | C How To Program | 4 | 183.92
March | Core Servlets and JavaServer Pages | 13 | 856.70
March | Internet and World Wide Web: How to Program | 21 | 1071.58
The query I constructed:
SELECT DISTINCT ON (OrderDate, Book.Title) Book.Title, to_char(OrderDate, 'Month') AS "Order Date",
OrderLine.Quantity AS "Order Quantity", (SUM(Quantity*UnitSellingPrice)) AS "Total Value"
FROM Book
INNER JOIN Publisher
ON Book.PublisherID=Publisher.PublisherID
INNER JOIN OrderLine
ON Book.BookID=OrderLine.BookID
INNER JOIN ShopOrder
ON OrderLine.ShopOrderID=ShopOrder.ShopOrderID
WHERE Publisher.Name='Prentice Hall'
GROUP BY book.title, OrderDate, orderline.quantity
ORDER BY OrderDate ASC;
The results I get:
title | Order Date | Order Quantity | Total Value
---------------------------------------------+------------+----------------+-------------
Internet and World Wide Web: How to Program | February | 10 | 299.90
Internet and World Wide Web: How to Program | February | 5 | 149.95
C How To Program | March | 3 | 68.97
Core Servlets and JavaServer Pages | March | 10 | 329.50
Internet and World Wide Web: How to Program | March | 20 | 519.80
C How To Program | March | 1 | 22.99
Core Servlets and JavaServer Pages | March | 3 | 98.85
Internet and World Wide Web: How to Program | March | 1 | 15.99
Thanks so much for any help!