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;


猜你喜欢

转载自blog.csdn.net/miluli1/article/details/79617097
今日推荐