我是 SQL 查询的新手。您能帮我解决以下问题吗?
表 1 报价观察:
id value quotePointId asOfTime
表 2 报价点:
id, quotoType
quoteType 可以是 LastPrice、RepoRate 等
我需要从表1中选择id和值,asOfTime,quoteType,asOfTime最高,表2中的quoteType=LastPrice(=1)或RepoRate(=2);我需要返回两条记录,一条为最后价格,一条为每个 id 的回购利率,但最后价格和回购利率都应该是最高的 asOfTime。
我有这个,但它给出了最后价格或回购利率,以 asOfTime 较高者为准。
SELECT QuoteObservations.id, QuoteObservations.value,
QuoteObservations.quotePointId, max(QuoteObservations.asOfTime) as asOfTime,
QuoteObservations.dataProviderId, QuotePoints.quoteType
FROM QuoteObservations, QuotePoints
WHERE (QuoteObservations.id =1 OR QuoteObservations.id = 2 )
AND QuoteObservations.quotePointId = QuotePoints.id
AND (QuotePoints.quoteType = 1 or QuotePoints.quoteType = 2)
group by QuoteObservations.id, QuoteObservations.value,
QuoteObservations.quotePointId,QuoteObservations.dataProviderId, QuotePoints.quoteType;