2017-08-23 33 views
0

Emp表中現有的數據:SQL查詢來獲取在SQL Server中的每個記錄所有的父母和孩子2014

EmpId Name   RejEmpId DOJ    DOL   
---------------------------------------------------------- 
1  Name1   NULL 10-12-2014  12-06-2015 
2  Name1   1  06-04-2016  24-12-2016  
3  Name1   2  01-04-2017  NULL   
4  Name2   NULL 22-12-2014  21-07-2015 
5  Name2   4  10-04-2016  22-12-2016  
6  Name3   NULL 10-05-2015  NULL    
7  Name4   NULL 10-05-2015  NULL  

我想自定義列(所有家長和孩子EMPID,實際司法部,實際DOL )

EmpId Name RejEmpId DOJ  DOL   All    
              Parent    
              And 
              Child Actual  Actual 
              EmpId DOJ   DOL   
1 Name1 NULL 10-12-2014 12-06-2015 1.2.3 10-12-2014 NULL 
2 Name1 1  06-04-2016 24-12-2016 1.2.3 10-12-2014 NULL 
3 Name1 2  01-04-2017 NULL  1.2.3 10-12-2014 NULL 
4 Name2 NULL 22-12-2014 21-07-2015 4.5  22-12-2014 22-12-2016 
5 Name2 4  10-04-2016 22-12-2016 4.5  22-12-2014 22-12-2016 
6 Name3 NULL 10-05-2015 NULL  6  10-05-2015 NULL 
7 Name4 NULL 10-05-2015 NULL  7  10-05-2015 NULL 
8 Name5 NULL 12-06-2015 20-12-2016 8  12-06-2015 20-12-2016 
+1

問題是什麼? 「爲我編寫查詢」不是一個問題。有* LOT *重複問題,顯示如何用'hierarchyid'或遞歸CTE編寫分層查詢。你嘗試過什麼嗎?你有沒有遇到任何問題? –

+0

我確實使用https://www.codeproject.com/Articles/818694/SQL-queries-to-manage-hierarchical-or-parent-child –

+0

我曾使用https://www.codeproject.com/Articles/818694/SQL-queries-to-manage-hierarchical-or-parent-child「所有可能的父項在列」部分。我無法做到真正的DOJ和實際DOL。實際DOJ是第一個家長DOJ和實際DOL最後一個孩子DOL –

回答

0

如果我理解正確,你可以按照以下查詢:

進行連結,你可以使用的東西,獲得司法部的第一個值,你可以使用FIRST_VALUE函數是SQL Server 2012+同樣地,對於DOL您可以使用LAST_VALUE功能如下:如下

Select emp.EmpId, Emp.[Name], RejEmpId, emp.DOJ, DOL, concatStr as [AllParent and Child EmpId] 
    ,FIRST_VALUE(DOJ) over(partition by emp.[Name] order by EmpId) as ActualDOJ 
    ,LAST_VALUE(DOL) over(partition by emp.[Name] order by EmpId rows between current row and unbounded following) as AcualDOL 
    from #empdata emp 
cross apply (
    Select [Name], concatStr = stuff((select concat('.', EmpId) from #empdata e where e.[Name] = o.[Name] for xml path('')),1,1,'') 
    from #empdata o 
    group by [Name] 
) c where emp.[Name] = c.[Name] 

輸出:

+-------+-------+----------+------------+------------+---------------------------+------------+------------+ 
| EmpId | Name | RejEmpId | DOJ  | DOL  | AllParent and Child EmpId | ActualDOJ | AcualDOL | 
+-------+-------+----------+------------+------------+---------------------------+------------+------------+ 
|  1 | Name1 | NULL  | 2014-12-10 | 2015-06-12 |      1.2.3 | 2014-12-10 | NULL  | 
|  2 | Name1 | 1  | 2016-04-06 | 2016-12-24 |      1.2.3 | 2014-12-10 | NULL  | 
|  3 | Name1 | 2  | 2017-04-01 | NULL  |      1.2.3 | 2014-12-10 | NULL  | 
|  4 | Name2 | NULL  | 2014-12-22 | 2015-07-21 |      4.5 | 2014-12-22 | 2016-12-22 | 
|  5 | Name2 | 4  | 2016-04-10 | 2016-12-22 |      4.5 | 2014-12-22 | 2016-12-22 | 
|  6 | Name3 | NULL  | 2015-05-10 | NULL  |       6 | 2015-05-10 | NULL  | 
|  7 | Name4 | NULL  | 2015-05-10 | NULL  |       7 | 2015-05-10 | NULL  | 
+-------+-------+----------+------------+------------+---------------------------+------------+------------+ 
+0

同名,但它是不同的員工方式,發生了什麼 –

+0

因爲你需要提供一個具有正確的員工標識符屬性的列 –

+0

想要爲同一員工保持不平等。這是對的嗎。 –

相關問題