简体   繁体   English

MS Access:根据时间查询以显示当天的总数

[英]MS Access: Query to Show Day's Total Based on Time

I'm having a bit of difficulty in understanding a concept in MS Access Query. 我在理解MS Access查询中的概念时遇到了一些困难。 I'm not really good at queries. 我不是很擅长查询。 I just use the query builder to get my work done and thats where I'm stuck at this moment. 我只是使用查询生成器来完成我的工作,这就是我目前停留的地方。

Here's the situation: 情况如下:

I have a transactions table that has the follwoing fields 我有一个包含以下字段的交易表

[Group], [Date Time], [Receipt Amount], [other fields...] [组],[日期时间],[收据金额],[其他字段...]

Example: 例:

[ABC] [11/11/2013 20:15] [120.00] [ABC] [11/11/2013 20:15] [120.00]

[DEF] [12/11/2013 06:10] [200.00] [DEF] [12/11/2013 06:10] [200.00]

[ABC] [12/11/2013 06:20] [10.00] [ABC] [12/11/2013 06:20] [10.00]

[ABC] [12/11/2013 06:50] [400.00] [ABC] [12/11/2013 06:50] [400.00]

My reporting requirement is to show the days total amount based on dd/mm/yy 06:30 to dd/mm/yy 06:29. 我的报告要求是显示从dd / mm / yy 06:30到dd / mm / yy 06:29的天总数。

So, 11/11/2013 total should be calculated on transactions falling under 11/11/2013 06:30 till 12/11/2013 06:29. 因此,应根据2013年11月11日06:30到2013年12月11日06:29之间的交易计算2013年11月11日的总额。

I can't seem to write a query that can take a reporting date and give every group's day total based on time bounds. 我似乎无法编写一个查询,该查询可以使用报告日期并根据时限给出每个小组的一天总数。 Furthermore, each group can have different time bounds. 此外,每个组可以具有不同的时间范围。 I tried using a lookup table but can't seem to move forward. 我尝试使用查找表,但似乎无法前进。

I'll be thankful for any help. 感谢您的帮助。 I'll gladly explain more if required. 如果需要,我会很乐意解释更多。

Thanks, 谢谢,

i am not quite sure if i am getting the syntax for msaccess right, but i suggest you shift the [Date Time] back 6 hours 30 minutes and use that as your business day. 我不太确定我是否可以正确获取msaccess的语法,但是我建议您将[日期时间]后退6小时30分钟,并将其用作工作日。

select 
  [Group]
, DateAdd("h",-6,DateAdd("n",-30,[Date Time])) AS [Business Day]
, sum([Receipt Amount]) AS [Receipt Amount Sum]
from yourTable
group by 
  [Group]
, DateAdd("h",-6,DateAdd("n",-30,[Date Time]));

My suggestion would be to create a saved query in Access named [transactionsWithEffectiveDate]. 我的建议是在Access中创建一个名为[transactionsWithEffectiveDate]的保存的查询。

For [transactions] 对于[交易]

Group  Date Time            Receipt Amount
-----  -------------------  --------------
ABC    2013-11-11 20:15:00             120
DEF    2013-11-12 06:10:00             120
ABC    2013-11-12 06:20:00              10
ABC    2013-11-12 06:49:00             400
ABC    2013-11-12 06:50:00             200
ABC    2013-11-12 06:51:00             250

and [groups] 和[组]

Group  StartTime
-----  ---------
ABC    06:50:00 
DEF    05:30:00 

the query 查询

SELECT 
    Group, 
    [Date Time], 
    [Receipt Amount],
    DateAdd("d", IIf(DateDiff("s", NewDayStart, [Date Time]) < 0, -1, 0), CDate(Int(t.[Date Time]))) AS EffectiveDate
FROM
    (
        SELECT 
            t.Group, 
            t.[Date Time], 
            t.[Receipt Amount],
            CDate(Int(t.[Date Time]) + (g.StartTime - Int(g.StartTime))) AS NewDayStart
        FROM
            transactions t
            INNER JOIN
            groups g
                ON g.Group = t.Group
    )

returns 回报

Group  Date Time            Receipt Amount  EffectiveDate
-----  -------------------  --------------  -------------
ABC    2013-11-11 20:15:00             120  2013-11-11   
DEF    2013-11-12 06:10:00             120  2013-11-12   
ABC    2013-11-12 06:20:00              10  2013-11-11   
ABC    2013-11-12 06:49:00             400  2013-11-11   
ABC    2013-11-12 06:50:00             200  2013-11-12   
ABC    2013-11-12 06:51:00             250  2013-11-12   

You can then use that query as the basis for your subsequent queries and reports, eg, 然后,您可以将该查询用作后续查询和报告的基础,例如,

SELECT 
    EffectiveDate, 
    Sum([Receipt Amount]) AS [SumOfReceipt Amount]
FROM transactionsWithEffectiveDate
GROUP BY EffectiveDate;

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

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