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小时制
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小时制
相关文章推荐
- Oracle表空间管理
- oracle 11g中sql plus使用
- SecureCRT连接Oracle VM VirtualBox虚拟机ubuntu实测通过有图有真相
- 使用database control配置数据库时 要求在当前oracle主目录中配置监听程序
- Oracle行列互换 横表和纵表
- oracle 先安装客户端再安装服务端导致的tns无法解析错误--2015.11.25
- oracle 11g体系结构
- Oracle查看当天创建的所有表
- ORACLE 创建自增序列生成流水号
- 在64位SQL Server中创建Oracle的链接服务器
- TRANSLATE 函数
- Oracle 11g必须开启的服务及服务详细介绍
- 本人对Oracle Bill Of Material模块的一些了解
- Oracle 树操作(select…start with…connect by…prior)
- oracle 11g安装后的配置及使用
- Oracle数据库的关闭与打开(环境为sqlplus)
- Oracle direct path read 事件解释
- Oracle 修改字段类型和长度
- Oracle中文汉字占用字节
- Oraclesys和system用户、sysdba和sysoper系统权限、sysdba和dba角色的区别