原始查询:
7/20/2011 I
7/20/2011 O
7/19/2011 I
7/18/2011 O
7/17/2011 I
I =在O =结账时检入
然后我进行查询以分离I和O所以它会像这样返回
select
case when CHECKTYPE='I' then checktime else 0 end as 'Check in',
case when CHECKTYPE='O' then checktime else 0 end as 'Check Out',
CHECKTYPE,BadgeNo,EmpName
from
Checkinout
where
and checktime BETWEEN '07/17/2011' and '07/26/2011' and EmpName = 'ASIDOY,' and BadgeNo <> '1900-01-01 00:00:00.000'
group by
checktime,CHECKTYPE,BadgeNo,EmpName
结果
Check IN Check out Status
7/17/2011 1/1/1900 I
1/1/1900 7/18/2011 O
7/19/2011 1/1/1900 I
1/1/1900 7/20/2011 O
7/20/2011 1/1/1900 I
但我有一个问题,查询创建puts(1/1/1900),以填补所有空白,我想删除它。但我不知道如何为它编写查询,以便像这样返回。
Check In Check Out
7/17/2011 7/18/2011
7/19/2011 7/20/2011
7/20/2011 7/21/2011
答案 0 :(得分:2)
尝试以下查询
select
case when CHECKTYPE='I' then checktime end as Check in,
case when CHECKTYPE='O' then checktime end as Check Out,
CHECKTYPE,BadgeNo,EmpName
from
Checkinout
它将为空列返回NULL
答案 1 :(得分:0)
假设在“匹配”结账前总是办理登机手续,而且不知道如何处理结果第三行的遗漏结账......
您需要将要为列值提供的行连接到最终输出。它会是这样的:
SELECT
ci.checktime,
co.checktime
FROM
CheckInOut ci
inner join
CheckInOut co
on
ci.checktime < co.checktime
left join
CheckInOut co_anti
on
ci.checktime < co_anti.checktime and
co_anti.checktime < co.checktime and
co_anti.checktype = 'o'
WHERE
ci.checktype = 'i' and
co.checktype = 'o' and
co_anti.checktime is null
我们将该表连接到自身3次,以确保co
中与ci
行匹配的行是最早可以匹配的行。
此时我没有包含其他列或条件,因为我只想关注获得所需结果中显示的列所需的内容。您可以根据自己的需要调整上述内容。