2010-08-15 79 views
1

價格日期時間的最新的N日數據 1.0 20100815 1 1.2 20100815 2 1.3 20100815 3 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) 

是這樣嗎?這是否有效? 任何改善這個問題的建議?

如果我想只顯示一個價格與最新的時間,如何做到這一點?

非常感謝!

+0

,或者你想展示他們全部? – RedFilter 2010-08-15 20:36:40

+0

感謝您的意見,我更新了我的文章 – skydoor 2010-08-15 20:43:20

+1

任何特定的SQL? – 2010-08-15 20:47:59

回答

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 
0
Select Top 3 Sum(price), 
      date, 
      Sum(time) 
From allquotes 
Group By date 
Order By date Desc 
你想要哪個價格時有同一日期重複取勝