Select 来自 table 的最后一个低值
Select last low value from a table
我有一个 table 这样的:
id | value | datestamp
-----------------------
1 | 89 | 2015-05-25 16:49:25
2 | 60 | 2015-05-26 16:49:25
3 | 42 | 2015-05-27 16:49:25
4 | 12 | 2015-05-28 16:49:25
5 | 3 | 2015-05-29 16:49:25
6 | 95 | 2015-05-30 16:49:25
7 | 75 | 2015-06-01 16:49:25
8 | 52 | 2015-06-02 16:49:25
9 | 31 | 2015-06-03 16:49:25
10 | 12 | 2015-06-04 16:49:25
11 | 99 | 2015-06-05 16:49:25
...
这些值是百分比,介于 100 和 0 之间,除了每个周期一次外,总是递减,但几乎从不完全是 0 或 100,只是大约。
是否有一个简单的 select 查询来获取具有最新低值的行?在本例中为 10 ID。
预先感谢您的帮助。
更新
正如 Tim Biegeleisen 所说,这是一个差距和孤岛问题。我想要 table 中的最后一行,其中前后的值都较大,但最后一行除外。
要获取最新值,请应用按日期排序,然后按值排序
Select top(1) * from table order by datestamp desc, value
select id,name from table order by id desc limit 1
它应该适合你:
SELECT TOP 1
id
, value
, datestemp
FROM yourtable yt1
INNER JOIN
(
SELECT
MIN(yt2.value) minval
, MONTH(yt2.datestamp) datemonth
FROM yourtable yt2
GROUP BY MONTH(yt2.datestamp)
) yt3 ON yt3.minval = yt1.value AND yt3.datemonth = MONTH(yt1.datestamp)
ORDER BY yt1.datestamp DESC
不知道我的 Sql 是否支持 TOP 子句。如果一个月中有一个最小值,那么您将不需要 top 子句。该查询基于考虑到您选择的最小值是最近一个月的事实。
更新:
我想你想要的是这样的:
SELECT * FROM
(
SELECT * FROM yourtable ORDER BY datestamp DESC LIMIT 2
) S
ORDER BY value LIMIT 1
你可以试试这个
select * form tbl_name order by id desc limit 1
虽然这对人类来说是一项简单的任务,但在 SQL
中很难表达
The values are percentages, between 100 and 0, always decreasing except one time per cycle
我通过自连接解决了这个问题,它应该提供最佳性能。但是,您应该在日期戳上创建(如果尚未完成)索引。
SELECT a.*
FROM tbl1 a
INNER JOIN tbl1 b ON b.datestamp > a.datestamp
WHERE b.value > a.value
GROUP BY a.id
ORDER BY a.datestamp DESC, b.datestamp DESC
LIMIT 1
更新
I want the last row in the table where the value coming both before and after are larger
如果你想确定你能做到这一点
SELECT a.*
FROM tbl1 a
INNER JOIN tbl1 b ON b.datestamp > a.datestamp
INNER JOIN tbl1 c ON c.datestamp < a.datestamp
WHERE b.value > a.value
AND c.value > a.value
GROUP BY a.id
ORDER BY a.datestamp DESC, b.datestamp DESC
LIMIT 1
但我认为没有必要。
我有一个 table 这样的:
id | value | datestamp
-----------------------
1 | 89 | 2015-05-25 16:49:25
2 | 60 | 2015-05-26 16:49:25
3 | 42 | 2015-05-27 16:49:25
4 | 12 | 2015-05-28 16:49:25
5 | 3 | 2015-05-29 16:49:25
6 | 95 | 2015-05-30 16:49:25
7 | 75 | 2015-06-01 16:49:25
8 | 52 | 2015-06-02 16:49:25
9 | 31 | 2015-06-03 16:49:25
10 | 12 | 2015-06-04 16:49:25
11 | 99 | 2015-06-05 16:49:25
...
这些值是百分比,介于 100 和 0 之间,除了每个周期一次外,总是递减,但几乎从不完全是 0 或 100,只是大约。 是否有一个简单的 select 查询来获取具有最新低值的行?在本例中为 10 ID。 预先感谢您的帮助。
更新
正如 Tim Biegeleisen 所说,这是一个差距和孤岛问题。我想要 table 中的最后一行,其中前后的值都较大,但最后一行除外。
要获取最新值,请应用按日期排序,然后按值排序
Select top(1) * from table order by datestamp desc, value
select id,name from table order by id desc limit 1
它应该适合你:
SELECT TOP 1
id
, value
, datestemp
FROM yourtable yt1
INNER JOIN
(
SELECT
MIN(yt2.value) minval
, MONTH(yt2.datestamp) datemonth
FROM yourtable yt2
GROUP BY MONTH(yt2.datestamp)
) yt3 ON yt3.minval = yt1.value AND yt3.datemonth = MONTH(yt1.datestamp)
ORDER BY yt1.datestamp DESC
不知道我的 Sql 是否支持 TOP 子句。如果一个月中有一个最小值,那么您将不需要 top 子句。该查询基于考虑到您选择的最小值是最近一个月的事实。
更新:
我想你想要的是这样的:
SELECT * FROM
(
SELECT * FROM yourtable ORDER BY datestamp DESC LIMIT 2
) S
ORDER BY value LIMIT 1
你可以试试这个
select * form tbl_name order by id desc limit 1
虽然这对人类来说是一项简单的任务,但在 SQL
中很难表达The values are percentages, between 100 and 0, always decreasing except one time per cycle
我通过自连接解决了这个问题,它应该提供最佳性能。但是,您应该在日期戳上创建(如果尚未完成)索引。
SELECT a.*
FROM tbl1 a
INNER JOIN tbl1 b ON b.datestamp > a.datestamp
WHERE b.value > a.value
GROUP BY a.id
ORDER BY a.datestamp DESC, b.datestamp DESC
LIMIT 1
更新
I want the last row in the table where the value coming both before and after are larger
如果你想确定你能做到这一点
SELECT a.*
FROM tbl1 a
INNER JOIN tbl1 b ON b.datestamp > a.datestamp
INNER JOIN tbl1 c ON c.datestamp < a.datestamp
WHERE b.value > a.value
AND c.value > a.value
GROUP BY a.id
ORDER BY a.datestamp DESC, b.datestamp DESC
LIMIT 1
但我认为没有必要。