Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help needed updating a table 1

Status
Not open for further replies.

teach314

Technical User
Jul 29, 2011
183
0
0
CA
hello

I am asking for help writing a query (or, possibly, VBA).

Consider two tables, tblA and tblB.

Code:
   tblA   [PK: X, Y, c]                                           tblB    [PK: X, c, r]

    X         Y         c       MatchCount                         X         c        r
  -------------------------------------------                     -----------------------
    ...                                                            ...
    25        33        6           0                              24        7        3
    25        33        9           0                                
                                                                   25        2        8
    25        34        4           0                              25        4        6    
    25        34        5           0                              25        4        8    
    25        34        7           0                              25        5        6
    25        34        8           0                              25        5        8    
    25        34       10           0                              25        8        6    
                                                                   25       10        6
    25        35        1           0                              25       10        8    
    25        35        2           0                                
    25        35        8           0                              26        7        4
    25        35       10           0                              26        9        8
    ...                                                            ...

For each X, Y grouping in tblA, I want to match the two tables on X and c, then UPDATE tblA's MatchCount column to show the number
of matches. For the example shown, the tblA should end up looking like this...

Code:
   tblA   [PK: X, Y, c]  
                    
    X         Y         c       MatchCount    
  -------------------------------------------   
    ...                              
    25        33        2           1
    25        33        9           0                                
                                      
    25        34        4           2
    25        34        5           2  
    25        34        7           0
    25        34        8           1 
    25        34       10           2
                                      
    25        35        1           0
    25        35        2           1
    25        35        8           1
    25        35       10           2 
    ...

Thank you for any assistance
Teach314

 
hi,

This objective can be accomplished by a simple query, not an update query, since calculated fields such as this should never be a table field.

Code:
SELECT A.X, A.Y, A.c, Count(*) As MatchCount
FROM tblA A LEFT OUTER JOIN B tblB ON A.c = B.c
WHERE A.X = B.X
GROUP BY A.X, A.Y, A.c;


Skip,
[sub]
[glasses]Just traded in my OLD subtlety...
for a NUance![tongue][/sub]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top