即使 sql developer 中不存在数据,也返回给定员工的行

Posted

技术标签:

【中文标题】即使 sql developer 中不存在数据,也返回给定员工的行【英文标题】:return rows for given employees even if data does not exists in sql developer 【发布时间】:2013-12-31 06:59:00 【问题描述】:

我的 sql 查询返回特定周给定员工的正确结果

例如。我的员工名单是 (emp1 , emp2 , emp3 , emp4)

如果 emp4 在那一周没有做任何工作,则根本不会返回该员工的行。

我的查询如下:

select a.creator_login ,
sum (case when a.activitytype = 'NCE- Installation' then a.duration else 0 end) as NCE_Installation ,
sum (case when a.activitytype = 'NCE- Migration' then a.duration else 0 end) as NCE_Migration ,
sum (case when a.activitytype = 'NCE-Circuit Testing' then a.duration else 0 end) as NCE_Circuit_Testing ,
sum (case when a.activitytype = 'NCE-Communication - External' then a.duration else 0 end) as NCE_Communication_External,
sum (case when a.activitytype = 'NCE-Communication - Internal' then a.duration else 0 end) as NCE_Communication_Internal,
sum (case when a.activitytype = 'Exception' then a.duration else 0 end) as Exception,
sum (case when a.activitytype = 'NCE-Configuration' then a.duration else 0 end) as NCE_Configuration,
sum (case when a.activitytype = 'NCE-Design Reqt Gathering' then a.duration else 0 end) as NCE_Design_Reqt_Gathering,
sum (case when a.activitytype = 'NCE-Documentation' then a.duration else 0 end) as NCE_Documentation,
sum (case when a.activitytype = 'Notes' then a.duration else 0 end) as Notes,
sum (case when b.openingcode = 'GOC Acceptance' then a.duration else 0 end) as  GOC_Acceptance,
sum (case when a.activitytype = 'To Do' then a.duration else 0 end) as To_Do 
from vware.snap_ticketactivities a , vware.snap_troubletickets b 
where a.ticketrowid  = b.ticketrowid  
and a.creator_login in ('AMITTAL','HSHARMA','NKHAN','PKSINGH','PPATNANA','PTHAKUR','SDAS','SPATEL','VDASS','VVIGNESHWARAN','AOAK') and a.created between  trunc(sysdate-12)  and trunc(sysdate-6 )
group by a.creator_login

如果该周的员工记录不存在,则员工姓名应显示为 0 值

【问题讨论】:

你应该删除old version of your question以避免重复。 @YaroslavShabalin 嘿,你能帮帮我吗\ 如果您有单独的表来存储员工姓名,那么您可以将其外部连接到查询中的表中。 emp4 应该在该表中,并将在结果查询中。 @YaroslavShabalin,我知道如果有另一个带有员工姓名的表可以做到,但在我的情况下我该怎么做,检查我的查询 你怎么知道提到的员工根本存在?在某个地方应该有关于他的信息。你从哪里得到IN 子句的列表? 【参考方案1】:

试试这个:

select emp_list.creator_login,
       sum(case
             when a.activitytype = 'NCE- Installation' then
              a.duration
             else
              0
           end) as NCE_Installation,
       sum(case
             when a.activitytype = 'NCE- Migration' then
              a.duration
             else
              0
           end) as NCE_Migration,
       sum(case
             when a.activitytype = 'NCE-Circuit Testing' then
              a.duration
             else
              0
           end) as NCE_Circuit_Testing,
       sum(case
             when a.activitytype = 'NCE-Communication - External' then
              a.duration
             else
              0
           end) as NCE_Communication_External,
       sum(case
             when a.activitytype = 'NCE-Communication - Internal' then
              a.duration
             else
              0
           end) as NCE_Communication_Internal,
       sum(case
             when a.activitytype = 'Exception' then
              a.duration
             else
              0
           end) as Exception,
       sum(case
             when a.activitytype = 'NCE-Configuration' then
              a.duration
             else
              0
           end) as NCE_Configuration,
       sum(case
             when a.activitytype = 'NCE-Design Reqt Gathering' then
              a.duration
             else
              0
           end) as NCE_Design_Reqt_Gathering,
       sum(case
             when a.activitytype = 'NCE-Documentation' then
              a.duration
             else
              0
           end) as NCE_Documentation,
       sum(case
             when a.activitytype = 'Notes' then
              a.duration
             else
              0
           end) as Notes,
       sum(case
             when b.openingcode = 'GOC Acceptance' then
              a.duration
             else
              0
           end) as GOC_Acceptance,
       sum(case
             when a.activitytype = 'To Do' then
              a.duration
             else
              0
           end) as To_Do
  from (select 'AMITTAL' creator_login
          from dual
        union all
        select 'HSHARMA'
          from dual
        union all
        select 'NKHAN'
          from dual
        union all
        select 'PKSINGH'
          from dual
        union all
        select 'PPATNANA'
          from dual
        union all
        select 'PTHAKUR'
          from dual
        union all
        select 'SDAS'
          from dual
        union all
        select 'SPATEL'
          from dual
        union all
        select 'VDASS'
          from dual
        union all
        select 'VVIGNESHWARAN'
          from dual
        union all
        select 'AOAK' from dual) emp_list
  left outer join vware.snap_ticketactivities a on emp_list.creator_login =
                                                   a.creator_login
 inner join vware.snap_troubletickets b on a.ticketrowid = b.ticketrowid
 where nvl(a.created, trunc(sysdate - 12)) between trunc(sysdate - 12) and
       trunc(sysdate - 6)
 group by emp_list.creator_login;

【讨论】:

以上是关于即使 sql developer 中不存在数据,也返回给定员工的行的主要内容,如果未能解决你的问题,请参考以下文章

从 SQL SELECT 语句中获取值,即使它在表中不存在

CSS 模块在 Codesandbox 中不起作用 - 即使它们存在也找不到文件

即使 laravel 中不存在数据,关系仍然返回行

为啥即使数据库中不存在 id 和/或用户名我也会成功

即使数据库中不存在我如何返回特定时间的数据

即使数据库中不存在,也要显示记录