LEFT JOIN Using distinct?

后端 未结 3 677
一向
一向 2021-01-20 02:47

Originally, I have 15 employees on my table. Two of them have the same LastName.

If I leave the E.MAT_EMP selection as it is, it works fine and 15 emplo

3条回答
  •  自闭症患者
    2021-01-20 03:31

    Replace the * with the column names you want to show, and don't remove it from the subquery.

        Select ROW_NUMBER() OVER (ORDER BY LastName ASC) as [N°], [LastName], [FirstName] /*other needed columns here*/
    From  ( 
            Select E.MAT_EMP, NOM_EMP as [LastName],PRENOM_EMP as [FirstName],
                  Item      = day(d)
                  ,Value     = 
                  case when (D between DEBUT_DRC and FIN_DRC) and STATUS_DRC = ''Accepté'' then ''RC'' 
                  when (D between DEBUT_DAB and FIN_DAB) and STATUS_DAB = ''Accepté'' then ''ABS'' 
                  when (D between DC_DEBUT and DC_FIN) and STATUS_DC = ''Accepté'' then DCon.CODE_TYPE_CONGE
                  else '''' 
    
                  end
             From 
             DEMANDE_RECUPERATION DC RIGHT JOIN EMPLOYE E 
             ON DC.MAT_EMP = E.MAT_EMP 
    
             LEFT JOIN DEMANDE_ABSENCE ABS
             ON E.MAT_EMP = ABS.MAT_EMP 
    
             LEFT JOIN DEMANDE_CONGE DCon 
             ON E.MAT_EMP = DCon.MAT_EMP 
    
             Cross Join (
                            Select Top (DateDiff(DAY,@D1,@D2)+1) D=DateAdd(DAY,-1+Row_Number() Over (Order By (Select Null)),@D1) From  master..spt_values n1
                        ) B
           ) src
     Pivot (max(value) for Item in ('+@Cols+') ) pvt
    

提交回复
热议问题