MySql:按分组前排序

时间:2015-12-16 14:02:25

标签: mysql group-by subquery sql-order-by grouping

我想在分组之前订购子查询。问题是子查询的顺序好像我们就像查询一样。但由于子查询顺序无效。

以下是查询:

{{1}}

现在我正在尝试获得有序查询。如果我确实做到了这一点,我将毫无疑问地对我的数据进行分组。

1 个答案:

答案 0 :(得分:0)

在子查询中使用Row_Number ROW_NUMBER()OVER(ORDER BY schedule_id)

SELECT * FROM (
SELECT 
ROW_NUMBER() OVER(ORDER BY schedule_id DESC ) as RowId,
a.id,
citypair,
a.season,
airline,
class,
fare,
rbd,
season_from,
season_to,
schedule_id,
cp.departure_id,
cp.destination_id,
sch.date_time
FROM tarifftool_price_log as a
JOIN tarifftool_seasons as b ON a.seasonId = b.id
JOIN tarifftool_citypairs as cp ON citypair = cp.id
JOIN tarifftool_schedule_queue AS sch ON schedule_id = sch.id
WHERE       b.date = '12/26' AND class = 'b'  ) as qq 
GROUP BY citypair, airline