0

我是 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;
4

3 回答 3

0

您需要删除限制结果的 WHERE 条件QuoteObservations.id =1 OR QuoteObservations.id = 2

这是修改后的 SQL,删除了该条件。我还将 JOIN 条件移到了 JOIN 子句中。

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;
于 2013-08-01T17:45:12.000 回答
0
SELECT
    QuoteObservations.id, 
    QuoteObservations.value, 
    QuoteObservations.asOfTime,
    QuotePoints.quoteType
FROM
    QuoteObservations
    LEFT JOIN QuotePoints
    ON QuoteObservations.quotePointId = QuotePoints.id
WHERE
    QuotePoints.quoteType = 1 
    OR QuotePoints.quoteType = 2
ORDER BY 
    QuoteObservations.asOfTime DESC
LIMIT 1;
于 2013-08-01T17:54:19.613 回答
0

我创建了这个查询,它有效,但我敢打赌有更好、更有效的方法可以做到这一点,有人可以帮忙吗?

select q.*
from (
            select     QuoteObservations.id, QuoteObservations.value,   QuoteObservations.quotePointId, max(QuoteObservations.asOfTime) as asOfTime, QuoteObservations.dataProviderId, qp.quoteType
            from [QuoteObservations]
            inner join QuotePoints qp
            on qp.id = QuoteObservations.quotePointId
            where quotePointId = 1
            group by QuoteObservations.id, QuoteObservations.value, QuoteObservations.quotePointId, QuoteObservations.dataProviderId
    ) q
inner join (

            select     QuoteObservations.id, QuoteObservations.value, QuoteObservations.quotePointId, max(QuoteObservations.asOfTime) as asOfTime, QuoteObservations.dataProviderId, qp.quoteType
            from [QuoteObservations]
            inner join QuotePoints qp
            on qp.id = QuoteObservations.quotePointId
            where quotePointId = 2
            group by QuoteObservations.id, QuoteObservations.value, QuoteObservations.quotePointId, QuoteObservations.dataProviderId
) p
    on  q.id = p.id
inner join (
            select     QuoteObservations.id, QuoteObservations.value, QuoteObservations.quotePointId, max(QuoteObservations.asOfTime) as asOfTime, QuoteObservations.dataProviderId, qp.quoteType
            from [QuoteObservations]
            inner join QuotePoints qp
            on qp.id = QuoteObservations.quotePointId
            where quotePointId = 10
            group by QuoteObservations.id, QuoteObservations.value, QuoteObservations.quotePointId, QuoteObservations.dataProviderId
) s
    on  s.id = p.id
于 2013-08-08T16:12:48.493 回答