Home Forums SQL Server 2008 T-SQL (SS2K8) Matching 2 separate UNPIVOT statements with Null Values RE: Matching 2 separate UNPIVOT statements with Null Values

  • It makes sense, but I found no relation to the question.

    The APPLY operator is similar to a JOIN but with additional capabilities. It allows you to reference columns from tables previously used in the FROM clause.

    Now, let's compare the code.

    SELECT at.ID,

    u.Step,

    u.CompletedDate,

    u.FollowupDate

    FROM #Activity_Tracking at

    CROSS APPLY( VALUES('Step1', Step1_C, Step1_F),

    ('Step2', Step2_C, Step2_F),

    ('Step3', Step3_C, Step3_F),

    ('Step4', Step4_C, Step4_F))u(Step,CompletedDate,FollowupDate);

    SELECT A.Id, B.Name, C.State, D.Step, D.FollowupDate, D.CompletedDate

    from

    A left outer join on A.ID = B.ID

    Inner Join C on A.ID = C.ID

    at Cross Apply (Values

    ('step1', step1_F, Step1_C)

    ('step2', step2_F, Step2_C)

    ('step3', step3_F, Step3_C)

    ('step4', step4_F, Step4_C)) as D(Step,FollowupDate, CompletedDate)

    Can you spot the errors and differences? Maybe if we format it the same way, the 3 errors will become easier to spot.

    SELECT A.Id,

    B.Name,

    C.State,

    D.Step,

    D.FollowupDate,

    D.CompletedDate

    FROM A

    LEFT OUTER JOIN ON A.ID = B.ID

    INNER JOIN C ON A.ID = C.ID at

    CROSS APPLY (VALUES ('step1', step1_F, Step1_C)

    ('step2', step2_F, Step2_C)

    ('step3', step3_F, Step3_C)

    ('step4', step4_F, Step4_C)) as D(Step,FollowupDate, CompletedDate)

    Remember, you need to understand the code that you're using, that's why I'm not giving you the full answer and want you to find it.

    Luis C.
    General Disclaimer:
    Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

    How to post data/code on a forum to get the best help: Option 1 / Option 2