select I.USERID,I.Name,I.CHECKTIME,I.DEFAULTDEPTID,I.DEPTNAME,I.STATE,CONVERT(CHAR(8), I.CHECKTIME, 114) AS TimeIN, CONVERT(CHAR(8), O.CHECKTIME, 114) AS TimeOUTS
from (select USERID,Name,CHECKTIME,DEFAULTDEPTID,DEPTNAME,STATE ,CONVERT(CHAR(8), CHECKTIME, 114) AS TimeIN from checkinout where CHECKTYPE='I' and CONVERT(CHAR(10), CHECKTIME, 23)='$DChk') I left join (select USERID,Name,CHECKTIME,CONVERT(CHAR(8), CHECKTIME, 114) AS TimeOUTS from checkinout where CHECKTYPE='O' and CONVERT(CHAR(10), CHECKTIME, 23)='$DChk') O on I.UserID=O.UserID