2013-08-06 11 views
0

我問同樣的問題,但沒有得到最終答案,所以,再試一次。 有兩種類型的報價,回購利率或最後價格,我想選擇具有最高日期時間的回購利率,以及具有來自兩個表中最高日期時間的最後價格。 這是我從第一篇文章中獲得幫助的答案,但如果兩者都存在,則需要兩個記錄(回購和最終價格)。任何人都可以請幫助改變下面的查詢:非常感謝!sql,c,選擇兩行最高的時間戳和兩列的組

SELECT QuoteObservations.id, 
     QuoteObservations.value, 
     QuoteObservations.quotePointId, 
     max(QuoteObservations.asOfTime) as asOfTime, 
     QuoteObservations.dataProviderId, 
     QuotePoints.quoteType 
FROM QuoteObservations 
INNER JOIN 
     QuotePoints 
ON  QuoteObservations.quotePointId = QuotePoints.id 
WHERE QuotePoints.quoteType in (1,2) 
group by 
     QuoteObservations.id, 
     QuoteObservations.value, 
     QuoteObservations.quotePointId, 
     QuoteObservations.dataProviderId, 
     QuotePoints.quoteType; 
+2

與其重新提出問題,也許您應該編輯原始問題? – Kermit

+0

同意上面的兩條評論。回答您之前提出的問題的評論並提供有關答案的反饋會更加恭敬。 – Andomar

+0

我編輯了問題,並回復了評論,並且4天過去了,似乎人們很忙。謝謝。 – eyu

回答

0

你要求返回兩行。以下SQL將完成您描述的內容:

SELECT TOP(1) QuoteObservations.id, 
    QuoteObservations.value, 
    QuoteObservations.quotePointId, 
    QuoteObservations.asOfTime, 
    QuoteObservations.dataProviderId, 
    QuotePoints.quoteType 
FROM QuoteObservations 
INNER JOIN QuotePoints 
ON  QuoteObservations.quotePointId = QuotePoints.id 
WHERE QuotePoints.quoteType = 1 -- Get most recent row for quoteType 1 
ORDER BY QuoteObservations.asOfTime DESC 

UNION ALL 

SELECT TOP(1) QuoteObservations.id, 
    QuoteObservations.value, 
    QuoteObservations.quotePointId, 
    QuoteObservations.asOfTime, 
    QuoteObservations.dataProviderId, 
    QuotePoints.quoteType 
FROM QuoteObservations 
INNER JOIN QuotePoints 
ON  QuoteObservations.quotePointId = QuotePoints.id 
WHERE QuotePoints.quoteType = 2 -- Get most recent row for quoteType 2 
ORDER BY QuoteObservations.asOfTime DESC