UPDATE and SELECT table concatenation [duplicate]

六月ゝ 毕业季﹏ 提交于 2019-12-14 03:25:52

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!