带有条件的where子句中的SQL SUMs

时间:2015-02-03 16:18:51

标签: sql oracle sum subquery

我希望得到一个"总数"商业报告XYZ。他们想要的是季节,学期,员工人数,以及员工的总工作时间,只有当员工数量减少时才会出现!=任何等于下降的增量。

尝试做这样的事情:

select year,
   season,
   (select count(distinct empID)
  from tableA
 where a.season = season
   and a.year = year) "Employees",
   (select sum(hours)
   from(
     select distinct year,season,empID,hours
       from tableA
              where code like 'Drop%'
    )
  where a.season = season
    and a.year = year) "Dropped"

from tableA a
-- need help below
where (select sum(hours)
   from(
     select distinct year,season,empID,hours
       from tableA
      where code like 'Drop%'
    )
   where a.season = season
     and a.year = year
     and a.emplID = emplID)
!=
 (select sum(hours)
   from(
     select distinct year,season,empID,hours
       from tableA
      where code like 'Add%'
    )
   where a.season = season
     and a.year = year
     and a.emplID = emplID)
  group by year,season

看来我没有正确地做我的where子句。我不相信我正在加入emplID到每个emplID正确地排除那些" drop" <> "添加"

编辑: 样本数据:
年,季,EMPLID,时间,代码
2015年,秋季,001,10,下降
20150 FALL,001,10,添加
2015年,秋天,002,5,降
2015年,FALL,003,10,Drop

总小时数应为15. EmplyID 001应从总计中移除,因为他的滴数完全等于添加。

3 个答案:

答案 0 :(得分:0)

我认为你可以用条件聚合做你想做的事。像这样:

select year, season, count(distinct empID) as Employees,
       sum(case when code like 'Drop%' then hours end) as Dropped
from tableA
group by year, season;

很难确切地说出你想要什么,因为你没有样本数据和期望的结果(或者更好的是,SQL小提琴)。您可能还需要having子句:

having (sum(case when code like 'Drop%' then hours end) <>
        sum(case when code like 'Add%' then hours end) 
       )

答案 1 :(得分:0)

你想要这样的结果吗?

SELECT
   year
   ,season
   ,COUNT(DISTINCT empID) AS Employees
   ,SUM(CASE WHEN code LIKE 'Drop%' THEN hours ELSE 0 END) AS Dropped
FROM
   TableA
GROUP BY
   year
   ,season
HAVING
   (
   SUM(CASE WHEN code LIKE 'Drop%' THEN hours ELSE 0 END) 
   - SUM(CASE WHEN code LIKE 'Add%' THEN hours ELSE 0 END)
   ) <> 0

答案 2 :(得分:0)

我设法通过一些分析来解决这个问题..;)

  with tableA as ( 
           select 2015 year, 1 season, 1234 empID, 2 hours  , 'Add' code from dual union all
           select 2015 year, 1 season, 1234 empID, 3 hours  , 'Add' code from dual union all
           select 2015 year, 1 season, 1234 empID, 4 hours  , 'Add' code from dual union all
           select 2015 year, 1 season, 1234 empID, 2 hours  , 'Drop' code from dual union all
           select 2015 year, 1 season, 2345 empID, 5 hours  , 'Add' code from dual union all
           select 2015 year, 1 season, 2345 empID, 3.5 hours, 'Add' code from dual union all
           select 2015 year, 2 season, 1234 empID, 7 hours  , 'Add' code from dual union all
           select 2015 year, 2 season, 1234 empID, 5 hours  , 'Add' code from dual union all
           select 2015 year, 2 season, 2345 empID, 5 hours  , 'Add' code from dual union all
           select 2015 year, 2 season, 7890 empID, 3 hours  , 'Add' code from dual union all
           select 2014 year, 1 season, 1234 empID, 1 hours  , 'Add' code from dual union all
           select 2014 year, 1 season, 1234 empID, 2 hours  , 'Add' code from dual union all
           select 2014 year, 1 season, 1234 empID, 4 hours  , 'Add' code from dual
        ),
     w_group as (     
        select year, season, empID, hours, code,
               lead(hours) over (partition by year, season, empID, hours 
                             order by case when code like 'Drop%' then 'DROP'
                                           when code like 'Add%' then 'ADD'
                                           else NULL end ) new_hours
          from tableA
        )
  select year, season, count(distinct empID),
              sum(hours-nvl(new_hours,0)) total_hours
    from w_group
   where code like 'Add%'
   group by year, season
  /

        YEAR     SEASON COUNT(DISTINCTEMPID) TOTAL_HOURS
  ---------- ---------- -------------------- -----------
        2015          1                    2        15.5
        2014          1                    1           7
        2015          2                    3          20

(第一部分&#34;有表A和#34;只是伪造一些数据,因为你没有提供任何数据):)

[编辑] 根据你的数据和你的解释纠正 - 简而言之,你重新计算DROPs,(减去ADD),我正在做相反的事情 [edit2]根据评论/反馈用小调整替换下面的查询:如果他们的DROP-ADD为零,则不计算empID

 with tableA as ( 
     select 2015 year, 'FALL' season, '001' empID, 10 hours, 'Drop' code from dual union all
     select 2015 year, 'FALL' season, '001' empID, 10 hours, 'Add' code from dual union all
     select 2015 year, 'FALL' season, '002' empID, 5  hours, 'Drop' code from dual union all
     select 2015 year, 'FALL' season, '003' empID, 10 hours, 'Drop' code from dual
       ),
    w_group as (     
       select year, season, empID, hours, code,
              lag(hours) over (partition by year, season, empID, hours 
                            order by case when code like 'Drop%' then 'DROP'
                                          when code like 'Add%' then 'ADD'
                                          else NULL end ) new_hours
         from tableA
       )
 select year, season, count(distinct empID),
             sum(hours-nvl(new_hours,0)) total_hours
   from w_group
  where code like 'Drop%'
    and hours - nvl(new_hours,0) > 0
  group by year, season
 /

        YEAR SEAS COUNT(DISTINCTEMPID) TOTAL_HOURS
  ---------- ---- -------------------- -----------
        2015 FALL                    2          15

[/编辑]