2013-07-31 61 views
-2

我已經follwing數據:要從組員工只能得到第一個記錄

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

你能告訴我們你已經嘗試了什麼? – hkf

+0

@shweta:你如何決定哪一行是第一行? –

+0

@ravi singh:每個員工第一排的第一排是員工的第一次登錄打卡,所以它取決於登錄時間。 – shweta

回答

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 

這是輸出:

enter image description here

+0

爲了記錄,這不會遠程執行OP所要求的。它*會*只挑選每個用戶中的一個,但完全忽略結果的登錄時間。嘗試在樣本數據中混合時間並觀察發生了什麼:http://sqlfiddle.com/#!3/d41d8/18282 – Curt

+0

會發生什麼?正如我所說 - 「如果您需要其他的排序標準,您可以使用任何一列。」 – gotqn

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()

+0

** [您的查詢結果](http://sqlfiddle.com/#!3/867d2/1)** – Luv

+0

@Luv您的數據讓每個用戶同時登錄三四個不同的地方。不太確定這是OP詢問的:-) – Curt

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 
+0

這將提供編號最小的IP地址,但不是第一次登錄。 – Curt

+0

@shweta確切的** [相同的輸出](http://sqlfiddle.com/#!3/867d2/2)**你需要。 – Luv

+0

@Luv我得到了你的查詢輸出..但這個查詢將返回登錄Ip的最小值?它可能會在將來產生問題 – shweta

0

下面是查詢正從重複的記錄只有一條記錄,

SELECT * ,count(*) as cnt,emp_name FROM `emp` 
group by emp_name