问题
My task is: Choose all profitable flights. transportation is a general information about the flight. ticket includes information about a number of tickets and their cost. So, how to concatenate UPDATE and SELECT table in order to find only those rows with profit greater than 0.UPDATE table should go first because profit field is empty
UPDATE transportations
set profit =((transportations.ticket.cost*
(transportations.ticket.sold_q+transportations.ticket.booked_q))-(0.2*
transportations.plan_oil))
WHERE profit is null
SELECT profit FROM transportations
WHERE profit >0
ORDER BY profit
回答1:
Saving calculated data is usually not advisable. And you appear to be using a multi-value field. How can you save each ticket profit calc? Saving profit for each flight record would require aggregating the ticket calcs. Saving aggregate data with an UPDATE will result in a 'non-updatable' error.
If you want to return each ticket's profit calc and only those greater than 0, try:
SELECT *, (ticket.cost * (ticket.sold_q + ticket.booked_q))-(0.2* plan_oil) AS Profit
FROM transportations
WHERE (ticket.cost * (ticket.sold_q + ticket.booked_q))-(0.2* plan_oil) > 0;
If you want to consider the aggregate profit for each flight, try:
SELECT FlightID, Sum((ticket.cost * (ticket.sold_q + ticket.booked_q))-(0.2* plan_oil)) AS Profit
FROM transportations
GROUP BY FlightID
HAVING Sum((ticket.cost * (ticket.sold_q + ticket.booked_q))-(0.2* plan_oil)) > 0;
来源:https://stackoverflow.com/questions/51011712/update-and-select-table-concatenation