views:

338

answers:

1

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.

+1  A: 

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.

Wilhelm
@Wilhelm. Showing Syntax Error in Join Operator - Gopal
Gopal
Still am not get proper answer
Gopal