我有這張桌子可以訪問
用戶資訊
| 用戶身份 | 姓名 | 部門編號 |
|---|---|---|
| 1 | JJ | 002 |
| 2 | KK | 001 |
部門
| 部門 | 部門名稱 |
|---|---|
| 001 | 測驗 |
| 002 | 排序 |
| 003 | 設計 |
簽出
| 用戶身份 | 姓名 | 檢查時間 | 檢查型別 |
|---|---|---|---|
| 1 | JJ | 2022 年 2 月 5 日上午 7 點 45 分 10 秒 | 1 |
| 1 | JJ | 2022 年 2 月 5 日晚上 18:00:10 | 0 |
| 1 | JJ | 2022 年 2 月 6 日上午 6:30:00 | 1 |
| 1 | JJ | 2022 年 2 月 6 日晚上 18:10:30 | 0 |
| 2 | KK | 2022 年 2 月 6 日上午 6:10:30 | 1 |
| 2 | KK | 2022 年 2 月 6 日晚上 18:20:30 | 0 |
但我想創建一個將顯示此表的內部聯接
| 用戶身份 | 姓名 | 部門名稱 | 在 | 出去 |
|---|---|---|---|---|
| 1 | JJ | 排序 | 2022 年 2 月 5 日上午 7 點 45 分 10 秒 | 2022 年 2 月 5 日晚上 18:00:10 |
| 1 | JJ | 排序 | 2022 年 2 月 6 日上午 6:30:00 | 2022 年 2 月 6 日晚上 18:10:30 |
| 1 | KK | 測驗 | 2022 年 2 月 6 日上午 6:10:30 | 2022 年 2 月 6 日晚上 18:20:30 |
這是我使用的 SQL,但我不知道如何將 0 作為 Out 和 1 作為 In
SELECT Userinfo.userid as ID,UserInfo.Name as Name,Dept.DeptName as Dept , Checkinout.Checktime
from ( Userinfo
inner join Checkinout on Userinfo.userid = Checkinout.userid )
inner join Dept on Userinfo.DeptId =Dept.DeptId ;
uj5u.com熱心網友回復:
從非標準化 Checkinout 開始,您可以直接獲取除 Out 之外的所有內容。然后在查詢中添加一個計算欄位,為每個 In Checktime 查找適當的 Out Checktime:


Out: (SELECT TOP 1 q.Checktime FROM [Checkinout] as q WHERE (q.userid = [Checkinout].[userid]) AND (q.Checktype = 0) AND (q.Checktime > [Checkinout].[Checktime])
ORDER BY q.Checktime ASC)
SQL:SELECT Userinfo.userid, Userinfo.Username, Dept.[Dept Name], Checkinout.Checktime AS [In], (SELECT TOP 1 q.Checktime FROM [Checkinout] as q WHERE (q.userid = [Checkinout].[userid]) AND (q.Checktype = 0) AND (q.Checktime > [Checkinout].[Checktime])
ORDER BY q.Checktime ASC) AS Out
FROM (Dept INNER JOIN Userinfo ON Dept.deptid = Userinfo.[dept id]) INNER JOIN Checkinout ON Userinfo.userid = Checkinout.userid
WHERE (((Checkinout.Checktype)=1))
'I used a nested query for the calculated field and calculated Out with the business rules that each user check-in must have a corresponding and later check-out by the same user without any check-in/check-outs between.
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/429081.html
