带条件的Oracle SQL Sum

时间:2019-01-21 11:27:00

标签: sql oracle report

我试图按部门和月份求和(当前时数),(疾病时数)。这笔款项必须包括一份案情陈述,以包括我所从事的部门。我已经尝试过多次,但是似乎无法按预期工作。

EMP_NO     Wage_type     Hours_worked   Department      OBJID(UNIQUE)  DATE
10011      Normal        14             1063            ABC116         04/01/18
10011      Normal        07             1063            ABC117         05/01/18
10011      SICK          21             1063            ABC118         01/02/18
10030      Normal        12.5           1054            ABC119         02/02/18
10030      SICK          7              1054            ABC120         03/02/18
10030      SICK          7              1054            ABC121         04/02/18

原始数据等的示例代码:

Select 
 TPDRS.OBJID
,TPDRS.WAGE_HOURS
,(case 
    when TPDRS.ORG_CODE = 1010 then  'Trolley P1'
    when TPDRS.ORG_CODE = 1011 then  'Trolley P2'
    when TPDRS.ORG_CODE = 1053 then  'Trolley P3'
    when TPDRS.ORG_CODE between 1054 and 1057 then  'Trolley P4'
    when TPDRS.ORG_CODE between 1040 and 1047 then  'Trolley P5'
    when TPDRS.ORG_CODE in ('1063','1064','1065','1068') then 'Trolley P6'
    else 'NOT REQUIRED'
        end) as Department
,TPDRS.EMP_NO
,TPDRS.ACCOUNT_DATE
,EXTRACT(Month from TPDRS.ACCOUNT_DATE) "Month_Number"
,EXTRACT(Year from TPDRS.ACCOUNT_DATE) "Year"

from TrolleyParkAttendant  TPDRS
WHERE TPDRS.ACCOUNT_DATE > '01/DEC/2017'
 AND (TPDRS.WAGE_GRP_DB IN ( 'O', 'N' ) or WAGE_CODE = 'SICK')

Expected Results:
Department    HoursSick     HoursPresent     Month
Trolley P6      0             21              01
Trolley P6     21             0               02 
Trolley P3     14             12.5            02

我们将为您提供任何帮助

3 个答案:

答案 0 :(得分:3)

您需要条件聚合,以避免重复表达式将它们嵌套在派生表中(在Oracle中是 Inline View ):

select
    Department
  ,sum(case when Wage_type = 'SICK' then Hours_worked else 0 end) as HoursSick
  ,sum(case when Wage_type = 'Normal' then Hours_worked else 0 end) as HoursPresent
  ,"Month_Number"
  ,"Year"
from
 (
    Select 
     (case 
        when TPDRS.ORG_CODE = 1010 then  'Trolley P1'
        when TPDRS.ORG_CODE = 1011 then  'Trolley P2'
        when TPDRS.ORG_CODE = 1053 then  'Trolley P3'
        when TPDRS.ORG_CODE between 1054 and 1057 then  'Trolley P4'
        when TPDRS.ORG_CODE between 1040 and 1047 then  'Trolley P5'
        when TPDRS.ORG_CODE in ('1063','1064','1065','1068') then 'Trolley P6'
        else 'NOT REQUIRED'
            end) as Department
    ,EXTRACT(Month from TPDRS.ACCOUNT_DATE) "Month_Number"
    ,EXTRACT(Year from TPDRS.ACCOUNT_DATE) "Year"

    from TrolleyParkAttendant  TPDRS
    WHERE TPDRS.ACCOUNT_DATE > '01/DEC/2017'
     AND (TPDRS.WAGE_GRP_DB IN ( 'O', 'N' ) or WAGE_CODE = 'SICK')
 ) dt
group by
    Department
  ,"Month_Number"
  ,"Year"

答案 1 :(得分:2)

您可以将以下SQL语句与conditional aggregation

一起使用
SELECT decode(1063,'Trolley P6',1054,'Trolley P3') as Department,
       sum(case when Wage_type='SICK' then Hours_worked end) as HoursSick,
       sum(case when Wage_type='Normal' then Hours_worked end) as HoursPresent,
       to_char(TPDRS.ACCOUNT_DATE,'mm') as "Month"       
  FROM TrolleyParkAttendant  TPDRS
 WHERE TPDRS.ACCOUNT_DATE > '01/DEC/2017'
   AND (TPDRS.WAGE_GRP_DB IN ( 'O', 'N' ) or WAGE_CODE = 'SICK')
 GROUP BY decode(1063,'Trolley P6',1054,'Trolley P3'), 
          to_char(TPDRS.ACCOUNT_DATE,'mm');

答案 2 :(得分:2)

您的数据模型似乎缺少部门表和组织表。您可能应该创建它们。好吧,CTE(class Paragraph extends Resource { public static $model = 'myvendor\\mypackage\\app\\Models\\Paragraph'; public function fields(Request $request) { return [ BelongsTo::make('Page'), // String | Class name must be a valid object or string BelongsTo::make(Page::class) // NovaResource | Class myvendor\mypackage\app\Nova\Myvendor\mypackage\app\Nova\Page not found (notice the capital letter in the second occurrence of Myvendor) BelongsTo::make('Page', 'page', \myvendor\mypackage\app\Nova\Page::class) // Class name must be a valid object or string BelongsTo::make('Page', 'page', myvendor\mypackage\app\Nova\Page::class) // Class name must be a valid object or string ]; } 子句)现在就可以做...

WITH

我认为with dept as ( select 'P1' as id, 'Trolley P1' as name from dual union all select 'P2' as id, 'Trolley P2' as name from dual union all select 'P3' as id, 'Trolley P3' as name from dual union all select 'P4' as id, 'Trolley P4' as name from dual union all select 'P4' as id, 'Trolley P5' as name from dual union all select 'P6' as id, 'Trolley P6' as name from dual ) , org as ( select 1010 as code, 'P1' as dept_id from dual union all select 1011 as code, 'P2' as dept_id from dual union all select 1053 as code, 'P3' as dept_id from dual union all select 1054 as code, 'P4' as dept_id from dual union all select 1057 as code, 'P4' as dept_id from dual union all select 1040 as code, 'P5' as dept_id from dual union all select 1047 as code, 'P5' as dept_id from dual union all select 1063 as code, 'P6' as dept_id from dual union all select 1064 as code, 'P6' as dept_id from dual union all select 1065 as code, 'P6' as dept_id from dual union all select 1068 as code, 'P6' as dept_id from dual ) select dept.name as department, sum(case when tpa.wage_code = 'SICK' then wage_hours end) as hours_sick, sum(case when tpa.wage_grp_db in ('O', 'N') then wage_hours end) as hours_present, to_char(account_date, 'yyyy-mm') as month from trolleyparkattendant tpa join org on org.code = tpa.org_code join dept on dept.id = org.dept_id group by dept.name, to_char(account_date, 'yyyy-mm') order by month, department; wage_grp_db in ('O', 'N')是互斥的。您的wage_code = 'SICK'子句暗示了这一点。如果不是这种情况,您可能必须采用这种方式。

如果您也要计算非部门记录,请改为WHERE