I have the following query:
select
product_name,
(select bid_price
from bids
where bid_id = current_bid_id) as final_price
from items
where close_date > '2023/01/01'
and close_date < '2023/02/01';
which outputs:
product_name final_price
ball 20
bat 30
hockey_stick 50
Instead of this, I want either:
product_name final_price
ball 20
bat 30
hockey_stick 50
total 100
or
product_name final_price total
ball 20 100
bat 30 100
hockey_stick 50 100
I’m struggling on how to do this as its an alias column
3
Answers
Try this one:
You can use: GROUP BY WITH ROLLUP
see: DBFIDDLE
results:
As a starter: presumably the price selection subquery in the
select
clause always returns at most one row (otherwise you query would error), so it could be expressed as a left join (or even as an inner join, if there always is a bid to match the item).Then: we can add easily add another column to the resultset that computes the total of all prices using window functions:
I slightly changed the date filtering, assuming that
close_date
is of a date-like datatype and that you want to filter on the entire day.