SQL:如何将多行合并为一行

时间:2017-03-18 06:20:25

标签: sql-server pivot

这是我在这个非常有用的博客中的第一篇文章。 在我的考勤系统中,我有不同的部分,每个部分有3或4班。像A Shift B Shift和C Shift一样。 我有一个SQL查询。每个班次的输出多行。

SELECT          
            sc.SectName,sns.ShiftName,
            dbo.fn_ReceptiveEmployee_SectionWise(sc.Sect_Number) as SetupForWorker,
            dbo.fn_AsperRegister_SectionWise(sc.Sect_Number, prd.Shift_Number,'01 jan 2017') as HandsInRegister,
            isnull(dbo.fn_Daily_Present_Days_SectWise(sc.Sect_Number, prd.Shift_Number,'01 jan 2017'),0) as EmpTotalPresent,
            isnull(dbo.fn_Daily_Late_Days_SectWise(sc.Sect_Number,prd.Shift_Number,'01 jan 2017'),0) as EmpTotalLate,       
            isnull(dbo.fn_Daily_Absent_Days_SectWise(sc.Sect_Number,prd.Shift_Number,'01 jan 2017'),0) as EmpTotalAbsent,
            isnull(dbo.fn_Daily_WH_Days_SectWise(sc.Sect_Number,prd.Shift_Number,'01 jan 2017'),0) as EmpTotalWHDay,        
            isnull(dbo.fn_Daily_Leave_Total_SectWise(sc.Sect_Number,prd.Shift_Number,'01 jan 2017'),0) as EmpTotalLeaveDay

        FROM    dbo.tblEmployee emp INNER JOIN
                dbo.tblProcessed_Data prd ON emp.Emp_Number = prd.Emp_Number INNER JOIN
                dbo.tblSection sc ON emp.Sect_Number = sc.Sect_Number INNER JOIN
                dbo.tblDesignation dsg ON emp.Desig_Number = dsg.Desig_Number INNER JOIN
                dbo.tblDepartment dpt ON sc.Dept_Number = dpt.Dept_Number INNER JOIN
                dbo.tblBranch br ON dpt.Branch_Number = br.Branch_Number INNER JOIN
                dbo.tblCompany cmp ON dpt.Comp_Number = cmp.Comp_Number INNER JOIN 
                dbo.tbl_ShiftNameSettings sns ON sns.Shift_Number= prd.Shift_Number

        Where   emp.EmpED = 1 
                and br.Branch_Number = 2    
                and prd.PunchDate = convert(varchar,'01 jan 2017',12)
                and cmp.Comp_Number = 1
                --and dbo.tblProcessed_Data.Shift_Number = 1

        group by prd.Shift_Number, sns.ShiftName,sc.Sect_Number,sc.SectName

但是我希望每个部分的每一个班次都是一行的。

Result

应该是......

Result in One Row

请帮帮我.. 谢谢。

0 个答案:

没有答案