我只是有这个查询,我想通过sum函数的结果添加其记录的顺序,如何添加?
Select bus.trans_comp_id,
SUM(bus.passengers*trips.cost)
From bus inner join trips on bus.ID=trips.bus_id
group by bus.trans_comp_id
输出这个:
trans_comp_id
1) 1:412000.00
2) 2:75000.00
我想用desc命令将其输出:
trans_comp_id
2) 2:75000.00
1) 1:412000.00
答案 0 :(得分:1)
您只需将SUM
功能添加到ORDER BY
caluse:
Select bus.trans_comp_id,
SUM(bus.passengers*trips.cost)
From bus inner join trips on bus.ID=trips.bus_id
group by bus.trans_comp_id
order by SUM(bus.passengers*trips.cost) desc
答案 1 :(得分:1)
只需添加一个ORDER BY
子句,该子句使用SELECT
子句中定义的计算值的别名:
SELECT bus.trans_comp_id,
SUM(bus.passengers*trips.cost) AS s
FROM bus
INNER JOIN trips ON bus.ID=trips.bus_id
GROUP BY bus.trans_comp_id
ORDER BY s DESC