TL;DR:
- Say I have a table
receiptwith columns (ReceiptID, ItemName, Spend). - How can I create a view that limits to a maximum of one unique
ReceiptIDrow?
Details of what I tried and it's drawbacks
// Creating the view `view_of_unique_receipts`
WITH ordered_receipts AS (
SELECT *,
ROW_NUMBER() OVER ( PARTITION BY ReceiptName) AS rn
FROM receipt_*
)
SELECT * EXCEPT (rn)
FROM ordered_receipts
WHERE rn = 1
- Doing
SELECT * FROM receiptcosts e.g. 50 GB - Doing
SELECT ReceiptName, Spend FROM receiptcosts e.g. 10 GB - Doing
SELECT ReceiptName, Spend FROM view_of_unique_receiptscosts 50GB- How can I make this only cost 10gb?
- It costs 50GB because the row_number partition always selects all the columns (even though the final query is not asking for it)