MS Access获得平均时间

时间:2017-07-22 05:22:08

标签: ms-access

我有这个表:

<style>
  div {
    color:blue;
  }
</style>
<div>abc</div>

我有这个问题:

button_log
    =======================================
    |ID | table_no | transaction_datetime |
    =======================================
    | 1 |    1     |  22/07/2017          |
    | 2 |    1     |  22/07/2017          |
    | 3 |    1     |  22/07/2017          |
    | 4 |    1     |  22/07/2017          |

service_time
   ===============================
   |ID | table_no | service_time |
   ===============================
   | 1 |    1     |  00:00:06    |
   | 2 |    1     |  00:00:05    |
   | 3 |    1     |  00:00:03    |
   | 4 |    1     |  00:00:10    |

但我有这个结果

select b.table_no, count(*) as total_call_count, sum(TimeValue(c.service_time) * 86400) as total_time from button_log b, service_time_log c where b.table_no = c.table_no and Format(b.transaction_datetime,""DD/MM/yyyy"") = '" & Date.Now.ToString("d") & "' group by b.table_no

我的预期结果是:

   ==========================================
   |table_no | total_call_count | total_time|
   ==========================================
   |   1     |       4          |    96     |

我怎样才能将每个记录的total_time总和一次?

3 个答案:

答案 0 :(得分:1)

我假设表名在定义中是错误的。您编写了service_time但在查询中使用了service_time_log。我将使用第二个。结果乘以4,因为button_log中有4行与where子句中的条件匹配。所以你需要首先区分button_log表。

select b.table_no, count(*) as total_call_count, sum(TimeValue(c.service_time) * 86400) as total_time
from (select distinct table_no, transaction_datetime from button_log) b, service_time_log c
where b.table_no = c.table_no and Format(b.transaction_datetime,""DD/MM/yyyy"") = '" & Date.Now.ToString("d") & "'
group by b.table_no

使用join的替代解决方案:

select b.table_no, count(*) as total_call_count, sum(TimeValue(c.service_time) * 86400) as total_time
from
    (select distinct table_no, transaction_datetime from button_log
    where Format(transaction_datetime,""DD/MM/yyyy"") = '" & Date.Now.ToString("d") & "') b
    inner join service_time_log c on b.table_no = c.table_no
group by b.table_no

答案 1 :(得分:0)

请勿使日期/时间处理复杂化,请尝试以下操作:

"Select
    button_log.table_no,
    Count(*) As total_call_count,
    (Select Sum(service_time_log.service_time) * 86400 
    From service_time_log 
    Where service_time_log.table_no = button_log.table_no) As total_time 
From 
    button_log
Where
    button_log.transaction_datetime = #" & DateTime.Today.ToString("yyyy'/'MM'/'dd") & "#
Group By
    button_log.table_no"

答案 2 :(得分:0)

SELECT button_log.table_no, Count(transaction_datetime) AS CountOfTransDate, Q1.TotalTime FROM (SELECT table_no, Sum(TimeValue([service_time])*86400) AS TotalTime FROM service_time_log GROUP BY table_no) AS Q1 INNER JOIN button_log ON Q1.table_no = button_log.table_no WHERE button_log.transaction_datetime=Date() GROUP BY button_log.table_no, Q1.TotalTime;

您的示例显示了国际日期格式。由于我的数据库使用Access标准(美国)格式,因此我不必操纵日期。根据您的情况调整。

但是,由于service_time_log没有日期值,因此无法按日期过滤其记录,那么TotalTime如何与button_log日期相关联?每个table_no只有一个日期吗?

您的示例在table_no 1的每个表中显示4条记录。每个table_no的每个表中是否总会有相同数量的记录?