我正在尝试对结果进行排序,但我的语句顺序被忽略了......我明白为什么但不知道如何解决它。这是我的代码:
SELECT
result.station_id as 'Station ID',
result.date as 'Date',
result.id as 'ID',
COALESCE(sum(if(result.parameter = '1', result.result, NULL)),'-') as 'RESULT A)',
COALESCE(sum(if(result.parameter = '2', result.result, NULL)),'-') as 'RESULT B)',
FROM (
SELECT
event.station_id,
event.date,
event.id,
e_result.result
FROM event
Inner Join e_result ON e_result.id = event.id
WHERE
(event.station_id = '0001') AND `devent`.`date` >= '1999-02-08' AND `event`.`date` <= '2011-12-20') as result
GROUP BY by result.id
ORDER BY
result.station_id ASC,
result.date DESC
它正在正确分组,但之后没有对结果进行排序......似乎正在对分组进行排序。我需要在分组完成后对整个结果集(不是每个分组)进行排序
样本输出:
Station Date ID Result A Result B
20 7/6/2009 g003 - 3
12 2/8/1999 g000 19.2 -
12 2/8/1999 g001 19.9 -
12 2/14/1999 g002 19.1 -
17 4/9/2003 i001 22.2 4
应该
Station Date ID Result A Result B
12 2/14/1999 g002 19.1 -
12 2/8/1999 g000 19.2 -
12 2/8/1999 g001 19.9 -
17 4/9/2003 i001 22.2 4
20 7/6/2009 g003 - 3
表:
e_result id, parameter, result
g002, 1, 19.1
g000, 1, 19.2
g001, 1, 19.9
i001, 1, 22.2
i001, 2, 4
g003, 2, 3
event station_id, date, id
20, 7/16/2009, g003
12, 2/8/1999, g000
12, 2/8/1999, g001
12, 2/14/1999, g002
17, 4/9/2003, i001