从表中选择最新的ONE日数据

时间:2010-08-15 20:34:27

标签: sql

价格日期时间     1.0 20100815 1     1.2 20100815 2     1.3 20100815 3     2 20100814 1     3.1 20100813 1     3.2 20100813 2     :     :     :

现在我想一直选择最新的3天价格,我这样使用

select price, date from allquotes where date in 
      (select date from allquotes group by date order by date desc limit 3)

这是对的吗?这有效吗? 有什么建议可以改善这个吗?

如果我想在最新时间只显示一个价格,该怎么做?

非常感谢!

2 个答案:

答案 0 :(得分:1)

这应该可以解决SQL Server上的问题:

select top 3 q.pricee, q.date, q.time
from (
    select date, max(time) as MaxTime
    from allquotes
    group by date
) qm
inner join quotes q on qm.date = q.date and qm.MaxTime = time
order by date desc

对于MySQL,请尝试:

select q.pricee, q.date, q.time
from (
    select date, max(time) as MaxTime
    from allquotes
    group by date
) qm
inner join quotes q on qm.date = q.date and qm.MaxTime = time
order by date desc
limit 3

答案 1 :(得分:0)

Select Top 3 Sum(price),
             date,
             Sum(time)
From   allquotes
Group By date
Order By date Desc