We help IT Professionals succeed at work.

Suggestions for Table Update

Anthony6890
Anthony6890 asked
on
Hi All,

I have a table that features unique rows of data.  This table is basically a list of dependents listed under an employee.  To export this into a flat file, the dependents need to be changed from being listed below the employee to being listed across the employee's record in a subsequent table.  I've already created the subsequent table that provides enough space for 12 unique dependents.  I'm trying to figure out the best way of updating the table with all the dependents that fall within the criteria of an expression.  

For Example:

Table 1
ID #     Dependent ID#
1a         2a
1a         3a
1a         4a

Table 2

1a                 2a                  3a                     4a  

I've created an update query that updates the appropriate columns for the first dependent; however, I feel like this might be a very cumbersome process of doing it, i.e. with 12 separate update queries.  Does anyone have any suggestions?

Thank you.
Comment
Watch Question

Top Expert 2016

Commented:
upload a db with the table.
Most Valuable Expert 2015
Distinguished Expert 2018
Commented:
You can create a creative crosstab query:

TRANSFORM
    First(Table1.[Dependent ID#])
SELECT
    Table1.[ID#]
FROM
    Table1
GROUP BY
    Table1.[ID#]
PIVOT
    Table1.[Dependent ID#];

/gustav
Distinguished Expert 2017
Commented:
The crosstab is the simplest solution but you will first need to assign sequence numbers to the dependents for an employee so each dependent has a number from 1 to 12.  Then when you pivot, 1 will update column 1, 2 will update 2, etc.

Select EmpID, DependentID, SeqNum = DCount("*", "tblDependents", "EmpID = " & EmpID & " AND DependentID < " & DependentID) + 1 AS SeqNum;

This will number the dependents for an employee sequentially in order by their DependentID.  Do not save the SeqNumber.  Just use this as part of the query that is used for the crosstab.