Home Forums SQL Server 2008 T-SQL (SS2K8) pulling records from a table between date ranges in another table RE: pulling records from a table between date ranges in another table

  • OlyKLin (3/17/2014)


    I can't add the column b/c I need to query all records between the 1st and 2nd date of the lookup table and add the column 30 to it to represent that these records happened during the first 30 days.

    Then I need to get a 2nd set of records between the 2nd and 3rd dates and tag a column that has 60 meaning that those records were created in the last 60 days

    etc. But, they need to be all in record set.

    If I didn't need the look up table to dictate the 30 days, that would fabulous and I could just update each row based on a beginning date and calculate whether it's in a 30 day range, 60 day range or 90 day range of a start date

    Right, add the extra data point to the lookup table. Then it will just be in the second column from the lookup table.

    select MyColumns, ExtraColumnFromLookupTable

    from SomeTable

    join LookupTable on SomeConditions

    Where SomeConditions

    If that doesn't work or make sense you need to post more information. In order to help we will need a few things:

    1. Sample DDL in the form of CREATE TABLE statements

    2. Sample data in the form of INSERT INTO statements

    3. Expected results based on the sample data

    Please take a few minutes and read the first article in my signature for best practices when posting questions.

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/