Not equal to selections and update

Ask a Question related to Coldfusion Database Access, Design and Development.

  1. #1

    Default Not equal to selections and update

    I am linking two access tables by key payroll_number. If the payroll number is
    in table 1 but not in table 2, I want to take all records from table 1 and
    append them to table 2. I would use my cf query and the sql insert command but
    what is the selection command/proper syntax to do this ?


    trojnfn Guest

  2. Similar Questions and Discussions

    1. datagrid multiple selections
      How do i go about dealing with multiple selections within a datagrid? Any tutorials or examples. Thanks Dave
    2. Multiple Selections
      I have a multiple dropdown which have multiple selection. If I select multiple value on my multiple dropdown list and submit it,how can i write...
    3. HTTPService with multiple selections?
      I have a form with a list of criteria that I need to send to the server for processing. Does anyone know how to send it with an HTTPService ? I have...
    4. fuzzy selections
      This is something I have been trying to figure out and I have to ask the experts out there for some elucidation. Broadly, the question is: how...
    5. Multiple Selections from combo box
      I have a memo field I would like to populate like a combo box. However I would like to be able to make several choices from the list to populate...
  3. #2

    Default Re: Not equal to selections and update

    Assuming that table 1 and table 2 have the same structure (same columns, data
    types, etc.)

    INSERT
    INTO table2
    SELECT *
    FROM table1 t1
    WHERE NOT EXISTS (SELECT 1
    FROM table2 t2
    WHERE t1.payroll_number = t2.payroll_number)

    Phil

    paross1 Guest

  4. #3

    Default Re: Not equal to selections and update

    Hi Phil,

    Thanks for the response.

    However, I am having a hard time making your code work, particulary the syntax
    from table1 t1 means what ? And the command where not exists (select 1.. is
    that a typoor select 1 is part of the command ?

    trojnfn Guest

  5. #4

    Default Re: Not equal to selections and update

    The t1, t2, etc. are "alias" names assigned to the tables in order to reference
    the columns by table name, especially in the sub select. (I guess that you can
    add the AS, such as in FROM table1 AS t1, etc. )

    The SELECT 1 is not a typo -- EXISTS (or NOT EXISTS) works like a boolean
    test, so it really doesn't matter what you select in your subquery, it only
    returns rows in the outer select where the inner select is true.

    If you are still having trouble, what are your table and column names?

    Phil

    paross1 Guest

  6. #5

    Default Re: Not equal to selections and update

    My two tables are identical in layout. The first table name is A60_new and the
    second table is A60_baseline. I want to append everyting from A60_new into
    A60_Baseline only if the key value baseline_num from A60_new is not already in
    A60_Baseline.
    If it is already there, I do not want to append. I hope this makes sense. What
    is the propery syntax to do this ?
    Thanks for your help.

    trojnfn Guest

  7. #6

    Default Re: Not equal to selections and update

    Did you try something like this?

    INSERT
    INTO A60_Baseline
    SELECT *
    FROM A60_new AS t1
    WHERE NOT EXISTS (SELECT 1
    FROM A60_Baseline AS t2
    WHERE t1.baseline_num = t2.baseline_num)

    Phil
    paross1 Guest

  8. #7

    Default Re: Not equal to selections and update

    Hi Phil,

    I was just about to try your method when the requirements changed slightly and
    I was wondering if your method still applies. I now have a table called
    Custodians and one called CompanyOwned. Custodians contains columns that are in
    CompanyOwned. I want to add (append) the like columns from CompanyOwned into
    the Custodians table but only if it does not already exists. In this case,
    payroll number is my key linkage in both tables. Another problem is that the
    payroll number has a contract code associated with it in the CompanyOwned
    table. The same payroll number and the same contract code can appear multilpe
    times in the CompanyOwned table but I only want to append one of the records.
    If the same payroll number has a different contract code, then I want to append
    that record also, even though the paroll number is the same, it has a different
    contract code. For example, a payroll number-contract code combination might be
    something like this :

    pay100 cd250
    pay100 cd250
    pay100 cd250
    pay100 cd530
    pay100 cd530
    pay100 cd800

    In this example, i would want to append payroll pay100 and contract cd250
    once, even though it appears 3 times, pay100 and contract 530 once, even though
    it appears 2 times, and payroll pay100 and contract cd800 ,to the Custodians
    table.

    Can this still be done with your method ? If not, can your method be modified
    to accomodate these new requirements ?

    Thanks for your help.

    trojnfn Guest

  9. #8

    Default Re: Not equal to selections and update

    correction, my key linkages are the payroll number and contract code combinations
    trojnfn Guest

  10. #9

    Default Re: Not equal to selections and update

    So, what you are saying is that you only want to insert a new row into
    Custodians from data in CompanyOwned if there are no rows in Custodians that
    have the same payroll number, and you only want to insert ONE row from
    CompanyOwned even if there are multiple rows, right? Does it matter which one
    is used from CompanyOwned? In other words, since you are selecting a possible
    one of many, which one do you want to use?

    The query below is modified to add a correlated subquery to restrict the
    selection in your outer select to one of each PayrollNumber, and it selects the
    row with the largest ContractCode, but you could very well select the smallest
    (MIN).

    INSERT
    INTO Custodians
    SELECT *
    FROM CompanyOwned AS t1
    WHERE NOT EXISTS (SELECT 1
    FROM Custodians AS t2
    WHERE t1.PayrollNumber = t2.PayrollNumber)
    AND t1.ContractCode = (SELECT MAX(t2.ContractCode)
    FROM CompanyOwned AS t2
    WHERE t1.PayrollNumber = t2.PayrollNumber)

    Does this look kind of like what you are trying to do?

    Phil

    paross1 Guest

Posting Permissions

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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139