SQL 筆試題

楊奇龍發表於2010-07-12

一個筆試題:

sql語句的問題

SQL> select * from test2;

         J          F
---------- ----------
         3          5
         4          5
         5          5

SQL> select * from test3;

         K          Y
---------- ----------
         5          0
         6          0
我需要得到的結果是

         DD         F             Y
---------- ----------   ----------
         3          5
         4          5
         5          5             0
         6                         0

 

 

SQL> insert into test2  values (3,5);
已建立 1 行。
SQL> insert into test2  values (4,5);
已建立 1 行。
SQL> insert into test2  values (5,5);
已建立 1 行。
SQL> create table test3 (k number,y number);

表已建立。
SQL> select * from test2;
         J          F                                                          
---------- ----------                                                          
         3          5                                                          
         4          5                                                          
         5          5                                                          

SQL> insert into test3  values (5,0);
已建立 1 行。
SQL> insert into test3  values (6,0);
已建立 1 行。
SQL> select * from test3;
         K          Y                                                          
---------- ----------                                                          
         5          0                                                          
         6          0                                                          

SQL> commit;
提交完成。

-------------錯解------------
----使用union
SQL> select j DD,f,to_number(null) from test2
  2  union
  3  select k  ,to_number(null),y from test3;

        DD          F TO_NUMBER(NULL)                                          
---------- ---------- ---------------                                          
         3          5                                                          
         4          5                                                          
         5          5                                                          
         5                          0                                          
         6                          0                                         

-------------使用union all
SQL> select j DD,f,to_number(null) y from test2
  2  union all
  3  select k  ,to_number(null),y from test3;

        DD          F          Y                                               
---------- ---------- ----------                                               
         3          5                                                          
         4          5                                                          
         5          5                                                          
         5                     0                                               
         6                     0         

題目的意思是按test2.j=test3.k做full join
我寫的肯定結果一樣,因為沒有重複行
                                       
 ------------正解-----------

SQL> select nvl(j,k) DD ,f,y from test2 full join test3 on j=k;
        DD          F          Y                                               
---------- ---------- ----------                                               
         3          5                                                          
         4          5                                                          
         5          5          0                                               
         6                      0
                                               
-------------------------------------

select dd,max(f),max(y)
from
(select j DD,f,to_number(null) y from test2
union
select k  ,to_number(null),y from test3)
group by dd;-------------風鈴中の鬼提供!

-

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/22664653/viewspace-667852/,如需轉載,請註明出處,否則將追究法律責任。

相關文章