sql 日结

--生成日结数据

====================================
-- Author:        <Author,,Name>
-- Create date: <Create Date,,>
-- Description: 资金日结
-- =============================================
ALTER PROCEDURE [UC].[P_Fin_DailySummary_Insert]
AS
      BEGIN TRY
        SET NOCOUNT ON ;
        DECLARE @ErrorID INT= 0
        DECLARE @vcExecInfo VARCHAR(1)=‘‘
        SET NOCOUNT ON ;
        DECLARE @dtDate VARCHAR(15)= CONVERT(VARCHAR(15), DATEADD(day, ( -1 ), GETDATE()), 23)
        DECLARE @dtYesterdayDate VARCHAR(15)= CONVERT(VARCHAR(15), DATEADD(day, ( -2 ), GETDATE()), 23)
        DECLARE @dtTodayDate VARCHAR(15)= CONVERT(VARCHAR(15), DATEADD(day, ( 0 ), GETDATE()), 23)

        --DELETE UC.Fin_DailySummary
        --WHERE dtBalanceDate = @dtDate

        INSERT INTO UC.Fin_DailySummary ( dtBalanceDate, vcCustomerNo, vcLoginId, nCustomerType, dmYesterdayBalance, dmTodayBalance, dmRechargeAmount, dmWithDrawAmount, dmSellGoldAmount, dmBuyGoldAmount, dmGoldCompensate, dmWithDrawFee, dmRechargeFee, dmGoldSellFee, dmAdvertAmount, dmRechargeAdjust, dmWithDrawAdjust, dmGoldOrderAdjust )
  SELECT @dtDate,  a.vcCustomerNo, a.vcUserName,a.nCustomerType,0 AS dmYesterdayBalance,ISNULL(a.dmCurrentFund-ISNULL(c.dmFund,0),0) AS dmTodayBalance, ISNULL(b.dmRechargeAmount, 0) AS dmRechargeAmount, ISNULL(b.dmWithDrawAmount, 0) AS dmWithDrawAmount,ISNULL(b.dmSellGoldAmount, 0) AS dmSellGoldAmount, ISNULL(b.dmBuyGoldAmount, 0) AS dmBuyGoldAmount,  ISNULL(b.dmGoldCompensate, 0) AS dmGoldCompensate, ISNULL(b.dmWithdrawFee, 0) AS dmWithDrawFee, ISNULL(b.dmRechargeFee, 0) AS dmRechargeFee,0 AS dmGoldSellFee,0 AS dmAdvertAmount,ISNULL(b.dmRechargeAdjust, 0) AS dmRechargeAdjust,ISNULL(b.dmWithDrawAdjust, 0) AS dmWithDrawAdjust,ISNULL(b.dmGoldOrderAdjust, 0) AS dmGoldOrderAdjust
                  FROM      UC.Cus_Customer AS a
                            LEFT JOIN ( SELECT  vcCustomerNo,
                            ISNULL(SUM(CASE nFundType
                                       WHEN 10 THEN dmFund
                                       ELSE 0 END), 0) AS dmRechargeAmount,
                            ISNULL(SUM(CASE nFundType
                                       WHEN 14 THEN dmFund
                                       ELSE 0 END), 0) AS dmWithDrawAmount,
                            ISNULL(SUM(CASE nFundType
                                       WHEN 22 THEN dmFund
                                       ELSE 0 END), 0) AS dmBuyGoldAmount,
                            ISNULL(SUM(CASE nFundType
                                       WHEN 23 THEN dmFund
                                       ELSE 0 END), 0) AS dmSellGoldAmount,
                            ISNULL(SUM(CASE nFundType
                                       WHEN 17 THEN dmFund
                                       ELSE 0 END), 0) AS dmGoldCompensate,
                            ISNULL(SUM(CASE nFundType
                                       WHEN 11 THEN dmFund
                                       ELSE 0 END), 0) AS dmRechargeFee,
                            ISNULL(SUM(CASE nFundType
                                       WHEN 18 THEN dmFund
                                       ELSE 0 END), 0) AS dmWithdrawFee,
                            ISNULL(SUM(CASE nAdjustType
                                       WHEN 11 THEN dmFund
                                       ELSE 0 END), 0) AS dmRechargeAdjust,
                            ISNULL(SUM(CASE nAdjustType
                                       WHEN 12 THEN dmFund
                                       ELSE 0 END), 0) AS dmWithDrawAdjust,
                            ISNULL(SUM(CASE nAdjustType
                                       WHEN 13 THEN dmFund
                                       ELSE 0 END), 0) AS dmGoldOrderAdjust
                    FROM    UC.Fin_CustomerFundJour
                    WHERE   CONVERT(VARCHAR(10),dtCreateDate,120) = @dtDate
                    GROUP BY vcCustomerNo ) AS b
                    ON a.vcCustomerNo = b.vcCustomerNo
                    LEFT JOIN (SELECT SUM(dmFund) AS dmFund,vcCustomerNo
                               FROM UC.Fin_CustomerFundJour
                               WHERE CONVERT(VARCHAR(10),dtCreateDate,120) = @dtTodayDate
                               GROUP BY vcCustomerNo) AS c
                    ON a.vcCustomerNo = c.vcCustomerNo                       

        --修改前日余额
        UPDATE  Fin_DailySummary
        SET     dmYesterdayBalance = c.dmTodayBalance
        FROM    Fin_DailySummary AS a ,
                ( SELECT    b.dmTodayBalance, b.vcCustomerNo
                  FROM      Fin_DailySummary AS b
                  WHERE     b.dtBalanceDate = @dtYesterdayDate ) c
        WHERE   a.vcCustomerNo = c.vcCustomerNo  AND a.dtBalanceDate = @dtDate                

    END TRY
     BEGIN CATCH
        IF @@trancount > 0
            ROLLBACK TRAN ;
        EXEC P_Pub_GetErrorMsgExecInfo @ErrorID OUT,@vcExecInfo OUT
    END CATCH

<--------------------------------------------------------------------分割线-------------------------------------------------------------------------------------->

--日结数据查询

-- =============================================-- Author:        <Author,,Name>
-- Create date: <Create Date,,>
-- Description:    客户资金日结查询
-- =============================================
ALTER PROCEDURE [UC].[P_Admin_Fin_DailySummary_Search]
    @vcBeginDate VARCHAR(50) ,
    @vcEndDate VARCHAR(50) ,
    @nCustomerType INT ,
    @vcSearValue VARCHAR(100) ,
    @nPageIndex INT ,
    @nPageSize INT ,
    @nTotleRecord INT OUT ,
    @nPageCount INT OUT
AS
    BEGIN
        SET NOCOUNT ON ;
        DECLARE @nRowStart INT
        DECLARE @nRowEnd INT
        DECLARE @vcSearchBeginDate VARCHAR(50)
        DECLARE @vcSearchEndDate VARCHAR(50)
        SET @nRowEnd = @nPageIndex * @nPageSize
        SET @nRowStart = @nRowEnd - @nPageSize + 1 

        --设置默认值
        IF @vcBeginDate = ‘‘
            SET @vcBeginDate = CONVERT(VARCHAR(10), GETDATE() - 1, 120)
        IF @vcEndDate = ‘‘
            SET @vcEndDate = CONVERT(VARCHAR(10), GETDATE(), 120)

        SET @vcSearchBeginDate = @vcBeginDate
        SET @vcSearchEndDate = @vcEndDate

        SELECT  *
        FROM    ( SELECT    ROW_NUMBER() OVER ( ORDER BY vcCustomerNo DESC ) AS RowIndex, *
                  FROM      UC.Fin_DailySummary
                  WHERE     ( @vcSearValue = ‘‘ OR ( vcLoginId = @vcSearValue OR vcCustomerNo = @vcSearValue ) ) AND ( @nCustomerType = 0 OR nCustomerType = @nCustomerType ) AND dtBalanceDate BETWEEN @vcBeginDate AND @vcEndDate ) AS cte
        WHERE   RowIndex BETWEEN @nRowStart AND @nRowEnd 

        IF NOT EXISTS ( SELECT  1
                        FROM    UC.Fin_DailySummary
                        WHERE   CONVERT(VARCHAR(10), dtBalanceDate, 120) = @vcSearchBeginDate )
            BEGIN
                --如果不存在按照开始日期查询的数据 取数据库最开始的时间
                SELECT  DISTINCT TOP 1
                        @vcSearchBeginDate = CONVERT(VARCHAR(10), dtBalanceDate, 120)
                FROM    UC.Fin_DailySummary
                ORDER BY CONVERT(VARCHAR(10), dtBalanceDate, 120) ASC
            END
        IF NOT EXISTS ( SELECT  1
                        FROM    UC.Fin_DailySummary
                        WHERE   CONVERT(VARCHAR(10), dtBalanceDate, 120) = @vcSearchEndDate )
            BEGIN
                --如果不存在按照结束日期查询的数据  取数据库最新的时间
                SELECT  DISTINCT TOP 1
                        @vcSearchEndDate = CONVERT(VARCHAR(10), dtBalanceDate, 120)
                FROM    UC.Fin_DailySummary
                ORDER BY CONVERT(VARCHAR(10), dtBalanceDate, 120) DESC
            END

        IF @nCustomerType = 0
            BEGIN
                --查询所有客户分类时

                --按照客户分类汇总信息
                SELECT  CASE nCustomerType
                          WHEN 10 THEN ‘正式客户合计‘ + CONVERT(VARCHAR(10), COUNT(1)) + ‘条‘
                          WHEN 11 THEN ‘测试客户合计‘ + CONVERT(VARCHAR(10), COUNT(1)) + ‘条‘
                          WHEN 12 THEN ‘内部客户合计‘ + CONVERT(VARCHAR(10), COUNT(1)) + ‘条‘
                        END AS vcCustomerNo, SUM(dmRechargeAmount) AS dmRechargeAmount, SUM(dmWithDrawAmount) AS dmWithDrawAmount, SUM(dmSellGoldAmount) AS dmSellGoldAmount, SUM(dmBuyGoldAmount) AS dmBuyGoldAmount, SUM(dmGoldCompensate) AS dmGoldCompensate, SUM(dmWithDrawFee) AS dmWithDrawFee, SUM(dmRechargeFee) AS dmRechargeFee, SUM(dmGoldSellFee) AS dmGoldSellFee, SUM(dmAdvertAmount) AS dmAdvertAmount, SUM(dmRechargeAdjust) AS dmRechargeAdjust, SUM(dmWithDrawAdjust) AS dmWithDrawAdjust, SUM(dmGoldOrderAdjust) AS dmGoldOrderAdjust, nCustomerType
                INTO    #summary
                FROM    UC.Fin_DailySummary
                WHERE   ( @vcSearValue = ‘‘ OR ( vcLoginId = @vcSearValue OR vcCustomerNo = @vcSearValue ) ) AND dtBalanceDate BETWEEN @vcBeginDate AND @vcEndDate
                GROUP BY nCustomerType

                --汇总所有的记录
                SELECT  ‘合计‘ + CONVERT(VARCHAR(10), COUNT(1)) + ‘条‘ AS vcCustomerNo, SUM(dmRechargeAmount) AS dmRechargeAmount, SUM(dmWithDrawAmount) AS dmWithDrawAmount, SUM(dmSellGoldAmount) AS dmSellGoldAmount, SUM(dmBuyGoldAmount) AS dmBuyGoldAmount, SUM(dmGoldCompensate) AS dmGoldCompensate, SUM(dmWithDrawFee) AS dmWithDrawFee, SUM(dmRechargeFee) AS dmRechargeFee, SUM(dmGoldSellFee) AS dmGoldSellFee, SUM(dmAdvertAmount) AS dmAdvertAmount, SUM(dmRechargeAdjust) AS dmRechargeAdjust, SUM(dmWithDrawAdjust) AS dmWithDrawAdjust, SUM(dmGoldOrderAdjust) AS dmGoldOrderAdjust
                INTO    #allsummary
                FROM    UC.Fin_DailySummary
                WHERE   ( @vcSearValue = ‘‘ OR ( vcLoginId = @vcSearValue OR vcCustomerNo = @vcSearValue ) ) AND dtBalanceDate BETWEEN @vcBeginDate AND @vcEndDate

                --按照客户分类 汇总前日余额
                SELECT  SUM(dmYesterdayBalance) AS dmYesterdayBalance, nCustomerType
                INTO    #yesterday
                FROM    UC.Fin_DailySummary
                WHERE   ( @vcSearValue = ‘‘ OR ( vcLoginId = @vcSearValue OR vcCustomerNo = @vcSearValue ) ) AND CONVERT(VARCHAR(10), dtBalanceDate, 120) = @vcSearchBeginDate
                GROUP BY nCustomerType 

                --按照客户分类 汇总当日余额
                SELECT  SUM(dmTodayBalance) AS dmTodayBalance, nCustomerType
                INTO    #today
                FROM    UC.Fin_DailySummary
                WHERE   ( @vcSearValue = ‘‘ OR ( vcLoginId = @vcSearValue OR vcCustomerNo = @vcSearValue ) ) AND CONVERT(VARCHAR(10), dtBalanceDate, 120) = @vcSearchEndDate
                GROUP BY nCustomerType  

                --汇总所有客户的前日余额
                SELECT  SUM(dmYesterdayBalance) AS dmYesterdayBalance
                INTO    #allyesterday
                FROM    UC.Fin_DailySummary
                WHERE   ( @vcSearValue = ‘‘ OR ( vcLoginId = @vcSearValue OR vcCustomerNo = @vcSearValue ) ) AND CONVERT(VARCHAR(10), dtBalanceDate, 120) = @vcSearchBeginDate

                --汇总所有客户的当日余额
                SELECT  SUM(dmTodayBalance) AS dmTodayBalance
                INTO    #alltoday
                FROM    UC.Fin_DailySummary
                WHERE   ( @vcSearValue = ‘‘ OR ( vcLoginId = @vcSearValue OR vcCustomerNo = @vcSearValue ) ) AND CONVERT(VARCHAR(10), dtBalanceDate, 120) = @vcSearchEndDate

                --返回结果(根据客户分类汇总的记录以及所有客户的汇总记录)
                SELECT  #summary.vcCustomerNo, #yesterday.dmYesterdayBalance, #today.dmTodayBalance, #summary.dmRechargeAmount, #summary.dmWithDrawAmount, #summary.dmSellGoldAmount, #summary.dmBuyGoldAmount, #summary.dmGoldCompensate, #summary.dmWithDrawFee, #summary.dmRechargeFee, #summary.dmGoldSellFee, #summary.dmAdvertAmount, #summary.dmRechargeAdjust, #summary.dmWithDrawAdjust, #summary.dmGoldOrderAdjust
                FROM    #summary
                        INNER JOIN #yesterday
                        ON #summary.nCustomerType = #yesterday.nCustomerType
                        INNER JOIN #today
                        ON #summary.nCustomerType = #today.nCustomerType
                UNION ALL
                ( SELECT    #allsummary.vcCustomerNo, #allyesterday.dmYesterdayBalance, #alltoday.dmTodayBalance, #allsummary.dmRechargeAmount, #allsummary.dmWithDrawAmount, #allsummary.dmSellGoldAmount, #allsummary.dmBuyGoldAmount, #allsummary.dmGoldCompensate, #allsummary.dmWithDrawFee, #allsummary.dmRechargeFee, #allsummary.dmGoldSellFee, #allsummary.dmAdvertAmount, #allsummary.dmRechargeAdjust, #allsummary.dmWithDrawAdjust, #allsummary.dmGoldOrderAdjust
                  FROM      #allsummary
                            CROSS JOIN #allyesterday
                            CROSS JOIN #alltoday)

            END
        ELSE
            BEGIN
                --按照客户分类查询时

                --根据条件汇总数据
                SELECT  ‘合计‘ + CONVERT(VARCHAR(10), COUNT(1)) + ‘条‘ AS vcCustomerNo, SUM(dmRechargeAmount) AS dmRechargeAmount, SUM(dmWithDrawAmount) AS dmWithDrawAmount, SUM(dmSellGoldAmount) AS dmSellGoldAmount, SUM(dmBuyGoldAmount) AS dmBuyGoldAmount, SUM(dmGoldCompensate) AS dmGoldCompensate, SUM(dmWithDrawFee) AS dmWithDrawFee, SUM(dmRechargeFee) AS dmRechargeFee, SUM(dmGoldSellFee) AS dmGoldSellFee, SUM(dmAdvertAmount) AS dmAdvertAmount, SUM(dmRechargeAdjust) AS dmRechargeAdjust, SUM(dmWithDrawAdjust) AS dmWithDrawAdjust, SUM(dmGoldOrderAdjust) AS dmGoldOrderAdjust
                INTO    #temp1
                FROM    UC.Fin_DailySummary
                WHERE   ( @vcSearValue = ‘‘ OR ( vcLoginId = @vcSearValue OR vcCustomerNo = @vcSearValue ) ) AND ( @nCustomerType = 0 OR nCustomerType = @nCustomerType ) AND dtBalanceDate BETWEEN @vcBeginDate AND @vcEndDate

                --根据条件汇总前日余额
                SELECT  SUM(dmYesterdayBalance) AS dmYesterdayBalance
                INTO    #allyesterday1
                FROM    UC.Fin_DailySummary
                WHERE   ( @vcSearValue = ‘‘ OR ( vcLoginId = @vcSearValue OR vcCustomerNo = @vcSearValue ) ) AND ( @nCustomerType = 0 OR nCustomerType = @nCustomerType ) AND CONVERT(VARCHAR(10), dtBalanceDate, 120) = @vcSearchBeginDate

                --根据条件汇总当日余额
                SELECT  SUM(dmTodayBalance) AS dmTodayBalance
                INTO    #alltoday1
                FROM    UC.Fin_DailySummary
                WHERE   ( @vcSearValue = ‘‘ OR ( vcLoginId = @vcSearValue OR vcCustomerNo = @vcSearValue ) ) AND ( @nCustomerType = 0 OR nCustomerType = @nCustomerType ) AND CONVERT(VARCHAR(10), dtBalanceDate, 120) = @vcSearchEndDate

                --返回结果
                SELECT  #temp1.vcCustomerNo, #allyesterday1.dmYesterdayBalance, #alltoday1.dmTodayBalance, #temp1.dmRechargeAmount, #temp1.dmWithDrawAmount, #temp1.dmSellGoldAmount, #temp1.dmBuyGoldAmount, #temp1.dmGoldCompensate, #temp1.dmWithDrawFee, #temp1.dmRechargeFee, #temp1.dmGoldSellFee, #temp1.dmAdvertAmount, #temp1.dmRechargeAdjust, #temp1.dmWithDrawAdjust, #temp1.dmGoldOrderAdjust
                FROM    #temp1
                        CROSS JOIN #allyesterday1
                        CROSS JOIN #alltoday1

            END   

        --返回所有的记录数以及分页数
        SELECT  @nTotleRecord = COUNT(1), @nPageCount = COUNT(1) / @nPageSize + CASE COUNT(1) % @nPageSize
                                                                                  WHEN 0 THEN 0
                                                                                  ELSE 1
                                                                                END
        FROM    UC.Fin_DailySummary
        WHERE   ( @vcSearValue = ‘‘ OR ( vcLoginId = @vcSearValue OR vcCustomerNo = @vcSearValue ) ) AND ( @nCustomerType = 0 OR nCustomerType = @nCustomerType ) AND dtBalanceDate BETWEEN @vcBeginDate AND @vcEndDate

    END
时间: 2024-11-10 14:03:13

sql 日结的相关文章

VS制作报表(机房收费系统个人版日结账单)

做到机房收费系统日结账单和周结账单的时候需要用到报表,记得做VB版的机房收费系统时用的报表设计器制作报表,现在VS自带报表设计功能,只需要我们绑定数据源便可实现对账单的显示.通过看师哥师姐和同学们的博客,自己终于实现了,下面是详细教程: 1.添加数据集 2.按照提示添加数据项 3.出现配置向导后新建连接 4.选择服务器并输入身份验证,选择数据库名称 5.测试连接成功后确定接续配置向导,下一步 6.选择使用SQL语句,下一步 7.输入SQL语句,生成查询 8.点完成,数据集就添加成功了 9.接下来

2014.8.4日结

1.Java中HashMap遍历的两种方式 两种方式推荐下面这种 Iterator iter = hMap.entrySet().iterator(); Map.Entry entry = (Map.Entry) iter.next(); (int) entry.getKey(); (int) entry.getValue(); http://www.cnblogs.com/meieiem/archive/2011/11/02/2233041.html 2.java中PriorityQueue优

2014.7.27日结

1.java中没有友元类,需要public,private,protected,package等关键字来限制 2.python单例模式的实现 http://blog.csdn.net/ghostfromheaven/article/details/7671853 3.命名法有匈牙利,骆驼,帕斯卡等命名法 http://wenku.baidu.com/link?url=Hgd5tK7JtP44gq5aH64bWh3zROvJOHFPUD78CQHKT8vyVyyb80eHjU5YtatpZic6M

20-50/小时天猫微店客服在家兼职日结

岗位职责: 1.通过在线聊天工具(旺旺)简单解答顾客问题 2.旺旺在线打字即可,手机也可下载,工作简单 3.主要负责为前来咨询的买家介绍相关产品(如:商品材质.大小.颜色). 4.有意者加腾讯客服qq: 384468514 详细了解 任职资格: 1.男女不限,16周岁以上 2.可以上网(手机电脑都可以) 3.全职妈妈,大学生,在家待业人员,均可报名. 兼职1小时20元 过了晚上 12点以后每小时25元工资日结 3500 奖金 提成每天稳定在线6小时 超过6个小时 每小时/25元 可在家做 全职:

2014.7.23日结

1.main函数可以递归吗? g++,vs2010是可以的 2.int main(){    static int a=5;    while(--a > 0){        cout << "before" << a << endl;        main();        cout << "after" << a << endl;    } }输出结果是 int main(){ 

2014.8.6日结

1.C++中类中静态变量可以通过对象访问吗? 可以,不是const类型的话还可以修改 2.C++类中静态变量初始化必须在类外? 大部分是,静态常量整形数据成员必须立即初始化 3.静态成员函数没有this指针,不能调用成员函数和成员数据? 是的,没有this指针,就不知道当前的类的成员函数地址 2014.8.6日结

盛pos代理0.48结算分润日结激活返400元

首先说下盛pos现在的政策机器固定成本99元激活代理返400最低0.48结算,市场交易量大,公司还会额外补贴2个点.分润,返现日结.分润,返现不扣税.一.产品基本介绍: 费率:信用卡0.6秒到账闪付费率0.38秒到账. 限额:单笔最高10元,单日每台POS最高可刷50万,支持所有带银联标识信用卡. 到账时间:365天不分节假日均可秒到:24小时秒到.刷卡收款:自选落地本地商户,100%不跳码 带积分,根据实际需求想消费什么就刷什么商户,远离套现风险,实现真实消费的场景.(除刷卡收款外,盛pos还

2014.8.23日结

数据挖掘经典算法 国际权威的学术组织ICDM,于06年12月年评选出的数据挖掘领域的十大经典算法:C4.5, k-Means, SVM, Apriori, EM, PageRank, AdaBoost, kNN, Naive Bayes, and CART. 个人论文采用的是Apriori,所以针对于此总结一下 基本概念: 1.支持度(P(AB)),置信度(条件概率P(B|A)) 例如:啤酒,尿布支持度3%,置信度80% 同时购买啤酒,尿布的是3%,购买啤酒的人中80%会买尿布 2.强规则:同时

日结(2018年12月22日)

春日的雨水,夏日的热情,秋日的余晖,冬至的来临.今天是快乐的一天,也是劳累的一天 这是荒唐的一天,也是值得纪念的一天.从早上9点多开始的鲍师傅之旅,伴随着两个小时的无奈和濒临崩溃的绝望结束.虽然很辛苦,但是排到的那一刻就仿佛一段debug很久的程序跑出了自己希望的结果一样,这种感觉是真的有点开心(我可以说买完看着后面的人焦急等待的神情是真的爽吗) 从来没有逛过这么久的街,也从来没有花钱花到身无分文还想浪.这个月的一次"大作战"改变了原本的生活轨迹,计划好的生活费成了不足,这种超出预期的