I have a task.
Basically I am selecting from table.
I want to outer apply (which means execute some code for each row) based on dynamic number coming form different table.
In Other words, I want to have dynamic number of outer apply.
Example :
SELECT C.ClientName, CS.Cnt
FROM Client C
OUTER APPLY
(
SELECT count(*) AS Cnt from ClientStatementDetail
where A= 1 AND B = 2
AND ClientdId = C.CLientID
) AS CS
-- I want to add another Oute Apply with diffrent condition.
OUTER APPLY
(
SELECT count(*) AS Cnt2 from ClientStatementDetail
where A= 3 AND B = 9
AND ClientdId = C.CLientID
) AS CS2
-- Now my problem that I do not how many combination Of A and B I have.
-- They are coming form diffrent table
Onthing came to my mind is to create a table value function that will return for me a table for all the rows
for that client then select the result. However in order to do so, we have to crate structure table with dayanmic columns.
In Other words, I was trying to implement this :
Dynamically add 50 columns to table variable
But structure tables can not be accessed in table function.
Any Help is appericated.