パンチイン/アウトの更新された最後の行を1日選択するにはヘルプが必要です。クエリのみを選択するSQL Serverの1つの日付に対して1つの行
私はこのクエリを使用しています:
SELECT
convert(nvarchar(20), EmpID) as EmpID, ID,
DATENAME(dw, date1) AS day, date1 AS date,
PunchDateTime AS timein,
COALESCE((SELECT MAX(PunchDateTime) AS Expr1
FROM PunchData AS b
WHERE empid = '61039'
AND (EmpID = a.EmpID)
AND (PunchType = 4)
AND PunchDateTime between a.PunchDateTime and dateadd(hour, 17, a.PunchDateTime)),
(SELECT MIN(PunchDateTime) AS Expr1
FROM PunchData AS b
WHERE empid = '61039'
AND (EmpID = a.EmpID)
AND (ID >= a.ID)
AND (PunchType = 1)
AND date1 = a.date1), 0) AS timeout
FROM
dbo.PunchData AS a
WHERE
empid = '61039'
AND (PunchType = 1)
GROUP BY
EmpID, ID, DATENAME(dw, date1), date1, PunchDateTime
UNION ALL
SELECT
convert(nvarchar(20), EmpID) as EmpID, ID,
DATENAME(dw, dateadd(hour, -12, PunchDateTime)) AS day,
convert(date, COALESCE((SELECT Min(PunchDateTime) AS Expr1
FROM PunchData AS b
WHERE EmpID = '61039'
AND (EmpID = a.EmpID)
AND (PunchType = 1)
AND PunchDateTime between dateadd(hour, -17, a.PunchDateTime)
and a.PunchDateTime),
dateadd(hour, -12, PunchDateTime))) AS date,
COALESCE((SELECT Min(PunchDateTime) AS Expr1
FROM PunchData AS b
WHERE EmpID = '61039'
AND (EmpID = a.EmpID)
AND (PunchType = 1)
AND PunchDateTime between dateadd(hour, -17, a.PunchDateTime)
and a.PunchDateTime),
(SELECT Max(PunchDateTime) AS Expr1
FROM PunchData AS b
WHERE EmpID = '61039'
AND (EmpID = a.EmpID)
AND (ID = a.ID)
AND (PunchType = 4)
AND date1 = a.date1), 0) AS timein,
PunchDateTime as timeout
FROM
dbo.PunchData AS a
WHERE
(PunchType = 4)
AND empid = '61039'
GROUP BY
EmpID, ID, DATENAME(dw, date1), date1, PunchDateTime
:おそらくこのような
何か(Select、From、Convert) - 大文字小文字**を選択して**それに固執してください* * - それらのすべてを一緒に混ぜないでください! –