我正在尝试从MIN
(MAX
)列获取Datetime
和InOut
时间,但两列的输出相同。
我的查询:
SELECT
CONVERT(NVARCHAR(12),MIN(Punch_history.Ecode)) as EmpCode,
CONVERT(NVARCHAR(12),MIN(EmployeeMaster.RecommandedBy)) as EmpID,
convert(date, InOut) as Report_date,
CONVERT(VARCHAR(10),(InOut),108) as InTime,
CONVERT(VARCHAR(10),(InOut),108) as OutTime,
CONVERT(NVARCHAR(12),MIN(TID)) as LOCATION
FROM Punch_history inner join EmployeeMaster on Punch_history.ECode = EmployeeMaster.Ecode
where CAST (InOut as DATE) between CAST(getdate() -1 as DATE ) and CAST(getdate() -1 as DATE ) and
EmployeeMaster.RecommandedBy like 'M0%' group by EmpID,InOut
答案 0 :(得分:2)
尝试按转换日期而不是inout进行分组。
SELECT
CONVERT(NVARCHAR(12),MIN(Punch_history.Ecode)) as EmpCode,
convert(date, InOut) as Report_date,
CONVERT(NVARCHAR(10),min(InOut),108) as InTime,
CONVERT(NVARCHAR(10),max(InOut),108) as OutTime
FROM Punch_history
where CAST (InOut as DATE) between CAST(getdate() -1 as DATE )
and CAST(getdate() -1 as DATE )
group by EmpID, convert(date, InOut)
答案 1 :(得分:0)
除了分组问题,你的where子句很奇怪。首先使用日期数学的快捷方式是一个没有胜利的情况。它会因datetime2而失败。在两个相同的值之间使用是很奇怪的。
SELECT
CONVERT(NVARCHAR(12), MIN(Punch_history.Ecode)) as EmpCode,
convert(date, MIN(InOut)) as Report_date,
CONVERT(NVARCHAR(10), min(InOut), 108) as InTime,
CONVERT(NVARCHAR(10), max(InOut), 108) as OutTime
FROM Punch_history
where convert(date, InOut) = convert(date, dateadd(day, -1, getdate()))
group by EmpID
答案 2 :(得分:0)
主要问题是你不应该按InOut(datetime)分组,这就是为什么在结果中max和min是相同的。实际上,这取消了您希望每天进行的分组。
SELECT
CONVERT(NVARCHAR(12),MIN(Punch_history.Empcode)) as EmpCode,
CONVERT(DATE , Max(InOut)) as Report_date,
CONVERT(NVARCHAR(10),min(InOut),108) as InTime,
CONVERT(NVARCHAR(10),max(InOut),108) as OutTime
FROM Punch_history
where datediff(day, CAST(InOut as DATE ) , CAST(getdate() as DATE ) )=1
group by EmpID
另外,如果empid和empcode是1-1(从逻辑上讲它们应该是) 然后你可以直接用empcode或两者进行分组 避免这种相当奇怪的Min(empcode)。