• Lrobinson 93181 (8/6/2013)


    I have provided the code and an example of the expected result set...so I am not sure what doesn't make sense? Maybe a step-by-step would help...it took me several conversations with the user to finally get it.

    1. Pull the records from each group with the latest c_dt

    This should return these two records:

    '11111','2010-09-06 12:00AM','25'

    '11111','2010-10-06 12:00AM','23'

    Where the C_Id is the same, but the c_rep is different.

    2. Nest the first query, to pull matching C_id's for final query, and then pull the latest "last_dt" that is earlier than the c_dt from the first set of records:

    '11111','2010-09-06 12:00AM','2010-09-04 12:00AM','25'

    '11111','2010-10-06 12:00AM','2010-10-04 12:00AM','23'

    Basically, I want to pull the last transactions for a specific group, using c_dt, then find another type of "last transaction" that occurred prior to that last transaction. I hope this helps.

    Thanks,

    I think part of the issue is that you originally defined c_ID as a primary key but all the rows in your sample data had the same value. That means it either needs to be a identity OR not the primary key. I am now thinking that it should not be the primary key?

    _______________________________________________________________

    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/