SQL 查询提取给定时间范围内的最大值?
SQL query for extract the highest value at a given time range?
我在 sql 查询中遇到问题。我需要获取最近 30 天的数据,然后仅删除 "voting" 值大于
的 4 个结果
数据库结构
Id | time | voting
1 | unix time | 3
2 | unix time | 2
3 | unix time | 4
4 | unix time | 1
5 | unix time | 6
我只想带我的数据:5-3-1-2
我试过
select a.*
from table a
inner join
( select votingng, max(time) as latest from table group by voting) v
on a.time = v.latest
and a.voting = v.voting
order by time desc limit
我想这就是你想要的:
select v.*
from voting v
where timestamp >= unix_timestamp(date_sub(curdate(), interval 1 month)
order by voting desc
limit 4;
听起来您正在尝试获得过去 30 天内的前 4 名投票结果。这些是否都能满足您的需求?
SELECT a.*
FROM table a
WHERE a.time > UNIX_TIMESTAMP(DATE_SUB(CURDATE(), INTERVAL 30 DAY))
ORDER BY a.voting DESC
LIMIT 4;
或
SELECT a.*
FROM table a
WHERE DATEDIFF( NOW(), FROM_UNIXTIME(a.time) ) <= 30
ORDER BY a.voting DESC
LIMIT 4;
希望这就是您要找的:
Select *
From
Voting
Where
time Between CURDATE() And DATE_SUB(CURDATE(),INTERVAL 30 DAY)
Order By voting Desc
Limit 4
如果您使用的是 teradata,请试试这个
SELECT * from table
qualify row_number () over(order by time desc)=1 ;
或
select * from
(select table.*, row_number () over(order by time desc) as RANK from table)
where RANK=1
我在 sql 查询中遇到问题。我需要获取最近 30 天的数据,然后仅删除 "voting" 值大于
的 4 个结果数据库结构
Id | time | voting
1 | unix time | 3
2 | unix time | 2
3 | unix time | 4
4 | unix time | 1
5 | unix time | 6
我只想带我的数据:5-3-1-2
我试过
select a.*
from table a
inner join
( select votingng, max(time) as latest from table group by voting) v
on a.time = v.latest
and a.voting = v.voting
order by time desc limit
我想这就是你想要的:
select v.*
from voting v
where timestamp >= unix_timestamp(date_sub(curdate(), interval 1 month)
order by voting desc
limit 4;
听起来您正在尝试获得过去 30 天内的前 4 名投票结果。这些是否都能满足您的需求?
SELECT a.*
FROM table a
WHERE a.time > UNIX_TIMESTAMP(DATE_SUB(CURDATE(), INTERVAL 30 DAY))
ORDER BY a.voting DESC
LIMIT 4;
或
SELECT a.*
FROM table a
WHERE DATEDIFF( NOW(), FROM_UNIXTIME(a.time) ) <= 30
ORDER BY a.voting DESC
LIMIT 4;
希望这就是您要找的:
Select *
From
Voting
Where
time Between CURDATE() And DATE_SUB(CURDATE(),INTERVAL 30 DAY)
Order By voting Desc
Limit 4
如果您使用的是 teradata,请试试这个
SELECT * from table
qualify row_number () over(order by time desc)=1 ;
或
select * from
(select table.*, row_number () over(order by time desc) as RANK from table)
where RANK=1