如何在查询中添加 order by Sum 函数?
how to add the order by Sum function in a query?
我只有这个查询,我想根据 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
您只需将 SUM
函数添加到 ORDER BY
语句即可:
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
只需添加一个 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
我只有这个查询,我想根据 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
您只需将 SUM
函数添加到 ORDER BY
语句即可:
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
只需添加一个 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