Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

Correlated inline view?

$
0
0

I have a need to correlate an inline view with the primary table in a query.

Essentially I am trying to flatten out some rows of data into columns.  I've got it working on a limited basis but it requires that I have the same where clauses in my main inline views as I have in my main query.  What I would LIKE to do is essentially the same as a correlated subquery where I link the inline view to a driving table.

If I am forced to have the same where clauses, it cannot be used as a generic solution.

What I am doing is as follows:

Select a.a,

           a.b,

          a.c,

          sub1.val1

from table_1 a

Left Outer Join (select b.col1 as val1

                          from table_1 b

==>                   where b.a = a.a

                          order by b.RunDate Desc

                          Offset 0 Rows Fetch Next 1 Rows Only) sub1 ON sub1.a = a.a

I put the arrow in above to show how I want to link my inline view to my driving table.  I actually have three fields I would use to tie the driving table with the subsidiary inline view. 

Is this possible.  Is there a better way to do this?  If I can make it generic, I'll have several inline views each with different offset values so that I flatten out the latest X rows into columns.

Thank you!

 Mark.


Viewing all articles
Browse latest Browse all 23857

Trending Articles