Creating Clever Constituencies

I've recently discovered a flaw in one of my nightly stored procedures which causes it to crash. This procedure was written to give Patron constituencies based on existing associations. The problem is that it will sometimes try to give a constituency of that particular type to someone who already had that constituency except that it's now inactive. The procedure is therefore crashing on a violation of PRIMARY KEY constraint, as it tries to add a new record of that customer_no and constituency (which together make the primary key for TX_CONST_CUST).

Bottom line, and possibly dumb question, I'm wondering if there's really any reason to keep inactive records in a TX table, since it seems that a lot of them don't do that (e.g. TX_CUST_KEYWORD). And, if so, it would seem that I could either delete all inactive records (e.g. where end_dt <= getdate()) of that constituency before I add them, or update the start_dt and end_dt for preexisting records accordingly.

Ideas?

Thanks, Tessiturians.

Parents
  • Hi, Matt:

     

    This is one I’ve played with a lot!

     

    Here’s a pattern that we follow. In the example below, a constituency of “YA” (member of our Young Artist Program) gets added to accounts that have an active affiliation to the Young Artist Program account, but only if they don’t already have the constituency. That’s the first insert statement below. The second insert adds a “former Young Artist” constituency to those who have an inactive affiliation—but only if they don’t already have that constituency. The delete statement at the top gets rid of any constituencies that have no end date and that no longer deserve the constituency. So if you want to use constituency to mark length of service, put an end date in it. Or, if you prefer, delete the end date in the constituencies for patrons who have the associations you are looking for, then add constituencies to those patrons who don’t already have them.

     

    Lucie

     

    --------------------------------

     

    DELETE TX_CONST_CUST

    WHERE constituency in (74) --Young Artist Studio-Current

      AND ISNULL(end_dt, 0) = 0

      AND customer_no IN

          (SELECT individual_customer_no

          FROM T_AFFILIATION

          WHERE group_customer_no = 299962 --FGO Young Artist Studio

            AND affiliation_type_id = 10007 --Employee

            AND inactive = 'Y')

     

    INSERT TX_CONST_CUST(customer_no, constituency)

    SELECT  distinct individual_customer_no, 74 --Young Artist Studio-Current

    FROM T_AFFILIATION

    WHERE group_customer_no = 299962 --FGO Young Artist Studio

      AND affiliation_type_id = 10007 --Employee

      AND inactive = 'N'

      AND individual_customer_no NOT IN

          (SELECT customer_no

          FROM TX_CONST_CUST

          WHERE constituency = 74)

      AND ISNULL(individual_customer_no,0)<>0

     

    INSERT tx_const_cust(customer_no, constituency)

    SELECT  distinct individual_customer_no, 75 --Young Artist Studio Alumnus/a

    FROM T_AFFILIATION

    WHERE group_customer_no = 299962 Employee

      AND inactive = 'Y'

      AND individual_customer_no NOT IN

          (SELECT customer_no

          FROM TX_CONST_CUST

          WHERE constituency = 75)

      AND ISNULL(individual_customer_no,0)<>0

     

    ______________________________
    Lucie Spieler
    IT Development and Training Manager

    FLORIDA GRAND opera

Reply
  • Hi, Matt:

     

    This is one I’ve played with a lot!

     

    Here’s a pattern that we follow. In the example below, a constituency of “YA” (member of our Young Artist Program) gets added to accounts that have an active affiliation to the Young Artist Program account, but only if they don’t already have the constituency. That’s the first insert statement below. The second insert adds a “former Young Artist” constituency to those who have an inactive affiliation—but only if they don’t already have that constituency. The delete statement at the top gets rid of any constituencies that have no end date and that no longer deserve the constituency. So if you want to use constituency to mark length of service, put an end date in it. Or, if you prefer, delete the end date in the constituencies for patrons who have the associations you are looking for, then add constituencies to those patrons who don’t already have them.

     

    Lucie

     

    --------------------------------

     

    DELETE TX_CONST_CUST

    WHERE constituency in (74) --Young Artist Studio-Current

      AND ISNULL(end_dt, 0) = 0

      AND customer_no IN

          (SELECT individual_customer_no

          FROM T_AFFILIATION

          WHERE group_customer_no = 299962 --FGO Young Artist Studio

            AND affiliation_type_id = 10007 --Employee

            AND inactive = 'Y')

     

    INSERT TX_CONST_CUST(customer_no, constituency)

    SELECT  distinct individual_customer_no, 74 --Young Artist Studio-Current

    FROM T_AFFILIATION

    WHERE group_customer_no = 299962 --FGO Young Artist Studio

      AND affiliation_type_id = 10007 --Employee

      AND inactive = 'N'

      AND individual_customer_no NOT IN

          (SELECT customer_no

          FROM TX_CONST_CUST

          WHERE constituency = 74)

      AND ISNULL(individual_customer_no,0)<>0

     

    INSERT tx_const_cust(customer_no, constituency)

    SELECT  distinct individual_customer_no, 75 --Young Artist Studio Alumnus/a

    FROM T_AFFILIATION

    WHERE group_customer_no = 299962 Employee

      AND inactive = 'Y'

      AND individual_customer_no NOT IN

          (SELECT customer_no

          FROM TX_CONST_CUST

          WHERE constituency = 75)

      AND ISNULL(individual_customer_no,0)<>0

     

    ______________________________
    Lucie Spieler
    IT Development and Training Manager

    FLORIDA GRAND opera

Children
No Data