I have a datetime column with several hours for the same date. I want to set the date, without repeating, in one column and the concatenated hours in another column.
The table structure is:
USERID CHECKTIME
-----------------------------
89 02/02/2021 05:06:07
89 02/02/2021 09:07:08
89 02/02/2021 12:15:20
89 03/02/2021 10:11:12
The output I'm looking for is:
DATE HOURS
------------------------------------------
02/02/2021 05:06:07, 09:07:08, 12:15:20
03/02/2021 10:11:12
I tried the following query, but, the resulting hours are the same for all the rows. I know it's because I'm specifying the values as literals, but, I couldn't find how to code the query so, the hours get concatenated for each different date:
SELECT DISTINCT
FORMAT(CHECKTIME,'dd/MM/yyyy') AS DATE,
STUFF((SELECT ', ' + FORMAT(CHECKTIME, 'HH:mm:ss')
FROM dbo.CHECKINOUT_PD
WHERE FORMAT(CHECKTIME, 'dd/MM/yyyy') = '02/02/2021'
AND USERID = 89
FOR XML PATH ('')), 1, 2, '') AS HOURS
FROM
dbo.CHECKINOUT_PD
WHERE
FORMAT(CHECKTIME, 'MM/yyyy') = '02/2021'
AND USERID = 89
I'll appreciate any help provided.
Thank you in advance!