假设我有这张桌子
Table name: Traffic
Seq. Type Amount
1 in 10
2 out 30
3 in 50
4 out 70
我需要的是获得前一个较小和下一个较大的值。所以,如果我有 40 作为一个值,我会得到......
Table name: Traffic
Seq. Type Amount
2 out 30
3 in 50
我已经尝试过用 MYSQL 来做,并且对结果非常满意
(select * from Traffic where
Amount < 40 order by Amount desc limit 1)
union
(select * from Traffic where
Amount > 40 order by Amount desc limit 1)
问题出在我尝试将其转换为 AS400 可接受的 SQL 语句时。当我将它与联合一起使用时,在 select 语句中似乎不允许 order by 和 fetch 功能(AS400 没有限制功能,所以我们使用 fetch,或者是吗?)。我总是得到一个关键字不是预期的错误。这是我的声明;
(select seq as sequence, type as status, amount as price from Traffic where
Amount < 40 order by price asc fetch first 1 rows only)
union
(select seq as sequence, type as status, amount as price from Traffic where
Amount > 40 order by price asc fetch first 1 rows only)
谁能告诉我出了什么问题,应该怎么办?另外,如果您知道其他方法可以达到我想要的结果,请分享。