使用MS Access 2003
我的查询。
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
形成上述查询,输出为
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
等......
日期一次又一次显示,因此日期只显示一次
预期产出
personid newday intime prevday outtime
127 20081112 073540 20081111 073024
127 20081113 073420 20081112 122737
127 20081117 073710 20081113 073024
等......
所以从上面的查询我怎样才能分组。
查询帮助?
答案 0 :(得分:0)
将DISTINCT放入您的第一个选择
e.g。
select distinct a.personid, a.cardeventdate as newday,...