分组在oracle sql和sum中

时间:2015-06-30 08:07:07

标签: sql oracle

请在以下代码中提供帮助。谢谢

Select lms_employees.lms_emp_code, (SELECT LMS_DEPT_DESC FROM LMS_DEPARTMENT WHERE LMS_DEPT_CODE = LMS_EMP_DEPT) AS LMS_ATT_DEPT, sum(lms_attendance.lms_att_lateness),
                Lms_Employees.Lms_Emp_Name,
                (Case 
                    when lms_per_emp_code is not null and lms_per_status !='CL' AND lms_lea_empl_code is not null and lms_att_remarks is not null then 
                    (case when lms_lea_leave_type ='L' and lms_lea_status !='CL' and lms_per_status !='CL' Then lms_att_remarks ||'|'||'Permission|Half-casual'
                        When Lms_Lea_Leave_Type ='S' And Lms_Lea_Status !='CL' And Lms_Per_Status !='CL' Then Lms_Att_Remarks ||'|'||'Permission|Half-sick'
                        when lms_lea_leave_type ='OFF' and lms_lea_status !='CL' and lms_per_status !='CL' Then lms_att_remarks ||'|'||'Permission|Half-off'
                    else lms_att_remarks ||'|'||'permission' end)

                When Lms_Per_Emp_Code Is Not Null and lms_per_status !='CL' Then 
                    (case when lms_lea_leave_type ='L' and lms_lea_status !='CL' and lms_per_status !='CL' then 'Half-casual|permission'
                        When Lms_Lea_Leave_Type ='S' And Lms_Lea_Status !='CL' And Lms_Per_Status !='CL' Then 'Half-sick|permission'
                        when lms_lea_leave_type ='OFF' and lms_lea_status !='CL' and lms_per_status !='CL' then 'Half-off|permission'
                        when lms_att_remarks is not null then lms_att_remarks ||'|'||'permission'
                    else 'permission' end)

                when lms_lea_empl_code Is Not Null and lms_lea_status !='CL' and (lms_lea_leave_type = 'L' or lms_lea_leave_type ='S' or lms_lea_leave_type = 'OFF') Then 
                    (case
                        when lms_lea_leave_type ='L' and lms_att_remarks is not null then lms_att_remarks ||'|'||'Half-casual'
                        When Lms_Lea_Leave_Type='S' And Lms_Att_Remarks Is Not Null Then Lms_Att_Remarks ||'|'||'Half-sick'
                        when lms_lea_leave_type='OFF' and lms_att_remarks is not null then lms_att_remarks ||'|'||'Half-off'
                        when lms_lea_leave_type='L' then 'Half-casual'
                        When Lms_Lea_Leave_Type='S' Then 'Half-sick'
                        when lms_lea_leave_type='OFF' then 'Half-off'
                    else null end) 

                Else lms_att_remarks
                End)remarks,

                lms_att_lateness, lms_att_extra_hrs,
                    (LMS_ATT_EXTRA_HRS - lms_att_LATENESS)as TIME_DIFF,
                     LMS_ATT_TIME_IN,LMS_ATT_TIME_OUT,TO_CHAR(LMS_ATT_DATE ,'DD-MM-YYYY')  AS LMS_ATT_DATE

                From Lms_Attendance 
                Left Join Lms_Permission
                On Lms_Attendance.Lms_Att_Empl_Code = Lms_Permission.Lms_Per_Emp_Code
                And Lms_Attendance.Lms_Att_Date = Lms_Permission.Lms_Per_Date
                Left Join Lms_Employees
                on LMS_EMPLOYEES.LMS_EMP_CODE=LMS_ATT_EMPL_CODE
                left join lms_leaves
                On Lms_Attendance.Lms_Att_Empl_Code = Lms_Leaves.Lms_Lea_Empl_Code
                And Lms_Attendance.Lms_Att_Date = Lms_leaves.Lms_lea_date_from

                Group By Lms_Employees.Lms_Emp_Code,
                --(Select Lms_Dept_Desc From Lms_Department Where Lms_Dept_Code = Lms_Emp_Dept),

                --Sum(Lms_Attendance.Lms_Att_Lateness),
                Lms_Employees.Lms_Emp_Name,Lms_Attendance.Lms_Att_Remarks,
                Lms_Att_Lateness, Lms_Att_Extra_Hrs,(Lms_Att_Extra_Hrs - Lms_Att_Lateness),
                LMS_ATT_TIME_IN,LMS_ATT_TIME_OUT,TO_CHAR(LMS_ATT_DATE ,'DD-MM-YYYY');
                Lms_Attendance.Lms_Att_Remarks;   

我想按员工代码分组

大家好,我想在下面的sql语句中按emp_code分组,以便我可以计算lms_att_lateness的总和。请帮忙。

1 个答案:

答案 0 :(得分:0)

您可以轻松删除GROUP BY功能并使用OVER PARTITION BY;当你可以把你的分组和订单标准放在那里

Select lms_employees.lms_emp_code, (SELECT LMS_DEPT_DESC FROM LMS_DEPARTMENT WHERE LMS_DEPT_CODE = LMS_EMP_DEPT) AS LMS_ATT_DEPT, 
     SUM(lms_attendance.lms_att_lateness) OVER ( PARTITION BY lms_emp_code ORDER BY NULL) as "your_sum",
                    Lms_Employees.Lms_Emp_Name,
                    (Case 
                        when lms_per_emp_code is not null and lms_per_status !='CL' AND lms_lea_empl_code is not null and lms_att_remarks is not null then 
                        (case when lms_lea_leave_type ='L' and lms_lea_status !='CL' and lms_per_status !='CL' Then lms_att_remarks ||'|'||'Permission|Half-casual'
                            When Lms_Lea_Leave_Type ='S' And Lms_Lea_Status !='CL' And Lms_Per_Status !='CL' Then Lms_Att_Remarks ||'|'||'Permission|Half-sick'
                            when lms_lea_leave_type ='OFF' and lms_lea_status !='CL' and lms_per_status !='CL' Then lms_att_remarks ||'|'||'Permission|Half-off'
                        else lms_att_remarks ||'|'||'permission' end)

                    When Lms_Per_Emp_Code Is Not Null and lms_per_status !='CL' Then 
                        (case when lms_lea_leave_type ='L' and lms_lea_status !='CL' and lms_per_status !='CL' then 'Half-casual|permission'
                            When Lms_Lea_Leave_Type ='S' And Lms_Lea_Status !='CL' And Lms_Per_Status !='CL' Then 'Half-sick|permission'
                            when lms_lea_leave_type ='OFF' and lms_lea_status !='CL' and lms_per_status !='CL' then 'Half-off|permission'
                            when lms_att_remarks is not null then lms_att_remarks ||'|'||'permission'
                        else 'permission' end)

                    when lms_lea_empl_code Is Not Null and lms_lea_status !='CL' and (lms_lea_leave_type = 'L' or lms_lea_leave_type ='S' or lms_lea_leave_type = 'OFF') Then 
                        (case
                            when lms_lea_leave_type ='L' and lms_att_remarks is not null then lms_att_remarks ||'|'||'Half-casual'
                            When Lms_Lea_Leave_Type='S' And Lms_Att_Remarks Is Not Null Then Lms_Att_Remarks ||'|'||'Half-sick'
                            when lms_lea_leave_type='OFF' and lms_att_remarks is not null then lms_att_remarks ||'|'||'Half-off'
                            when lms_lea_leave_type='L' then 'Half-casual'
                            When Lms_Lea_Leave_Type='S' Then 'Half-sick'
                            when lms_lea_leave_type='OFF' then 'Half-off'
                        else null end) 

                    Else lms_att_remarks
                    End)remarks,

                    lms_att_lateness, lms_att_extra_hrs,
                        (LMS_ATT_EXTRA_HRS - lms_att_LATENESS)as TIME_DIFF,
                         LMS_ATT_TIME_IN,LMS_ATT_TIME_OUT,TO_CHAR(LMS_ATT_DATE ,'DD-MM-YYYY')  AS LMS_ATT_DATE

                    From Lms_Attendance 
                    Left Join Lms_Permission
                    On Lms_Attendance.Lms_Att_Empl_Code = Lms_Permission.Lms_Per_Emp_Code
                    And Lms_Attendance.Lms_Att_Date = Lms_Permission.Lms_Per_Date
                    Left Join Lms_Employees
                    on LMS_EMPLOYEES.LMS_EMP_CODE=LMS_ATT_EMPL_CODE
                    left join lms_leaves
                    On Lms_Attendance.Lms_Att_Empl_Code = Lms_Leaves.Lms_Lea_Empl_Code
                    And Lms_Attendance.Lms_Att_Date = Lms_leaves.Lms_lea_date_from ;