報表表樣多excel表格與sql查詢結果一一匹配_order by decode and 中間表

wisdomone1發表於2013-03-05

 前言:報表是根據地區歸屬分為多個excel表格來填寫的。而有些歸屬地的表格可能
       根據報表統計無資料。但為了sql查詢與excel表格一一匹配對應;這樣填充
       報表時可以順序進行,不用交叉檢查匹配,節省了大量時間;但如何令未
       查詢出來的歸屬也在sql查詢中顯示一行呢;
 正文:
 
 --構建測試               a為歸屬地      b為指標      
SQL> create table t_match(a varchar2(100),b int);
 
Table created
--插入示例資料
SQL> insert into t_match values('渭南市',1);
 
1 row inserted
 
SQL> insert into t_match values('寶雞市',2);
 
1 row inserted
 
SQL> insert into t_match values('咸陽市',3);
 
1 row inserted
 
SQL> insert into t_match values('商洛市',4);
 
1 row inserted
 
SQL> commit;
 
Commit complete
--檢視示例表資料
SQL> select * from t_match;
 
A                                                                                                                      B
-------------------------------------------------------------------------------- ---------------------------------------
渭南市                                                                                                                 1
寶雞市                                                                                                                 2
咸陽市                                                                                                                 3
商洛市                                                                                                                 4


--實現的效果大致如下:
A                 B
---------------------
渭南市            1           
咸陽市            3
漢中市            NULL
商洛市            4
寶雞市            2

--分析
1,按指定的列值排序,可採用order bydecode(a,'',1,'',2)
2, 如何把漢中市合併顯示在查詢結果中
3,看這個樣子,一個表難以實現;藉助中間表,
  此表結構為:
  create table t_map(a varchar2(100),orig_col int);
  create sequence seq_map start with 1;
  insert into t_map values('渭南市',seq_map.nextval);
  insert into t_map values('咸陽市',seq_map.nextval);
  insert into t_map values('漢中市',seq_map.nextval);
  insert into t_map values('商洛市',seq_map.nextval);
  insert into t_map values('寶雞市',seq_map.nextval);
  commit;
 
 
  --執行符合查詢結果的sql
  select t_map.a,
         t_match.b
  from t_match,
       t_map
  where t_match.a(+)=t_map.a
  order by t_map.orig_col;
       
--附上最終執行結果
SQL>   select t_map.a,
  2           t_match.b
  3    from t_match,
  4         t_map
  5    where t_match.a(+)=t_map.a
  6    order by t_map.orig_col;
 
A                                                                                                                      B
-------------------------------------------------------------------------------- ---------------------------------------
渭南市                                                                                                                 1
咸陽市                                                                                                                 3
漢中市                                                                          
商洛市                                                                                                                 4
寶雞市                                                                                                                 2
        

--如果僅是單表且報表表樣的excel表格不太多的情況下
--此法的優點:不用建立中間表
        缺點:可能有些歸屬地查詢無結果,與報表表樣不一一對應,需要部分進行核對填寫,稍多費一點人力
可採用如下sql
select a,b
from t_match
order by decode(a,'渭南市',1,'咸陽市',2,'漢中市',3,'商洛市',4,'寶雞市',5);
        

小結:1,中間表

         2,左連線

         3,order by

          4,decode

           5,decode轉換值各為序列值1,2,3

           6,序列(其實與5道理相同)

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

相關文章