WITH CTE AS
(
SELECT *, CAST(CONCAT(CAST(ngayquet AS date), ' ', gio, ':', phut, ':', giay) AS DATETIME) AS thoigian_datetime,
RN = ROW_NUMBER() OVER(PARTITION BY manv, ngayquet ORDER BY CAST(CONCAT(CAST(ngayquet AS date), ' ', gio, ':', phut, ':', giay) AS DATETIME))
FROM dbo.tbl_nhatky_quetthe WHERE CAST(ngayquet AS DATE) BETWEEN '20181201' AND '20181220'
)
SELECT a.manv, a.thoigian_datetime AS tgbd, b.thoigian_datetime AS tgkt FROM (SELECT * FROM cte WHERE rn % 2 = 1 ) a
LEFT JOIN (SELECT * FROM cte WHERE rn % 2 = 0) b
ON a.manv = b.manv AND b.ngayquet = a.ngayquet AND b.rn-a.rn=1
WHERE a.manv = '007045'