• In addition to the benefits of a NUMBERS table shown by Micahel, the ability to intentionally generate a Cartisian product is anoter use of the table.  I'm referring to a Cartesion product where you want X number of rows not to perform some function but to get X number of rows of data that would otherwise require X number of UNION statements.  In our company we provide custom reporting for clients.  On a few occassions I have seen some of our 'SQL Knowledge Limited' report writers create code that contains several queries UNION'd  together because they need to generate a specific number of records from a query that would normally generate just 1 record.  I don't recall the specifics, only that I improved the query's performance greatly by using a NUMBERS table approach to generate the X number of copies of a record that the prior UNION version was doing.

    On a similiar note a DATE table is another wonderful utility table that works similiar to a NUMBERS table.  I have a utility table I call DATELOOKUP that conatins 1 row for every date (MM/DD/YYYY) between a range of years that covers any time frame that a client would need to report on for now and for the next 10 years.  Each row has many columns, each column containing a piece of info specific to the date.  For example in our business it's important to use the first day of a month specified by the user.  Without the date table, a combination of Date functions (like DateAdd) have to be used to get the first day of whatever MM/DD/YYYY a user enters as criteria.  WIth the DATELOOK table I can join the data in DATELOOKUP to the date the user specifies and return the column from that record that contains the first day of said Month/Year.  IN fact the code for generating and using the DATELOOKUP table is on the SQLServerCentral site within sample scripts.

    Excellent article and information!

    Thanks for sharing!

    Ed

    Kindest Regards,

    Just say No to Facebook!