我希望将EmpID连接到EmployeeNo,并结合第二张表中的姓,名和中间名,并且我想用最小和最大将条目O和I分开,但是如果它们没有最小值或max变为空白或null我只想在特定行中空白,因为如果他们不空白行,结果将是相同的。
这是第一场
|条目............................... | recordDate ..................................... ....空.......... |参考|
| ..0016930507201907:35I .... | ..2019-05-07 00:00:00.000 ....... | ..001693 ......... | ..1693 |
| ..0016930507201917:06O。| ..2019-05-07 00:00:00.000 ........ | ..001693 ......... | ..1693 |
| ..0016930507201907:35I ..。| ..2019-05-08 00:00:00.000 ....... | ..001693 ......... | ..1693 |
............................................ | ..2019-05 -08 00:00:00.000 ....... | ..001693 ........ | ..1693 |
..... |姓氏| ..................................... |名字| ......... ........... || middleName | ................................ |员工编号| ..... | Cruz | .................................. | Kimberly | ..... ............................... |卡斯蒂略| ................................ | 001693 |
我想将两个表与第二个表结合起来,将lastname,FirstName和middleName结合在一起。 employeeNo加入Empid,但是条目将在I和O之间是分开的,具有特定empId的最小值或最大值,但是如果条目没有I或O,则它将是空白,并且在其中
|名称............................................... | EmployeeNO | ..... ....... RecordDate ............ | ... TimeIn ........... | TimeOut |
|马萨诸塞州克鲁兹加利福尼亚州金伯利.... | 001693 ........... | 2019-05-07 00:00:00.000 .. | ... 07:35 .......... .... | 05:06 |
|马萨诸塞州克鲁兹| Kimberly,CA .... | 001693 ........... | 2019-05-08 00:00:00.000 ... | ... 07:35 |
答案 0 :(得分:0)
您可以尝试一下,
SELECT CONCAT(t2.[LastName], ', ', t2.[FirstName], ', ', t2.[MiddleName]) AS [Name],
t2.[EmployeeNO], t1.[RecordDate],
MIN(IIF(ISNULL(CHARINDEX('I', t1.[Entries], 0), -1) > 0, SUBSTRING(t1.[Entries], LEN(t1.[Entries]) - 5, 6), NULL)) AS [TimeIn],
MAX(IIF(ISNULL(CHARINDEX('O', t1.[Entries], 0), -1) > 0, SUBSTRING(t1.[Entries], LEN(t1.[Entries]) - 5, 6), NULL)) AS [TimeOut]
FROM [dbo].[Table_2] t2
RIGHT OUTER JOIN [dbo].[Table_1] t1 ON t2.[EmployeeNO] = t1.[Empid]
GROUP BY CONCAT(t2.[LastName], ', ', t2.[FirstName], ', ', t2.[MiddleName]),
t2.[EmployeeNO], t1.[recordDate]
*注意:根据您的问题
-[Table_1]是第一个表
-[Table_2]是第二张桌子