Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/oracle/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 即使没有匹配的记录,也获取零_Sql_Oracle_Oracle10g - Fatal编程技术网

Sql 即使没有匹配的记录,也获取零

Sql 即使没有匹配的记录,也获取零,sql,oracle,oracle10g,Sql,Oracle,Oracle10g,我在EMP_DETAILS表中有数据 EMPLOYEE_NUMBER ROLE NSA5421 CONTRACTOR NSA390 CONTRACTOR E8923 EMPLOYEE E2390 EMPLOYEE 我想有零显示,即使没有记录,我尝试了以下,但没有得到理想的结果 SELECT CASE WHEN cnt IS NULL THEN 0 ELSE cnt EN

我在EMP_DETAILS表中有数据

EMPLOYEE_NUMBER      ROLE
NSA5421              CONTRACTOR
NSA390               CONTRACTOR
E8923                EMPLOYEE
E2390                EMPLOYEE
我想有零显示,即使没有记录,我尝试了以下,但没有得到理想的结果

SELECT  CASE WHEN cnt IS NULL THEN 0 ELSE cnt END cnt,
        CASE WHEN role IS NULL THEN '0' ELSE role END role
FROM
(SELECT   COUNT (*) cnt, role
     FROM   emp_details
    WHERE   employee_number = 'E3400'
GROUP BY   role)
请尝试:

select 
  NVL(y."ROLE", '0') "ROLE",  
  case when y."ROLE" is null then 0 else  COUNT(*) end cnt 
from(
      SELECT 'E3400' employee_number from dual
  )x left join emp_details y on x.employee_number=y.employee_number
group by "ROLE"
请尝试:

select 
  NVL(y."ROLE", '0') "ROLE",  
  case when y."ROLE" is null then 0 else  COUNT(*) end cnt 
from(
      SELECT 'E3400' employee_number from dual
  )x left join emp_details y on x.employee_number=y.employee_number
group by "ROLE"

您的内部查询只返回空值,而不返回任何内容

SELECT   COUNT (*) cnt, role
     FROM   emp_details
    WHERE   employee_number = 'E3400'
GROUP BY   role

Use EXISTS子句

您的内部查询只返回空值,而不返回任何内容

SELECT   COUNT (*) cnt, role
     FROM   emp_details
    WHERE   employee_number = 'E3400'
GROUP BY   role
SELECT  CASE WHEN cnt IS NULL THEN 0 ELSE cnt END cnt,
        CASE WHEN role IS NULL THEN '0' ELSE role END role
FROM
(SELECT   COUNT (*) cnt, role
     FROM   emp_details
    WHERE   employee_number = 'E3400'
GROUP BY   ROLE
UNION
SELECT NULL, NULL  FROM dual
WHERE NOT EXISTS
     ( SELECT   COUNT (*) cnt, role
     FROM   emp_details
    WHERE   employee_number = 'E3400'
GROUP BY   ROLE
     )
);
用另一种方式使用EXISTS子句

SELECT  CASE WHEN cnt IS NULL THEN 0 ELSE cnt END cnt,
        CASE WHEN role IS NULL THEN '0' ELSE role END role
FROM
(SELECT   COUNT (*) cnt, role
     FROM   emp_details
    WHERE   employee_number = 'E3400'
GROUP BY   ROLE
UNION
SELECT NULL, NULL  FROM dual
WHERE NOT EXISTS
     ( SELECT   COUNT (*) cnt, role
     FROM   emp_details
    WHERE   employee_number = 'E3400'
GROUP BY   ROLE
     )
);
SELECT a.role,
       count(b.role)
  FROM emp_details a left outer join emp_details b
    on a.employee_number = b.employee_number
   and a.employee_number = 'E3400'
 group by a.role;
输出:

|      ROLE  | COUNT(B.ROLE)  |
-------------|---------- -----|
| CONTRACTOR |              0 |
|   EMPLOYEE |              0 |
.

还有另一种方法

SELECT a.role,
       count(b.role)
  FROM emp_details a left outer join emp_details b
    on a.employee_number = b.employee_number
   and a.employee_number = 'E3400'
 group by a.role;
输出:

|      ROLE  | COUNT(B.ROLE)  |
-------------|---------- -----|
| CONTRACTOR |              0 |
|   EMPLOYEE |              0 |

.

根据您的数据,员工编号为“E3400”,则没有返回记录=)根据您的数据,员工编号为“E3400”,则没有返回记录=)显示除“E3400”以外的员工编号的2条记录。@techdo,已更新,请查看。显示除“E3400”以外的员工编号的2条记录。@techdo,已更新,请复习。