how to group by, and select from two tables, need two records for each given id

后端 未结 3 1101
暖寄归人
暖寄归人 2021-01-26 13:08

I am new to SQL query. Can you please help me with the following?

table 1 QuoteObservations:

id value quotePointId asOfTime 

table 2

3条回答
  •  终归单人心
    2021-01-26 13:55

    I craeted this query, it works, but I bet there is a better and more efficient way to do it, can anyone out there help?

    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
    

提交回复
热议问题