Question

My request is a little strange and specific, please bear with me.

I have an Access 2003 database with a Comments table that has several hundred thousand lines in it. There are several fields - let's call them A, B, C, D and comment. Columns A-C form the PK of the table. Thus, you could have 5, 10, or 50 rows which have different comments, but all pertain to the A-C PK.

I have another table called CORE which contains a table core that has fields A, B, C, D (A-C PK) and comment_value_insert.

What I want to do:

In Comments, for each "set" of identical A-C rows (Field D can differ), if none of their comment values matches a certain value (let's call it critical), then look up the same row in CORE and insert into the comments table a new row with A-D and Comments.comment = core.comment_value_insert.

I'm not even sure if this is possible with SQL (perhaps VBA? Hence the tag). Any advice anyone? For further clarification please ask away.

Huge thanks in advance.

EDIT

To demonstrate exactly what I want:

If my Comments table looks like

A | B | C | D | Comment
1   2   3   b   val1
1   2   3   x   val2
1   2   3   a   val3
2   9   8   z   val4
2   9   8   a   val5
3   3   3   x   val6
END

And my CORE Table looks like

A | B | C | D | Comment_value_insert
1   2   3   u   critical1
2   9   8   t   critical2
4   8   6   x   critical3
END

Then I'd want the critical1 row from CORE to be inserted into Comments, as well as the critical2 row, but not the critical3 row.

Then the final product in the Comments table will be

A | B | C | D | Comment
1   2   3   b   val1
1   2   3   x   val2
1   2   3   a   val3
1   2   3   u   critical1
2   9   8   z   val4
2   9   8   a   val5
2   9   8   t   critical2
3   3   3   x   val6
END
Was it helpful?

Solution

Try something like this for the INSERT part. For the UPDATE part you should provide more information.

-- EDIT --

INSERT INTO Comments ( A, B, C, D, Comment )
SELECT DISTINCT C.A, C.B, C.C, C.D, C.comment_value_insert
FROM Core AS C INNER JOIN Comments as C1 ON C.A = C1.A and C.B = C1.B and C.C = C1.C
WHERE NOT EXISTS (SELECT * FROM Comments AS O WHERE O.Comment = "Critical" AND O.A = C.A AND O.B = C.B AND O.C = C.C);

Regards,

OTHER TIPS

** This version worked for me **

INSERT INTO comments  
SELECT a, 
       b, 
       c, 
       d, 
       comment_value_insert AS comment  
FROM   core  
WHERE  EXISTS (SELECT comments.*  
               FROM   comments  
               WHERE  core.c = comments.c  
                      AND core.b = comments.b  
                      AND core.a = comments.a)  
       AND NOT EXISTS (SELECT comments.*  
                       FROM   comments  
                       WHERE  core.c = comments.c  
                              AND core.b = comments.b  
                              AND core.a = comments.a  
                              AND core.comment_value_insert = comments.comment)   
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top