• ChrisM@Work (7/4/2012)


    dwain.c (7/4/2012)


    I just made the most awesome discovery while using cascading CROSS APPLYs so I thought I'd post it here.

    Did you know that inside the CROSS APPLY if you refer to a name that's ambiguous in the left table, that the CROSS APPLY assumes that it is from the inner table? To use the column from the left table (if ambiguous) you must then qualify it with the table alias.

    That is awesome cool and very handy to make the code concise!

    All right, everybody already knew that so I'll shut up now. :blush: Just had to tell somebody.

    Might be something worth mentioning in your article though Chris.

    Have you got an example, Dwain?

    Indeed I do! Take a look at the code I posted here: http://www.sqlservercentral.com/Forums/Topic1318149-392-10.aspx?Update=1, specifically the code I referred to as the "mother of all cascaded CROSS APPLYs" (last SQL script).

    A little explanation is in order I suppose. Stealing a chunk of that code (that won't run on its own):

    FROM BaseDistricts base

    -- Try: [6,55] Fail (no rows returned)

    -- Try: [8,25] Success (at least one row returned)

    CROSS APPLY (

    SELECT d13=d1, d14=d2, dall, [population]

    FROM dbo.NewDistricts

    WHERE n = 2 AND d1 = 8 AND d2 = 25 AND

    d1 NOT IN (base.d1,base.d2,base.d3,d4,d5,d6,d7,d8,d9,d10,d11,d12) AND

    d2 NOT IN (base.d1,base.d2,base.d3,d4,d5,d6,d7,d8,d9,d10,d11,d12)) a

    -- Try: [8,66] Fail

    -- Try: [25,55] Fail

    -- Try: [36,40] Success!

    CROSS APPLY (

    SELECT d15=d1, d16=d2, dall, [population]

    FROM dbo.NewDistricts

    WHERE n = 2 AND d1 = 36 AND d2 = 40 AND

    d1 NOT IN (base.d1,base.d2,base.d3,d4,d5,d6,d7,d8,d9,d10,d11,d12,d13,d14) AND

    d2 NOT IN (base.d1,base.d2,base.d3,d4,d5,d6,d7,d8,d9,d10,d11,d12,d13,d14)) b

    1. BaseDistricts is my base table and it contains the columns d1, d2, ... d12.

    2. Derived table a references all 12 of these fields, however only d1 and d2 need to be qualified as base. I also qualified d3 for purposes down the line.

    3. Derived table a creates a new d1 and d2 which can be referred to locally and are known as such without qualification. You only need to qualify d1 when you want to refer to the version created by base.

    4. Derived table b now references d13 and d14, which were created in derived table a also without qualification. Once again, non-qualified references to d1 and d2 refer to the d1 and d2 that come out of the NewDistricts table.

    Perhaps this should be known as a cascading, correlated CROSS APPLY!

    Hope that helps!


    My mantra: No loops! No CURSORs! No RBAR! Hoo-uh![/I]

    My thought question: Have you ever been told that your query runs too fast?

    My advice:
    INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
    The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.

    Need to UNPIVOT? Why not CROSS APPLY VALUES instead?[/url]
    Since random numbers are too important to be left to chance, let's generate some![/url]
    Learn to understand recursive CTEs by example.[/url]
    [url url=http://www.sqlservercentral.com/articles/St