如何获取今天记录的最小值(时间)和前一天记录的最大值(时间)

发布于 2024-07-26 04:37:56 字数 1857 浏览 1 评论 0原文

使用MS Access数据库

我想显示今天记录的最小值(时间)和前一天记录的最大值(时间)

PERSON ID  CARDEVENTDATE  CARDEVENTTIME
5008       20090805       080000
5008       20090805       140000
5008       20090809       180000
5008       20090809       220000
5008       20090813       090000
5008       20090813       200000
5008       20090818       110000
5008       20090818       232200
3405       20090805       080000
3405       20090805       180000
3405       20090809       070000
3405       20090809       230000
3010       20080806       090000
3010       20080806       230000
3010       20080810       100000
3010       20080810       160000

等等...

从上表中我想显示今天的最小值(时间)和前一天的最大值(时间

) day 表示不是昨天,而是特定人员 ID 的前一个卡片事件时间。

我正在使用子查询。

我正在使用下面提到的查询。

"SELECT PERSONID, CARDEVENTDATE, MIN (CARDEVENTTIME) AS INTIME, 
    MAX (CARDEVENTTIME) AS OUTTIME FROM (SELECT T_PERSON.PERSONID, 
    T_CARDEVENT.CARDEVENTDATE, T_CARDEVENT.CARDEVENTTIME 
FROM (T_TITLE INNER JOIN T_PERSON ON T_TITLE.TITLECODE = T_PERSON.TITLECODE)
    INNER JOIN T_CARDEVENT ON T_PERSON.PERSONID = T_CARDEVENT.PERSONID 
WHERE T_CARDEVENT.CARDEVENTDATE BETWEEN '" & sdate & "' AND '" & edate & "' 
ORDER BY T_PERSON.TITLECODE) GROUP BY PERSONID, CARDEVENTDATE))"

但我无法获得前一天的最大(时间)。 我想获取特定人员 ID 的今天最小(时间)和前一天最大(时间)。

前一天意味着不是昨天,而是特定人员 ID 的前一个卡片事件时间。

预期输出。

PERSON ID  CARDEVENTDATE  MIN TIME  CARDEVENTDATE  MAX TIME
5008       20090818       110000    20090813       200000
5008       20090813       090000    20090809       220000
5008       20090809       180000    20090805       140000
3405       20090809       070000    20090805       180000
3010       20080810       100000    20080806       230000

等等...

需要查询帮助。

Using MS Access database

I want to display today record min (time) and previous day record max (time)

Table

PERSON ID  CARDEVENTDATE  CARDEVENTTIME
5008       20090805       080000
5008       20090805       140000
5008       20090809       180000
5008       20090809       220000
5008       20090813       090000
5008       20090813       200000
5008       20090818       110000
5008       20090818       232200
3405       20090805       080000
3405       20090805       180000
3405       20090809       070000
3405       20090809       230000
3010       20080806       090000
3010       20080806       230000
3010       20080810       100000
3010       20080810       160000

and so on...

From the above table I want to display Today min (time) and previous day max (time)

Previous day means not yesterday, previous cardeventtime for the particular person id.

Am using sub queries.

I am using below mentioned query.

"SELECT PERSONID, CARDEVENTDATE, MIN (CARDEVENTTIME) AS INTIME, 
    MAX (CARDEVENTTIME) AS OUTTIME FROM (SELECT T_PERSON.PERSONID, 
    T_CARDEVENT.CARDEVENTDATE, T_CARDEVENT.CARDEVENTTIME 
FROM (T_TITLE INNER JOIN T_PERSON ON T_TITLE.TITLECODE = T_PERSON.TITLECODE)
    INNER JOIN T_CARDEVENT ON T_PERSON.PERSONID = T_CARDEVENT.PERSONID 
WHERE T_CARDEVENT.CARDEVENTDATE BETWEEN '" & sdate & "' AND '" & edate & "' 
ORDER BY T_PERSON.TITLECODE) GROUP BY PERSONID, CARDEVENTDATE))"

But am not able to get previous day max (time). I want to get Today min (time) and previous day Max (time) for the particular person id.

Previous day means not yesterday, previous cardeventtime for the particular person id.

Expected Output.

PERSON ID  CARDEVENTDATE  MIN TIME  CARDEVENTDATE  MAX TIME
5008       20090818       110000    20090813       200000
5008       20090813       090000    20090809       220000
5008       20090809       180000    20090805       140000
3405       20090809       070000    20090805       180000
3010       20080810       100000    20080806       230000

and so on...

Need Query Help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

初懵 2024-08-02 04:37:56

废弃最后一个答案。

假设您将查询保存为 DAYMINMAX。
您需要创建另一个查询:

SELECT a.PERSONID  
     , MAX(b.CARDEVENTDATE) AS PREVDAY  
     , a.CARDEVENTDATE AS NEWDAY  
    FROM DAYMINMAX AS a INNER JOIN  
         DAYMINMAX AS b ON a.PERSONID = b.PERSONID  
                       AND a.CARDEVENTDATE > b.CARDEVENTDATE  
    GROUP BY a.PERSONID, a.CARDEVENTDATE  

该查询为您提供每个人的一个日期及其前一个日期。 我们将其命名为 DAYPREVNEW。 现在您还需要另一个查询(您想要的查询):

SELECT a.PERSONID  
     , a.PREVDAY  
     , b.OUTTIME  
     , a.NEWDAY  
     , c.INTIME  
    FROM (DAYPREVNEW AS a INNER JOIN
         DAYMINMAX AS b ON a.PERSONID = b.PERSONID AND a.PREVDAY = b.CARDEVENTDATE) INNER JOIN DAYMINMAX AS c ON a.PERSONID = c.PERSONID AND a.NEWDAY = c.CARDEVENTDATE

由于您的原始查询被多次引用以创建所需的答案,因此最好将其创建为单独的查询,而不是使用子查询。

Scrap last answer.

Let's say you saved your query as DAYMINMAX.
You'll need to create another query:

SELECT a.PERSONID  
     , MAX(b.CARDEVENTDATE) AS PREVDAY  
     , a.CARDEVENTDATE AS NEWDAY  
    FROM DAYMINMAX AS a INNER JOIN  
         DAYMINMAX AS b ON a.PERSONID = b.PERSONID  
                       AND a.CARDEVENTDATE > b.CARDEVENTDATE  
    GROUP BY a.PERSONID, a.CARDEVENTDATE  

That query gives you one date and its previous date per person. Let's name it DAYPREVNEW. Now you'll need yet another one query (the one you want):

SELECT a.PERSONID  
     , a.PREVDAY  
     , b.OUTTIME  
     , a.NEWDAY  
     , c.INTIME  
    FROM (DAYPREVNEW AS a INNER JOIN
         DAYMINMAX AS b ON a.PERSONID = b.PERSONID AND a.PREVDAY = b.CARDEVENTDATE) INNER JOIN DAYMINMAX AS c ON a.PERSONID = c.PERSONID AND a.NEWDAY = c.CARDEVENTDATE

As your original query gets referenced so many times to create the answer needed, it is best to create it as a separate query than using sub-queries.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文