简体   繁体   English

用于比较多个日期的关系数据库的结构

[英]Structure of a relational database for comparing multiple dates

We have a Microsoft Access Database at work to track an ongoing list of customers. 我们有一个Microsoft Access数据库在工作,以跟踪客户的持续列表。 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. 每个客户必须与多个部门(总共13个(!)部门)签订合同,在发送和接收合同时,我们要为此跟踪每个客户的当前进度。 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) 我希望能够计算出客户和部门的DEP_X_SENTDEP_X_RECEIVED之间的时间间隔(例如,客户ID 1的“部门A:2天,部门B:5天...”)
  • 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. 更重要的是,我想将一个客户的所有DEP_X_RECEIVED日期彼此进行比较:确定收到合同的第一个( MIN )和最后一个( MAX )日期,以查找每个客户直到所有合同到期需要多少天。接收。 (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). (例如,客户ID 1的“合同在6天内收到”,因为第一份是在5月3日收到的,最后一份是在5月9日收到的)。 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. 在MySQL中,我可以使用GREATESTLEAST功能来比较不同列之间的值,但是在Access中,我现在必须依靠VBA,我认为这是一种不好的做法。 How can I normalize and restructure my table for archieving my goals with rather simple MAX , MIN and AVG operations? 如何通过相当简单的MAXMINAVG操作规范化和重组表以归档目标? 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. 现在,您可以简单地执行所需的分析,并将原始布局作为数据透视表报表或LEFT OUTER JOIN从DEPARTMENT表检索到新的TABLE_1。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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