Help with a SELECT statement

  • It's not really clear how LineNo joins, but this gives the correct result.

    WITH Questions(Contact,[LineNo],LineNoMax,Question) AS (

    SELECT a.Contact,a.[LineNo],MIN(b.[LineNo])-1,a.Question

    FROM #Question a

    LEFT OUTER JOIN #Question b ON b.Contact=a.Contact AND b.[LineNo]>a.[LineNo]

    GROUP BY a.Contact,a.[LineNo],a.Question)

    SELECT a.Contact,a.Question,b.Answer

    FROM Questions a

    INNER JOIN #Answer b ON b.Contact=a.Contact AND b.[LineNo] BETWEEN a.[LineNo] AND COALESCE(a.LineNoMax,b.[LineNo])

    ____________________________________________________

    Deja View - The strange feeling that somewhere, sometime you've optimised this query before

    How to get the best help on a forum

    http://www.sqlservercentral.com/articles/Best+Practices/61537
  • Perfect, thanks Mark.

Viewing 2 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply