iT邦幫忙

4

十三、無中生有(Oracle SQL 2013/09/28)

資料庫的架構,有就是有、無還是無,如何從報表上生出”無”的資料,
就像今天是國定假日2013/9/28,報表上如何顯出未刷卡的日子,並顯示NULL。
這個月好多假日,刷卡明細表上列出來是這樣[簡單舉例],9/15以前略過不提
日期 卡號 刷卡否

2013/9/16 102001 V
2013/9/17 102001 V
2013/9/24 102001 V

9/18-9/20 連假不用刷卡
9/23 請假未刷
9/25-9/30 出差未刷

報表則要這樣顯示
日期 卡號 刷卡否

2013/9/16 102001 V
2013/9/17 102001 V
2013/9/18 102001 NULL
2013/9/19 102001 NULL
2013/9/20 102001 NULL
2013/9/21 102001 NULL
2013/9/22 102001 NULL
2013/9/23 102001 NULL
2013/9/24 102001 V
2013/9/25 102001 NULL
2013/9/26 102001 NULL
2013/9/27 102001 NULL
2013/9/28 102001 NULL
2013/9/29 102001 NULL
2013/9/30 102001 NULL

如果是用Pl/Sql 先補資料再出報表,那就不用說了。

create table emp_card
( card_date date,
  card_id number,
  card_flag  varchar2(5));

insert into emp_card (card_date, card_id, card_flag) values (‘2013/9/16’, 102001, ‘V’);
insert into emp_card (card_date, card_id, card_flag) values (‘2013/9/17’, 102001, ‘V’);
insert into emp_card (card_date, card_id, card_flag) values (‘2013/9/24’, 102001, ‘V’);
commir;

原本結果
select card_date, card_id, card_flag from emp_card;

希望呈現效果的SQL
select d_all.m_date, ec.card_id, nvl(ec.card_flag, ‘NULL’) card_flag
  from (select trunc(sysdate, ‘mm’) + rownum - 1  m_date
              from all_tables
            where rownum <= to_number(to_char(sysdate, ‘dd’))   ) d_all,
           emp_card  ec
 where d_all.m_date = ec.card_date(+);

p.s outter join 可能不會顯示正常結果,可能還要等上班後測試修改一下。
例如:先把兩個日期抓出來union後,再來join。

[開發技術組]全文閱讀
http://ithelp.ithome.com.tw/ironman6/player/yafuu168/dev/1
[鐵人人生組]全文閱讀
http://ithelp.ithome.com.tw/ironman6/player/yafuu168/life/1


圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 則留言

0
pajace2001
iT邦研究生 1 級 ‧ 2013-09-28 21:57:52

沙發
拍手

月半車甫 iT邦研究生 3 級 ‧ 2013-10-25 14:07:15 檢舉

忘了回應本篇了

感謝!謝謝

我要留言

立即登入留言