1. 程式人生 > >oracle資料庫計算兩個時間型別欄位值的時間差,並轉換為合適的時間格式顯示(按時分秒展示)

oracle資料庫計算兩個時間型別欄位值的時間差,並轉換為合適的時間格式顯示(按時分秒展示)

1.背景

   資料庫表名為tablename。其中兩個欄位為startdate(開始時間),closedate(結束時間)。


2.需求

   建立試圖,查詢出間隔時間並顯示為自定義的格式。


3.sql語句


 select t.*, round(to_number(t.closedate-t.startdate)*24*60*60)||'秒' "線上時長(秒)",

(

decode((trunc(to_number(t.closedate-t.startdate)*24)),null,null,0,null,trunc(to_number(t.closedate-t.startdate)*24)||'時')

||

decode((trunc(mod((to_number(t.closedate-t.startdate)*24*60),60))),null,null,0,null,trunc(mod((to_number(t.closedate-t.startdate)*24*60),60))||'分')

||

decode((trunc(mod((to_number(t.closedate-t.startdate)*24*60*60),60))),null,'無',trunc(mod((to_number(t.closedate-t.startdate)*24*60*60),60))||'秒'))                                                                 "登入時長(顯示為   **時**分**秒  的形式)",


(select count(*)  from tablename b where b.userbiscode=t.userbiscode and to_char(b.startdate,'yyyy-mm-dd')=to_char(sysdate,'yyyy-mm-dd'))                                                          "今日登入次數",

(select closedate from 

     (select a.closedate,rownum as rn  from  

       (select closedate from tablename where to_char(startdate,'yyyy-mm-dd')=to_char(sysdate,'yyyy-mm-dd') order            by startdate desc)    a) 

              where rn<2)                                                                                   "最後登入時間"

from tablename t 

        WHERE to_char(t.startdate,'yyyy-mm-dd')=to_char(sysdate,'yyyy-mm-dd') order by t.startdate desc;