Home Forums SQL Server 2005 T-SQL (SS2K5) Is this a "gaps and islands" problem? Finding gaps in overlapping times. RE: Is this a "gaps and islands" problem? Finding gaps in overlapping times.

  • GPO (8/14/2013)


    OK. So I'm trying to understand table value constructors and cross apply. If I do this (on a SQL 2008 machine):

    SELECT *

    FROM (VALUES (0),(0),(0),(0),(0),(0),(0),(0),(0),(0)) a (n)

    CROSS JOIN (VALUES (0),(0),(0),(0),(0),(0),(0),(0),(0),(0)) b (n)

    CROSS JOIN (VALUES (0),(0),(0),(0),(0),(0),(0),(0),(0),(0)) c (n)

    CROSS JOIN (VALUES (0),(0),(0),(0),(0),(0),(0),(0),(0),(0)) d (n)

    CROSS JOIN (VALUES (0),(0),(0),(0),(0),(0),(0),(0),(0),(0)) e (n)

    ...I get 100,000 rows and 5 columns. All the columns are called n and all the values are zero. What does this do? Why do I need 5 identical columns?

    Try it like this:

    SELECT n=ROW_NUMBER() OVER (ORDER BY (SELECT $))

    FROM (VALUES (0),(0),(0),(0),(0),(0),(0),(0),(0),(0)) a (n)

    CROSS JOIN (VALUES (0),(0),(0),(0),(0),(0),(0),(0),(0),(0)) b (n)

    CROSS JOIN (VALUES (0),(0),(0),(0),(0),(0),(0),(0),(0),(0)) c (n)

    CROSS JOIN (VALUES (0),(0),(0),(0),(0),(0),(0),(0),(0),(0)) d (n)

    CROSS JOIN (VALUES (0),(0),(0),(0),(0),(0),(0),(0),(0),(0)) e (n)

    And you find it generates a 100,000 row tally table.


    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