Вам необходимо добавить столбец cast(dl.LogDate as date)
в предложение Group By.
ИСПРАВЛЕНИЕ: неверное прочтение раунда псевдонимов в подзапросе; проблема в том, что в подзапросе используется коррелированная ссылка на d.LogDate. Учитывая следующее DDL
CREATE TABLE Employees (EmployeeId INT PRIMARY KEY
, EmployeeCode INT
, EmployeeName VARCHAR(255))
CREATE TABLE DeviceLogs (UserId INT FOREIGN KEY REFERENCES dbo.Employees(EmployeeId), LogDate DATETIME)
Это работает: -
select distinct e.EmployeeId, EmployeeCode, EmployeeName, Cast(d.LogDate as date) AttDate,
PunchTimes = Stuff((Select ', ' + convert(varchar, dl.LogDate, 8)
from DeviceLogs dl WITH (NoLock)
where dl.UserId = e.EmployeeId and cast(dl.LogDate as
date) = cast(d.LogDate as date) for XML path('') ),1, 2, '')
from Employees e, DeviceLogs d
where e.EmployeeId = d.UserId
and cast(d.LogDate as date) between '1-Jun-2019' and '1-Jun-2019'
group by e.EmployeeId, EmployeeCode, EmployeeName, Cast(d.LogDate as date)
Но оригинальный запрос не: -
select distinct e.EmployeeId, EmployeeCode, EmployeeName, Cast(d.LogDate as date) AttDate,
PunchTimes = Stuff((Select ', ' + convert(varchar, d.LogDate, 8)
from DeviceLogs dl WITH (NoLock)
where dl.UserId = e.EmployeeId and cast(dl.LogDate as
date) = cast(d.LogDate as date) for XML path('') ),1, 2, '')
from Employees e, DeviceLogs d
where e.EmployeeId = d.UserId
and cast(d.LogDate as date) between '1-Jun-2019' and '1-Jun-2019'
group by e.EmployeeId, EmployeeCode, EmployeeName, Cast(d.LogDate as date)