zoukankan      html  css  js  c++  java
  • MySql 自定义函数

    drop function if exists f_get_costprice;
    delimiter $$
    CREATE FUNCTION `f_get_costprice`(`incrdcode` varchar(16),`amount` decimal(10,2),`balance` decimal(10,4), `incustid` int, `optype` int) RETURNS decimal(10,4)
    NO SQL
    BEGIN

    DECLARE costPrice decimal(10,4);/*成本价*/

    /*累计买入金额*/
    declare totalInBalance decimal(10,4);

    /*债权余额*/
    declare crdAmount decimal(10,4);

    /*累计卖出金额*/
    declare totalOutBalance decimal(10,4);

    /*本次买入金额*/
    declare nowInBalance decimal(10,4);

    /*本次卖出金额*/
    declare nowOutBalance decimal(10,4);

    select TotalBuyBalance into totalInBalance from custstock where crdcode=incrdcode and custid=incustid;
    select holdamount into crdAmount from custstock where crdcode=incrdcode and custid=incustid;
    select TotalSellBalance into totalOutBalance from custstock where crdcode=incrdcode and custid=incustid;

    /*买入*/
    IF(optype = 1) THEN
    SET nowInBalance=balance;
    SET nowOutBalance=0;
    SET crdAmount=crdAmount+amount;

    ELSE
    SET nowInBalance=0;
    SET nowOutBalance=balance;
    SET crdAmount=crdAmount-amount;

    END IF;



    /*成本价 =(累计买入金额+本次买入金额-累计卖出金额-本次卖出金额)/债权余额 )*/
    SET costPrice = (IFNULL(totalInBalance,0)+IFNULL(nowInBalance,0)-IFNULL(totalOutBalance,0)-IFNULL(nowOutBalance,0))/crdAmount;

    RETURN round(costPrice,4);

    END $$
    delimiter ;
    ALTER FUNCTION f_get_costprice COMMENT '计算成本价(输入参数: 债权代码,债权数额,本次发生金额,客户ID,操作类型:1买入,2卖出)';

  • 相关阅读:
    Linux配置NTP服务器,时间同步
    个人博客:有态度的HBase/Spark/BigData
    solr 6.2.1环境搭建
    大牛博客!Spark / Hadoop / Kafka / HBase / Storm
    hive 数据导出三种方式
    hive 分区表
    hive 创建orc表
    Tomcat中JVM内存溢出及合理配置及maxThreads如何配置(转)
    oracle 日期时间函数
    02: linux命令bak
  • 原文地址:https://www.cnblogs.com/spplus/p/5488864.html
Copyright © 2011-2022 走看看