views:

37

answers:

1

Using MS Access 2003

My query.

select a.personid, a.cardeventdate as newday, a.intime as intime,
    max(b.cardeventdate) as prevday, b.outtime as outtime 
from (
    select personid, cardeventdate, min(cardeventtime) as Intime, 
        max(cardeventtime) as outtime 
    from (
        SELECT T_PERSON.cardno, T_PERSON.NAME, T_TITLE.TITLENAME, 
            T_CARDEVENT.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  
        ORDER BY T_PERSON.TITLECODE) 
    group by personid, cardeventdate
    ) as a INNER JOIN (
    select personid, cardeventdate, min(cardeventtime) as Intime, 
        max(cardeventtime) as outtime 
    from (
        SELECT T_PERSON.cardno, T_PERSON.NAME, 
            T_TITLE.TITLENAME, T_CARDEVENT.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  
        ORDER BY T_PERSON.TITLECODE) 
    group by personid, cardeventdate) as b ON a.PERSONID = b.PERSONID 
AND a.CARDEVENTDATE > b.CARDEVENTDATE     
GROUP BY a.PERSONID, a.CARDEVENTDATE, a.intime, b.outtime

Form the above query, Output is

personid    newday      intime  prevday     outtime

127         20081112    073540  20081111    073024
127         20081113    073420  20081111    073024
127         20081113    073420  20081112    122737
127         20081117    073710  20081111    073024
127         20081117    073710  20081113    073420
127         20081117    073710  20081112    122737
1496        20080819    081935  20080818    110555
1496        20080903    064211  20080819    081935
1496        20080903    064211  20080818    110555
1496        20080904    124129  20080903    064254
1496        20080904    124129  20080819    081935
1496        20080904    124129  20080818    110555
1496        20080908    134345  20080903    064254
1496        20080908    134345  20080819    081935
1496        20080908    134345  20080818    110555
1496        20080908    134345  20080904    130936
1606        20080831    132538  20080824    102744
1696        20080825    143758  20080824    182058
1696        20080831    121407  20080825    153204
1696        20080831    121407  20080824    182058
1696        20080901    110704  20080831    121407
1696        20080901    110704  20080825    153204
1696        20080901    110704  20080824    182058
1696        20080902    103342  20080831    121407

so on...

Date is displaying again and again, So Date display only once

Expected Output

personid    newday    intime    prevday    outtime

127         20081112  073540    20081111   073024
127         20081113  073420    20081112   122737
127         20081117  073710    20081113   073024

so on...

So from the above query how can I make group by.

Query Help?

A: 

Put DISTINCT in your first select

e.g.

select distinct a.personid, a.cardeventdate as newday,...
kevchadders
Thanks for the accepted answer... ps. still found it strange I got downvoted by someone for the answer!
kevchadders