简体   繁体   中英

Structure of a relational database for comparing multiple dates

We have a Microsoft Access Database at work to track an ongoing list of customers. Each customer has to sign a contract with several departments - totally 13 (!) departments - for which we want to keep track about the current progress for each customer when a contract is sent and received. This structure looks similar to something like this:

    Table 1
    -------------------------------------------------------------------------------------------------------------------
    CUSTOMER_ID | ... | DEP_A_SENT | DEP_A_RECEIVED | DEP_B_SENT | DEP_B_RECEIVED | DEP_C_SENT | DEP_C_RECEIVED | ... |
    -------------------------------------------------------------------------------------------------------------------
         1      | ... | 2015-05-01 |   2015-05-03   | 2015-05-04 |   2015-05-09   | 2015-05-01 |   2015-05-05   | ... |
         2      | ... | 2015-05-01 |   2015-05-05   | 2015-05-01 |   2015-05-03   | 2015-05-13 |       ---      | ... |
         ...  
  • I want to be able to calculate the timespan between DEP_X_SENT with DEP_X_RECEIVED for customer and department (such as "department A: 2 days, department B: 5 days..." for customer ID 1)
  • More importantly, I want to compare all the DEP_X_RECEIVED dates with each other for one customer: Determining the first ( MIN ) and the last ( MAX ) date a contract has been received to finding how many days it takes for each customer until all contracts are received. (such as "the contracts were received within 6 days" for customer ID 1, because the first was received on May 3rd. and the last on May 9th). Furthermore, I want to calculate the average timespan this took for all customers. If the contract is not received yet, the is no value in that field.

In MySQL I can work with functions such GREATEST and LEAST to compare values between different columns, but in Access I have to rely for now on VBA and I think it is considered bad practice. How can I normalize and restructure my table for archieving my goals with rather simple MAX , MIN and AVG operations? Many thanks!

Simply fold your existing table into this structure:

create table TABLE_1 (
    CUSTOMER_ID     int,
    DEPARTMENT_ID   int, -- foreign key reference to DEPARTMENT table
    SENT            date,
    RECEIVED        date
);

Now you can perform the required analysis simply, and retrieve the original layout as either a Pivot report or LEFT OUTER JOIN from the DEPARTMENT table to the new TABLE_1.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM