0

我问了同样的问题,但没有得到最终答案,所以,在这里再试一次。有两种类型的报价,回购利率或最后价格,我想从两个表中选择日期时间最高的回购利率和日期时间最高的最后价格。这是一个答案,我从最初的帖子中得到了帮助,但如果两者都存在,我需要两条记录(回购和最后价格)。任何人都可以通过更改以下查询来提供帮助:非常感谢!

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

1 回答 1

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
于 2013-09-02T22:36:33.970 回答