你好我有这样的sql脚本。我对脚本的意图是产生今天的最高价值。但是脚本产生了意想不到的结果。任何人都可以帮我查看我的代码,看看它有什么问题吗?
脚本
SELECT MAX(Value),
TIMESTAMP,
fooditem,
cashcurren
FROM farm1
WHERE r.timestamp > 1405987200
AND r.timestamp <= (1405987200 + 86400)
AND fooditem = '2'
AND cashcurren = '10'
GROUP BY timestamp, fooditem, cashcurren;
意外结果
Value Timestamp fooditem cashcurren
200 1406029354 2 10
84 1406034965 2 10
536 1406034973 2 10
70 1406035006 2 10
63 1406035025 2 10
我想要的结果
价值时间戳fooditem cashcurren
536 1406034973 2 10
基本上我希望我的Oracle SQL从时间戳1405987200返回到食品#2和cashcurrency#10的最高值到1405987200 + 86400(在这种情况下,时间戳是7/22的整天)。
答案 0 :(得分:1)
SELECT Value, TIMESTAMP, fooditem, cashcurren
FROM farm1 f
WHERE timestamp between 1405987200 and (1405987200 + 86400)
AND fooditem = '2'
AND cashcurren = '10'
where value =
(select max(x.value)
from farm1 x
where x.timestamp between 1405987200 and (1405987200 + 86400)
and x.fooditem = f.fooditem
and x.cashcurren = f.cashcurren)
使用max(value)和按时间戳分组不会导致任何聚合,也没有意义。 (每个时间戳可能只有一个)
上述查询使用子查询选择给定时间戳范围,fooditem和cashcurren的最大值,然后将该值提供给where子句中的查询。
答案 1 :(得分:0)
SELECT MAX(Value),
TIMESTAMP,
fooditem,
cashcurren
FROM farm1
WHERE r.timestamp > 1405987200
AND r.timestamp <= (1405987200 + 86400)
AND fooditem = '2'
AND cashcurren = '10'
GROUP BY timestamp, fooditem, cashcurren
order by 1 desc limit 1;
答案 2 :(得分:0)
SELECT d.Value, d.TIMESTAMP, d.fooditem, d.cashcurren
FROM (SELECT MAX(Value) AS 'Value',
TIMESTAMP,
fooditem,
cashcurren
FROM farm1
WHERE r.timestamp > 1405987200
AND r.timestamp <= (1405987200 + 86400)
AND fooditem = '2'
AND cashcurren = '10'
GROUP BY timestamp, fooditem, cashcurren) AS d
ORDER BY d.Valuse DESC limit 1;