【问题标题】:Access 2013 Complicated SUM on Query/Report访问 2013 年复杂的 SUM 查询/报告
【发布时间】:2015-06-02 14:41:33
【问题描述】:

我正在尝试在 Access 2013 中创建一个显示当前状态和日期的查询,同时忽略旧的状态记录。

我有三张桌子:

customer 
-id (pk) 
-last 
-first

status 
-id (pk) 
status 
description

status-customer 
id (pk) 
customer_id 
status_id 
status_date 
notes

我想创建一个客户列表,其中包含他们处于特定状态的天数。

考虑以下连接表:

Last        First        Customer_id         Status_date       Status
John         Smith              1               05/1/2015        A
John         Smith              1               05/10/2015       B
John         Smith              1               05/14/2015       A
John         Smith              1               05/30/2015       B 
Mary         Johnson            2               05/26/2015       A

我想要这样的东西:

                                                                Days in
 Last        First        Customer_id         Status_date       Status A
 John         Smith              1               05/1/2015        25     
 Mary         Johnson            2               05/26/2015       5

请注意,计算的天数仅为 A 和 B 之间的天数。因此,对于 John Smith,记录 1 和 2 之间的间隔被添加到记录 3 和 4 之间的间隔中。如果状态没有从 A 变为 B,则使用当前日期计算总数。

谢谢。

【问题讨论】:

    标签: sql ms-access-2013


    【解决方案1】:

    相关子选择、派生表、分组依据等。我不知道MS Access如何减去日期,所以SUM(statusB - statusA)可能需要调整。

    select Last,
           First,
           Customer_id,
           MIN(statusdateA),
           SUM(statusdateB - statusdateA)
    (
    select Last,
           First,
           Customer_id,
           Status_date as statusdateA,
           (select min(t2.Status_date) from tablename t2
            where t2.Status_date > t1.Status_date
              and t2.status = 'B') as statusdateB
    from tablename t1
    where status = 'A'
    ) dt
    group by Last, First, Customer_id
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多