我有以下数据:
emp_no emp_name login level HOD_NAME Assigend_IP LoggedIn_IP Attendece_Location Shift_Timing
E31446 Amit Singh 09:39 Direct P00212 172.29.23.53 172.29.23.53 CSO-Ackruti Star-6F 09:30-18:30
E31446 Amit Singh 09:39 Direct P00212 172.29.23.53 172.29.23.53 CSO-Ackruti Star-6F 09:30-18:30
E39787 Anita Haridas Shenoy 09:41 Indirect E31446 172.29.24.38 172.29.23.55 CSO-Ackruti Star-6F 09:30-18:30
E39787 Anita Haridas Shenoy 09:41 Indirect E31446 172.29.24.38 172.29.24.38 CSO-Ackruti Star-6F 09:30-18:30
E39787 Anita Haridas Shenoy 09:41 Indirect E31446 172.29.24.38 172.29.23.55 CSO-Ackruti Star-6F 09:30-18:30
E39787 Anita Haridas Shenoy 09:41 Indirect E31446 172.29.24.38 172.29.24.38 CSO-Ackruti Star-6F 09:30-18:30
E37731 Ramesh Shukla 09:40 Indirect E31446 172.29.23.43 172.29.23.43 CSO-Ackruti Star-6F 09:30-18:30
E37731 Ramesh Shukla 09:40 Indirect E31446 172.29.23.43 172.29.23.55 CSO-Ackruti Star-6F 09:30-18:30
E33995 Rakesh Sharma 08:21 Direct P00212 172.29.23.17 172.29.23.17 CSO-Ackruti Star-6F 09:30-18:30
E43130 Lubna Shaikh 09:37 Indirect E33995 172.29.23.54 172.29.23.54 CSO-Ackruti Star-6F 09:30-18:30
E43130 Lubna Shaikh 09:37 Indirect E33995 172.29.23.54 172.29.23.58 CSO-Ackruti Star-6F 09:30-18:30
E43130 Lubna Shaikh 09:37 Indirect E33995 172.29.23.54 172.29.23.54 CSO-Ackruti Star-6F 09:30-18:30
E43130 Lubna Shaikh 09:37 Indirect E33995 172.29.23.54 172.29.23.58 CSO-Ackruti Star-6F 09:30-18:30
E43455 Manish Shukla Indirect E33995 172.29.23.45 172.29.23.45 CSO-Ackruti Star-6F 09:30-18:30
E44920 Shweta Salve 09:18 Indirect E33995 172.29.23.55 172.29.23.55 CSO-Ackruti Star-6F 09:30-18:30
现在我想以这种方式过滤这些数据,这样我就可以得到以下输出数据:
emp_no emp_name login level HOD_NAME Assigend_IP LoggedIn_IP Attendece_Location Shift_Timing
E31446 Amit Singh 9:39 Direct P00212 172.29.23.53 172.29.23.53 CSO-Ackruti Star-6F 09:30-18:30
E39787 Anita Haridas Shenoy 9:41 Indirect E31446 172.29.24.38 172.29.23.55 CSO-Ackruti Star-6F 09:30-18:30
E37731 Ramesh Shukla 9:40 Indirect E31446 172.29.23.43 172.29.23.43 CSO-Ackruti Star-6F 09:30-18:30
E33995 Rakesh Sharma 8:21 Direct P00212 172.29.23.17 172.29.23.17 CSO-Ackruti Star-6F 09:30-18:30
E43130 Lubna Shaikh 9:37 Indirect E33995 172.29.23.54 172.29.23.54 CSO-Ackruti Star-6F 09:30-18:30
E43455 Manish Shukla Indirect E33995 172.29.23.45 172.29.23.45 CSO-Ackruti Star-6F 09:30-18:30
E44920 Shweta Salve 9:18 Indirect E33995 172.29.23.55 172.29.23.55 CSO-Ackruti Star-6F 09:30-18:30
输出应仅包含每位员工的第一条记录。 我该如何为此编写查询?
答案 0 :(得分:2)
select
emp_no,emp_name,login,level,
HOD_NAME,Assigend_IP,min(LoggedIn_IP),
Attendece_Location,Shift_Timing
from table_name
group by
emp_no,emp_name,login,level,
HOD_NAME,Assigend_IP,
Attendece_Location,Shift_Timing
答案 1 :(得分:1)
以下是使用ROW_NUMBER函数的完整工作示例。关键部分是以下行:
ROW_NUMBER() OVER ( PARTITION BY [emp_no] ORDER BY [emp_no] )
这意味着将为每一行生成“ID”编号,按[emp_no]列对行进行分组。如果您需要其他排序条件,则可以使用任何一列。
这是整个代码:
SET NOCOUNT ON
GO
DECLARE @DataSource TABLE
(
[emp_no] VARCHAR(6)
,[emp_name] NVARCHAR(24)
,[login] VARCHAR(6)
,[level] NVARCHAR(25)
,[HOD_NAME] NVARCHAR(24)
,[Assigend_IP ] NVARCHAR(64)
,[LoggedIn_IP ] NVARCHAR(64)
,[Attendece_Location] NVARCHAR(64)
,[Shift_Timing] NVARCHAR(64)
)
INSERT INTO @DataSource
VALUES ('E31446','Amit Singh','09:39',' Direct','P00212','172.29.23.53','172.29.23.53','CSO-Ackruti Star-6F','09:30-18:30')
,('E31446','Amit Singh','09:39',' Direct','P00212','172.29.23.53','172.29.23.53','CSO-Ackruti Star-6F','09:30-18:30')
,('E39787','Anita Haridas Shenoy','09:41',' Indirect','E31446','172.29.24.38','172.29.23.55','CSO-Ackruti Star-6F','09:30-18:30')
,('E39787','Anita Haridas Shenoy','09:41',' Indirect','E31446','172.29.24.38','172.29.24.38','CSO-Ackruti Star-6F','09:30-18:30')
,('E39787','Anita Haridas Shenoy','09:41',' Indirect','E31446','172.29.24.38','172.29.23.55','CSO-Ackruti Star-6F','09:30-18:30')
,('E39787','Anita Haridas Shenoy','09:41',' Indirect','E31446','172.29.24.38','172.29.24.38','CSO-Ackruti Star-6F','09:30-18:30')
,('E37731','Ramesh Shukla',' 09:40',' Indirect','E31446','172.29.23.43','172.29.23.43','CSO-Ackruti Star-6F','09:30-18:30')
,('E37731','Ramesh Shukla',' 09:40',' Indirect','E31446','172.29.23.43','172.29.23.55','CSO-Ackruti Star-6F','09:30-18:30')
,('E33995','Rakesh Sharma',' 08:21',' Direct','P00212','172.29.23.17','172.29.23.17','CSO-Ackruti Star-6F','09:30-18:30')
,('E43130','Lubna Shaikh','09:37',' Indirect','E33995','172.29.23.54','172.29.23.54','CSO-Ackruti Star-6F','09:30-18:30')
,('E43130','Lubna Shaikh','09:37',' Indirect','E33995','172.29.23.54','172.29.23.58','CSO-Ackruti Star-6F','09:30-18:30')
,('E43130','Lubna Shaikh','09:37',' Indirect','E33995','172.29.23.54','172.29.23.54','CSO-Ackruti Star-6F','09:30-18:30')
,('E43130','Lubna Shaikh','09:37',' Indirect','E33995','172.29.23.54','172.29.23.58','CSO-Ackruti Star-6F','09:30-18:30')
,('E43455','Manish Shukla','',' Indirect','E33995','172.29.23.45','172.29.23.45','CSO-Ackruti Star-6F','09:30-18:30')
,('E44920','Shweta Salve','09:18',' Indirect','E33995','172.29.23.55','172.29.23.55','CSO-Ackruti Star-6F','09:30-18:30')
;WITH DataSource AS
(
SELECT ROW_NUMBER() OVER ( PARTITION BY [emp_no] ORDER BY [emp_no] ) AS [RecordNumber]
,[emp_no]
,[emp_name]
,[login]
,[level]
,[HOD_NAME]
,[Assigend_IP ]
,[LoggedIn_IP ]
,[Attendece_Location]
,[Shift_Timing]
FROM @DataSource
)
SELECT [emp_no]
,[emp_name]
,[login]
,[level]
,[HOD_NAME]
,[Assigend_IP ]
,[LoggedIn_IP ]
,[Attendece_Location]
,[Shift_Timing]
FROM DataSource
WHERE [RecordNumber] = 1
SET NOCOUNT OFF
GO
这是输出:
答案 2 :(得分:0)
SELECT *
FROM EmployeeLog LOG
INNER JOIN
(
SELECT emp_name,
MAX(login) AS last_login
FROM EmployeeLog
GROUP BY emp_name
) LATEST
ON LOG.emp_name = LATEST.emp_name
AND LOG.login = LATEST.last_login
这将为每位员工提供最新的登录记录,这是我认为您正在追求的。
如果您在最早登录后,请将子查询中的MAX()
更改为MIN()
。
答案 3 :(得分:0)
以下是从重复记录中仅获取一条记录的查询,
SELECT * ,count(*) as cnt,emp_name FROM `emp`
group by emp_name