开发者

Select the lastest N day data from table

开发者 https://www.devze.com 2023-01-11 10:55 出处:网络
price datetime 1.0201008151 1.2201008152 1.3201008153 2201008141 3.1201008131 开发者_开发问答3.2201008132

price date time 1.0 20100815 1 1.2 20100815 2 1.3 20100815 3 2 20100814 1 3.1 20100813 1 开发者_开发问答 3.2 20100813 2 : : :

Now I want to select the latest 3 days price with all the time, I use like this

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

Is this right? Is this efficient? Any suggestion to improve this?

If I would like to show only one price with the latest time, how to do that?

Thanks so much!


This should do the trick on 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

For MySQL, try:

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


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

精彩评论

暂无评论...
验证码 换一张
取 消