Results 1 to 4 of 4

Thread: Need Help Programming SQL to Run Cursor, Compare, and Loop

  1. #1
    Join Date
    Feb 2007
    Posts
    2

    Need Help Programming SQL to Run Cursor, Compare, and Loop

    I need to write a program in SQL that will compare the ID field of a table and then if the ID matches will compare the dates an account was opened and when it was closed to see if a customer with multiple accounts under the same ID has overlapping accounts or if the accounts were opened and closed consecutively. Any thoughts on the best way to code this?

  2. #2
    Join Date
    Sep 2002
    Posts
    5,938
    Do you have sample table schema, data and expected results? Want to compare between tables or something else?

  3. #3
    Join Date
    Feb 2007
    Posts
    2

    Sample Schema/Data

    sample table schema:

    ID decimal 9(10,0)
    Account decimal 9(10,0)
    Open_date datetime 8
    Close_date datetime 8

    I need to return a unique number of weeks an account was open without double counting weeks where two or more accounts were open per customer. So for instance let's say you have the following scenario:

    Accounts for customer ID 1234

    Account Open_date Close_date
    1 2007-01-01 2007-02-23
    2 2007-01-08 2007-02-09
    3 2007-02-12 NULL

    The customer would have 9 open weeks as of 2007-03-03. I need to write a cursor loop that first identifies if the ID from one record to the next matches and then how many weeks should be counted for each account without double counting overlapping weeks.

  4. #4
    Join Date
    Sep 2002
    Location
    Fantasy
    Posts
    4,254
    Please provide us a table with the actual data and the expected result.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •