您的位置:首页 > 数据库 > Oracle

oracle SQL语句心得

2015-11-25 14:00 459 查看
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小时制
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: