Oracle returns NULL after join

对employees表和department表进行分组后,再连接;与连接分组,再连接返回的结果不同.

要求:查询每个部门有多少人,包含没有部门的人,同时列出部门ID,部门name,最后的结果如下

DEPARTMENT_ID DEPARTMENT_NAME                    COUNTS
------------- ------------------------------ ----------
          100 Finance                                 6
           30 Purchasing                              6
                                                      1
           20 Marketing                               2
           70 Public Relations                        1
           90 Executive                               3
          110 Accounting                              2
           50 Shipping                               45
           40 Human Resources                         1
           80 Sales                                  34
           60 IT                                      5

 

测试1

(1)建立实体子表,拆开测试,为正常返回值

select count(*) counts,department_id from employees group by department_id;
    COUNTS DEPARTMENT_ID

---------- -------------

         6           100

         6            30

         1

         2            20

         1            70

         3            90

         2           110

        45            50

         1            40

        34            80

         5            60


11 rows selected.


create table D2 as select * from (select count(*) counts,department_id from employees group by department_id);
Table created.


17-MAR-12 HR @ hdb>desc D2;

 Name                                     Null?    Type

 -------------------------------------------------------------------------

 COUNTS                                            NUMBER

 DEPARTMENT_ID                                     NUMBER(4)


select D2.department_id,D.department_name,D2.counts from D2 left join departments D on D2.department_id = D.department_id;
DEPARTMENT_ID DEPARTMENT_NAME                    COUNTS

------------- ------------------------------ ----------

          100 Finance                                 6

           30 Purchasing                              6

                                                      1

           20 Marketing                               2

           70 Public Relations                        1

           90 Executive                               3

          110 Accounting                              2

           50 Shipping                               45

           40 Human Resources                         1

           80 Sales                                  34

           60 IT                                      5


11 rows selected.

(2),D2表为虚拟表时,含有NULL的行丢失.

select D2.department_id,D.department_name,D2.counts from (select count(*) counts,
department_id
from employees
group by department_id) D2left join departments D
on D2.department_id = D.department_id;
DEPARTMENT_ID DEPARTMENT_NAME                    COUNTS

------------- ------------------------------ ----------
           80 Sales                                  34
           50 Shipping                               45
           20 Marketing                               2
           90 Executive                               3
           70 Public Relations                        1
           60 IT                                      5
          110 Accounting                              2
           30 Purchasing                              6
           40 Human Resources                         1
          100 Finance                                 6

10 rows selected.

 

测试2

当在统计发生在连接的时候,再进行连接,结果正常.

select t1.department_id,
d1.department_name,
t1.counts
from (select e.department_id,count(*) counts from employees e left join departments d on e.department_id = d.department_id group by e.department_id) t1
left join departments d1
on t1.department_id = d1.department_id;
DEPARTMENT_ID DEPARTMENT_NAME                    COUNTS

------------- ------------------------------ ----------
          100 Finance                                 6
           30 Purchasing                              6
                                                      1
           20 Marketing                               2
           70 Public Relations                        1
           90 Executive                               3
          110 Accounting                              2
           50 Shipping                               45
           40 Human Resources                         1
           80 Sales                                  34
           60 IT                                      5

11 rows selected.

其中的子查询t1如下

select e.department_id,count(e.employee_id) countsfrom employees eleft join departments d on e.department_id = d.department_id group by e.department_id;
DEPARTMENT_ID          COUNTS                

---------------------- ----------------------

100                    6                     

30                     6                     

                       1                     

20                     2                     

70                     1                     

90                     3                     

110                    2                     

50                     45                    

40                     1                     

80                     34                    

60                     5                     

11 rows selected

 

此时下面的查询和t1的结果一样,但无法和departments连接成功,请问这个是为什么呢?

select e.department_id,count(e.employee_id) countsfrom employees egroup by e.department_id;
DEPARTMENT_ID          COUNTS                

---------------------- ----------------------

100                    6                     

30                     6                     

                       1                     

20                     2                     

70                     1                     

90                     3                     

110                    2                     

50                     45                    

40                     1                     

80                     34                    

60                     5                     

11 rows selected

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值