oracle SQL語句心得

oracle總結:


1、''和null在oracle中都代表null
過濾null,用''中間加空格: nvl(columnname,' ')


2.oracle小數格式化
2是保留2爲小數,0爲保留整數。round是四捨五入。trunk是截斷。
round(columnname,2)




3.取每個班的第一名,可以用rank over partion 分析函數。如果排序時間相同,可以增加一個ID排序。

sql = "select NVL(c.name,' ') channelName,NVL(c.code,' ') channelCode,NVL(sd.name,' ') channelOperator,"
+ "NVL(c.district,' ') channelArea,NVL(og.name,' ') channelOrgan,NVL(qfp.luminous_power,0) luminousPower  "
+ "from res_channel c  left join sys_organ og on og.id=c.organ_id "
+ "left join(select id,code,name from sys_dict  where dict_cat_id=(select id from sys_dict_cat where code='operator')) sd "
+ "on sd.code=c.operator  left join (select t.channel_id,t.luminous_power from (select t1.id,t1.scada_time,t1.channel_id," +
"t1.luminous_power,rank() over(partition by t1.channel_id order by t1.scada_time desc,t1.id desc) rank   " +
"from qt_fiber_perform t1) t where t.rank=1) qfp on qfp.channel_id=c.id "
+ "where c.type=:rcType";



4.分組函數和其他字段一起檢索。可以把分組結果作爲一個表。用其他表left join 分組結果。

String sql="select nvl(rc.name,' ') channelName,nvl(rc.code,' ') channelCode,nvl(sd2.name,' ') channelType,nvl(rc.district,' ')  channelDistrict," +
"sd.name channelOperator,round(rs.score,0) channelScore from res_channel rc left join (select s.code,s.name from sys_dict s where s.dict_cat_id=(select id from sys_dict_cat where code='operator')) sd " +
"on rc.operator=sd.code left join (select d.code,d.name from sys_dict d where d.dict_cat_id=(select id from sys_dict_cat where code='channel-type')) sd2 on rc.type=sd2.code left join (select r.id id,avg(q.score) score from res_channel r " +
"left join qt_channel_score q on r.id=q.channel_id group by r.id) rs on rc.id=rs.id";

5.多條件分組,可使用case when語句

String sql = "select count(*) recordCount,round(avg(case when t.scada_time>sysdate-3/24 and t.scada_time<=sysdate then t.luminous_power  end),2) avg8,"
+ "round(avg(case when t.scada_time>sysdate-6/24 and t.scada_time<=sysdate-3/24 then t.luminous_power  end),2) avg7,"
+ "round(avg(case when t.scada_time>sysdate-9/24 and t.scada_time<=sysdate-6/24 then t.luminous_power  end),2) avg6,"
+ "round(avg(case when t.scada_time>sysdate-12/24 and t.scada_time<=sysdate-9/24 then t.luminous_power  end),2) avg5,"
+ "round(avg(case when t.scada_time>sysdate-15/24 and t.scada_time<=sysdate-12/24 then t.luminous_power  end),2) avg4,"
+ "round(avg(case when t.scada_time>sysdate-18/24 and t.scada_time<=sysdate-15/24 then t.luminous_power  end),2) avg3,"
+ "round(avg(case when t.scada_time>sysdate-21/24 and t.scada_time<=sysdate-18/24 then t.luminous_power  end),2) avg2,"
+ "round(avg(case when t.scada_time>=sysdate-1 and t.scada_time<=sysdate-21/24 then t.luminous_power  end),2) avg1 "
+ "from qt_fiber_perform t where t.channel_id=:rcId";

6.oracle日期格式和JAVA日期格式有所區別

select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss') from dual//分鐘,必須用mi;hh是12小時制,hh24是24小時制

SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//MM必須大寫,hh小寫爲12小時制,HH大寫爲24小時制

7.distinct後面跟多個字段,只有多個字段都相同時才消除,distinct只能寫在字段最前面

select distinct name,age from user;

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章