`

待账单统计语句

阅读更多

-----------------------------------------------------------------------------------
--------------------------已优化的待开帐语句--------------------------------------------
------------------------------------------------------------------------------------
--优化版本
--把待开帐和已开帐的统计结果都取出,两个结果集 中去除已开帐的统计信息
WITH
 waitingBill as

(
 select t.inv_seller_cid invSellerCid,t.inv_seller_name invSellerName,t.debtor_cid debtorCid,t.debtor_name debtorName,to_char(t.bill_date,'yyyy-mm-dd') billDate,to_char(t.payoff_date,'yyyy-mm-dd') payoffDate,t.bill_plan billPlan,count(t.id_ticket) count,
    --已确认总金额
    sum(case
        when  t.status='C'
              then dc.bill_amt
              else 0
        end
     ) as confirmedAmt,
     ----已确认总数
      sum(case
        when  t.status='C'
              then 1
              else 0
        end
     ) as confirmedCount,
     --已确认订票总金额
       sum(case
        when  t.status='C' and t.orig_id_ticket is null
              then dc.bill_amt
              else 0
        end
     ) as billBookAmt,
     --已确认订票总数
     sum(case
        when  t.status='C' and t.orig_id_ticket is null
              then 1
              else 0
        end
     ) as billBookCount,
     --已确认退票总金额
     sum(case
        when  t.status='C' and t.orig_id_ticket is not null
              then dc.bill_amt
              else 0
        end
     ) as billReturnAmt,
     --已确认退票总数
     sum(case
        when  t.status='C' and t.orig_id_ticket is not null
              then 1
              else 0
        end
     ) as billReturnCount,
     --未确认订票总金额
      sum(case
        when  t.status!='C' and t.orig_id_ticket is null
              then t.amt
              else 0
        end
     ) as unconfirmedBillBookAmt,
     --未确认订票总数
     sum(case
        when  t.status!='C' and t.orig_id_ticket is null
              then 1
              else 0
        end
     ) as unconfirmedBillBookCount,
    --未确认退票总金额
     sum(case
        when  t.status!='C' and t.orig_id_ticket is not null
              then t.amt
              else 0
        end
     ) as unconfirmedBillReturnAmt,
    --未确认退票总数
     sum(case
        when  t.status!='C' and t.orig_id_ticket is not null
              then 1
              else 0
        end
     ) as unconfirmedBillReturnCount,
    --未确认总数
    sum(case
       when  t.status!='C'
           then 1
           else 0
       end
  )  as unconfirmedCount  from t_ticket t left join t_debtor_clear_list dc on t.id_ticket=dc.id_ticket
   where 1=1   and t.bill_date is not null
   --开帐日 在当前月中
    and to_char(t.bill_date,'yyyy-mm-dd')>='2009-06-01' and to_char(t.bill_date,'yyyy-mm-dd')<='2009-06-30'
    and t.bill_status='0'
    group by t.inv_seller_cid,t.inv_seller_name,t.debtor_cid,t.debtor_name, to_char(t.bill_date,'yyyy-mm-dd'),to_char(t.payoff_date,'yyyy-mm-dd'),t.bill_plan
),
billedBill as
(
 select a.* from  waitingBill a ,t_debtor_bill b
 where a.invSellerCid=b.bill_seller_cid and a.invSellerName=b.bill_seller_name and a.debtorCid=b.bill_debtor_cid and a.debtorName=b.bill_debtor_name and a.billDate=to_char(b.bill_date,'yyyy-mm-dd') and a.payoffDate=to_char(b.payoff_date,'yyyy-mm-dd') and a.billPlan=b.bill_plan
 )
--SELECT * FROM  waitingBill WHERE  billDate!=(SELECT  billDate from billedBill )
SELECT * FROM  waitingBill WHERE  (invSellerCid||invSellerName||debtorCid||debtorName||billDate||payoffDate||billPlan)!=(SELECT  (invSellerCid||invSellerName||debtorCid||debtorName||billDate||payoffDate||billPlan) from billedBill )

 


--原始版本
--把待开帐和已开帐的统计结果都取出,用 MINUS  在数据库内存中 去除已开帐的统计信息

select * from (select t.inv_seller_cid invSellerCid,t.inv_seller_name invSellerName,t.debtor_cid debtorCid,t.debtor_name debtorName,to_char(t.bill_date,'yyyy-mm-dd') billDate,to_char(t.payoff_date,'yyyy-mm-dd') payoffDate,t.bill_plan billPlan,count(t.id_ticket) count,
  --已确认总金额
  sum(case
      when  t.status='C'
            then dc.bill_amt
            else 0
      end
   ) as confirmedAmt,
   ----已确认总数
    sum(case
      when  t.status='C'
            then 1
            else 0
      end
   ) as confirmedCount,
   --已确认订票总金额
     sum(case
      when  t.status='C' and t.orig_id_ticket is null
            then dc.bill_amt
            else 0
      end
   ) as billBookAmt,
   --已确认订票总数
   sum(case
      when  t.status='C' and t.orig_id_ticket is null
            then 1
            else 0
      end
   ) as billBookCount,
   --已确认退票总金额
   sum(case
      when  t.status='C' and t.orig_id_ticket is not null
            then dc.bill_amt
            else 0
      end
   ) as billReturnAmt,
   --已确认退票总数
   sum(case
      when  t.status='C' and t.orig_id_ticket is not null
            then 1
            else 0
      end
   ) as billReturnCount,
   --未确认订票总金额
    sum(case
      when  t.status!='C' and t.orig_id_ticket is null
            then t.amt
            else 0
      end
   ) as unconfirmedBillBookAmt,
   --未确认订票总数
   sum(case
      when  t.status!='C' and t.orig_id_ticket is null
            then 1
            else 0
      end
   ) as unconfirmedBillBookCount,
  --未确认退票总金额
   sum(case
      when  t.status!='C' and t.orig_id_ticket is not null
            then t.amt
            else 0
      end
   ) as unconfirmedBillReturnAmt,
  --未确认退票总数
   sum(case
      when  t.status!='C' and t.orig_id_ticket is not null
            then 1
            else 0
      end
   ) as unconfirmedBillReturnCount,
  --未确认总数
  sum(case
     when  t.status!='C'
         then 1
         else 0
     end
)  as unconfirmedCount  from t_ticket t left join t_debtor_clear_list dc on t.id_ticket=dc.id_ticket
 where 1=1   and t.bill_date is not null
 --开帐日 在当前月中
  and to_char(t.bill_date,'yyyy-mm-dd')>='2009-06-01' and to_char(t.bill_date,'yyyy-mm-dd')<='2009-06-30'
  and t.bill_status='0'
   group by t.inv_seller_cid,t.inv_seller_name,t.debtor_cid,t.debtor_name,to_char(t.bill_date,'yyyy-mm-dd'),to_char(t.payoff_date,'yyyy-mm-dd'),t.bill_plan
 order by to_char(t.bill_date,'yyyy-mm-dd') asc,t.debtor_name desc)

--去除充分的统计信息
MINUS


 select a.* from (select t.inv_seller_cid invSellerCid,t.inv_seller_name invSellerName,t.debtor_cid debtorCid,t.debtor_name debtorName,to_char(t.bill_date,'yyyy-mm-dd') billDate,to_char(t.payoff_date,'yyyy-mm-dd') payoffDate,t.bill_plan billPlan,count(t.id_ticket) count,
  --已确认总金额
  sum(case
      when  t.status='C'
            then dc.bill_amt
            else 0
      end
   ) as confirmedAmt,
   ----已确认总数
    sum(case
      when  t.status='C'
            then 1
            else 0
      end
   ) as confirmedCount,
   --已确认订票总金额
     sum(case
      when  t.status='C' and t.orig_id_ticket is null
            then dc.bill_amt
            else 0
      end
   ) as billBookAmt,
   --已确认订票总数
   sum(case
      when  t.status='C' and t.orig_id_ticket is null
            then 1
            else 0
      end
   ) as billBookCount,
   --已确认退票总金额
   sum(case
      when  t.status='C' and t.orig_id_ticket is not null
            then dc.bill_amt
            else 0
      end
   ) as billReturnAmt,
   --已确认退票总数
   sum(case
      when  t.status='C' and t.orig_id_ticket is not null
            then 1
            else 0
      end
   ) as billReturnCount,
   --未确认订票总金额
    sum(case
      when  t.status!='C' and t.orig_id_ticket is null
            then t.amt
            else 0
      end
   ) as unconfirmedBillBookAmt,
   --未确认订票总数
   sum(case
      when  t.status!='C' and t.orig_id_ticket is null
            then 1
            else 0
      end
   ) as unconfirmedBillBookCount,
  --未确认退票总金额
   sum(case
      when  t.status!='C' and t.orig_id_ticket is not null
            then t.amt
            else 0
      end
   ) as unconfirmedBillReturnAmt,
  --未确认退票总数
   sum(case
      when  t.status!='C' and t.orig_id_ticket is not null
            then 1
            else 0
      end
   ) as unconfirmedBillReturnCount,
  --未确认总数
  sum(case
     when  t.status!='C'
         then 1
         else 0
     end
)  as unconfirmedCount  from t_ticket t left join t_debtor_clear_list dc on t.id_ticket=dc.id_ticket
 where 1=1   and t.bill_date is not null
 --开帐日 在当前月中
  and to_char(t.bill_date,'yyyy-mm-dd')>='2009-06-01' and to_char(t.bill_date,'yyyy-mm-dd')<='2009-06-30'
  and t.bill_status='0'
   group by t.inv_seller_cid,t.inv_seller_name,t.debtor_cid,t.debtor_name,to_char(t.bill_date,'yyyy-mm-dd'),to_char(t.payoff_date,'yyyy-mm-dd'),t.bill_plan
 order by to_char(t.bill_date,'yyyy-mm-dd') asc,t.debtor_name desc) a,
 (
 select db.*
 from t_debtor_bill db where db.bill_status=1) b
 -- where a.invSellerCid!=b.bill_seller_cid and a.invSellerName!=b.bill_seller_name and a.debtorCid!=b.bill_debtor_cid and a.debtorName!=b.bill_debtor_name and a.billDate!=b.bill_date and a.payoffDate!=b.payoff_date and a.billPlan!=b.bill_plan
where a.invSellerCid=b.bill_seller_cid and a.invSellerName=b.bill_seller_name and a.debtorCid=b.bill_debtor_cid and a.debtorName=b.bill_debtor_name and a.billDate=to_char(b.bill_date,'yyyy-mm-dd') and a.payoffDate=to_char(b.payoff_date,'yyyy-mm-dd') and a.billPlan=b.bill_plan

 

分享到:
评论

相关推荐

    基于JavaWeb的手机账单查询系统

    3. 结算功能:用户可以查看待结算账单,并进行在线支付,支持多种支付方式,如支付宝、微信支付等。 4. 管理员模块:管理员可以查看所有用户的账单,进行数据分析,处理缴费异常情况,如设置逾期提醒,处理退款请求...

    基于Qt的学生报账系统

    5. 审核与审批:管理员可以查看并审批学生的报账单,这可能需要增加状态字段来追踪报账单的状态(如待审批、已批准、已拒绝)。审批流程可以通过信号与槽实现,当状态改变时触发相应的通知或操作。 6. 报表统计:...

    vb+access酒店管理信息系统(论文+系统).rar

    例如,可以在客房预订界面中使用DataGrid来显示可预订的房间列表,也可以在账单管理界面中使用它来显示待支付的账单信息。同时,VB的窗体设计功能强大,可以轻松设计出符合酒店风格的用户界面。 在数据库方面,...

    房屋出租管理系统 SSH

    6. 报表统计:生成各种统计报表,如租金收入、房源空置率、租户满意度等,帮助管理者分析业务状况。 此外,项目还提供了SQLServer数据库文件,这可能包含了预设的数据表结构、初始化数据以及可能的存储过程或触发器...

    VB+SQL餐饮管理系统(源代码+系统+可执行程序).zip

    3. 订单管理:记录顾客点餐,处理订单状态(如待处理、烹饪中、已上菜、已完成)。 4. 库存管理:监控食材库存,及时补充,避免缺货。 5. 收银功能:计算账单,支持多种支付方式。 6. 报表统计:生成销售报告,帮助...

    北大青鸟二单元项目

    【北大青鸟二单元项目】是一个面向初学者的IT实践项目,主要涵盖了超市账单管理的实现。这个项目旨在帮助学员巩固和应用他们在北大青鸟学习的基础IT知识,特别是Java Web开发技能。通过参与这个项目,学员能够了解并...

    销售管理系统(Delphi+SQL)

    例如,可以使用SQL语句来添加新的销售记录、查询特定时间段的销售报告、统计产品销售额或追踪客户购买历史。SQL的高效性能和强大的数据处理能力使得它成为构建销售管理系统的理想选择。 销售管理系统通常包含以下几...

    vb酒店客房管理系统简单的

    6. **报表与统计**:提供各种统计报表,如入住率、收入分析等,以辅助酒店管理层做决策。 7. **安全机制**:为了保护数据安全,系统应具有用户权限管理,不同角色(如前台、经理)有不同的操作权限。 【关键技术与...

    订单系统数据库源码

    3. **订单表**:记录每个订单的基本信息,如订单号、顾客ID、下单时间、状态(待支付、已支付、已发货、已完成等)。订单表也可能包含订单总额和配送信息。 4. **订单详情表**:保存每个订单中的具体商品信息,包括...

    咖啡店信息管理数据库设计.doc

    对于咖啡店,数据流程可能包括顾客下单(输入订单信息)、订单处理(计算总价、生成账单)、库存扣减(根据订单消耗原料)、支付处理(记录支付状态)、报表生成(统计每日销售、利润)等步骤。 2.3 数据字典 数据...

Global site tag (gtag.js) - Google Analytics