易截截图软件、单文件、免安装、纯绿色、仅160KB

关于SQL avg 小数点位数的截取

在取一个平均值的时候往往遇到小数点位数的截取。
前几天遇到过这么个问题,在KingDee EAS 的查询分析器里面如果求某一物料的平均单价时我这样写道:
select avg = case sum(bentry.FQuantity) when 0 then 0 else (sum(bentry.famount)/sum(bentry.FQuantity)) end,
此语句看似没什么问题,毕竟是求平均值,可是如果要在页面显示小数点后两位时要怎么做呢?此报表不是BI样式,就在sql中如何实现?
正常情况下可以考虑的解决方案由很多,如:
cast( Round(A.iPrice,2) as varchar(50)) AS 单价
cast( Round(A.iPrice,0,1) as varchar(50)) AS 单价
cast(0.329999999999999   as   numeric(5,2)或者decimal)
但是很抱歉 KingDee EAS 6.0 里面我发现不能使用 cast ,round ,而cast  *******
 as numberic /decimal 就更不能用了。
还可以考虑substing()函数,concat()函数之类的。后来发现Oracle DB 里面有个关键性的函数:
charindex很管用于是写了段sql如下:
avg = case charindex('.',"tableName.avgprice ) when 0 then tableName.avgprice else substring(tableName.avgprice,0,charindex('.',tableName.avgprice)+2) end
如果平均值为整数而没有'.'存在时也能很好的解决。这就是搭配case 和charindex的作用于好处!
为了方便对这一SQL进行了整理。
 
/**
     * 截取小数点后两位
     * @author Kobe Bryant24
     * @param strsql
     * @param tableName
     * @return
     */
    protected String filtrateFloatValue(String strsql,String tableName){
       return strsql+"=case charindex('.'," +
       tableName+"."+strsql+") when 0 then " +
       tableName+"."+strsql+" else substring(" +
       tableName+"."+strsql+",0,charindex('.'," +
       tableName+"."+strsql+")+2) end" ;
    }
 
 


相关文档:

[收拢] 用sqlite 执行标准 sql 语法

http://www.umgr.com/blog/PostView.aspx?bpId=36294
 1. 执行sql语句
int sqlite3_exec(sqlite3*, const char *sql, sqlite3_callbacksql 语法
, void *,  char **errmsg );
这就是执行一条 sql 语句的函数。
第1个参数不再说了,是前面open函数得到的指针。说了是关键数据结构。
第2个参数const char ......

SQL SERVER 分页存储过程

存储过程代码:
 --drop procedure p_page
--go
create procedure p_page
(
@Tables varchar(1000), --表名如testtable
@PrimaryKey varchar(100),--表的主键,必须唯一性
@Sort varchar(200) = NULL,--排序字段如f_Name asc或f_name desc(注意只能有一个排序字段)
@CurrentPage int = 1,--当 ......

SQL Server 2005中应关注的30个知识点

<!--[if !supportLists]-->一、<!--[endif]-->SQL Server 2005数据库管理的10个最重要特点
<!--[if !supportLists]-->1.         <!--[endif]-->数据库镜像
通过新数据库镜像方法,将记录档案传送性能进行延伸。您将可以使用数据库镜像,通过将自动失效 ......

测量sql 解析耗费的时间比例

清空共享池,关闭,重启数据库
SQL> shutdown immediate;
SQL> startup;
开启 时间统计
SQL> set timi on
SQL> select count(*) from dba_objects;
  COUNT(*)
----------
     11308
Elapsed: 00:00:00.19
SQL> /
  COUNT(*)
----------
   &nb ......

pl/sql 实现归并算法

在网上看到关于归并算法,没有数据库级的,当然数据级的算法对于数据库来说就是一个排序。今天写一个pl/sql的归并算法。
归并的算法很容易理解,网上很详细,现只把源码贴出来。
(包声明部分)
CREATE OR REPLACE PACKAGE PG_MERGESORT IS
  -- Author  : wealth_khb@126.com
  -- Created : 2009-10- ......
© 2009 ej38.com All Rights Reserved. 关于E健网联系我们 | 站点地图 | 赣ICP备09004571号