`

最近项目中写的几个比较复杂的sql

阅读更多
1.这个是交叉表的应用, 用于统计分析报表
select entity_.*,
       entity_.field3 + entity_.field4 + entity_.field5 + entity_.field6 +
       entity_.field7 + entity_.field8 + entity_.field9 as sum_maintain,
       entity_.field1 + entity_.field2 + entity_.field3 + entity_.field4 +
       entity_.field5 + entity_.field6 + entity_.field7 + entity_.field8 +
       entity_.field9 + entity_.field10 as sum_all
  from (select result_.id,
               result_.name,
               sum(case result_.type_id
                     when 1 then -- 固定费用(一级)
                      amount
                      when 5 then -- 复印打印(二级)
                      amount
                      when 6 then -- 办公用品(二级)
                      amount
                      when 7 then -- 运费(二级)
                      amount
                      when 8 then -- 汇款手续费(二级)
                      amount
                      when 9 then -- 邮寄费(二级)
                      amount
                     else
                      0
                   end) field1, -- 固定费用,
               sum(case result_.type_id
                     when 2 then -- 差旅费(一级)
                      amount
                      when 10 then -- 通讯费(二级)
                      amount
                      when 11 then -- 业务招待费(二级)
                      amount
                      when 12 then -- 车辆使用费(二级)
                      amount
                      when 13 then -- 房租(二级)
                      amount
                      when 14 then -- 差旅车票(二级)
                      amount
                      when 15 then -- 住宿费(二级)
                      amount
                     else
                      0
                   end) field2, --  差旅费,
               sum(case result_.type_id
                     when 16 then
                      amount
                     else
                      0
                   end) field3, --  临聘人员奖励提成费,
               sum(case result_.type_id
                     when 17 then
                      amount
                     else
                      0
                   end) field4, --  促销礼品费,
               sum(case result_.type_id
                     when 18 then
                      amount
                     else
                      0
                   end) field5, -- 展示特陈费,
               sum(case result_.type_id
                     when 19 then
                      amount
                     else
                      0
                   end) field6, --  茶友会活动费,
               sum(case result_.type_id
                     when 20 then
                      amount
                     else
                      0
                   end) field7, --  新品进场费,
               sum(case result_.type_id
                     when 21 then
                      amount
                     else
                      0
                   end) field8, --  店员奖励,
               sum(case result_.type_id
                     when 22 then
                      amount
                     else
                      0
                   end) field9, --  终端客情费
               sum(case result_.type_id
                     when 4 then
                      amount
                     else
                      0
                   end) field10 --奖金
from (select detail_.*, s.groupname as name
                  from (select d.type_id,
                               d.amount,
                               get_salagroup_id(:salegroupid, d.employee_id) as id
                          from ch_process_form             pf,
                               ch_process_form_item_detail d
                         where d.form_id = pf.id
                           and (pf.process_type = :processType1 -- 拨付申请
                               or pf.process_type = :processType2) -- 报销申请
                           and pf.process_state != 4 -- 去掉被取消的
                           and (pf.process_state = :state or :state = -1)
                           and to_char(pf.create_date, 'yyyy-mm-dd hh24:mi:ss') >=
                               :start_
                           and to_char(pf.create_date, 'yyyy-mm-dd hh24:mi:ss') <=
                               :end_) detail_,
                       zx_dm_salegroup s
                 where detail_.id = s.salegroupid
                   and detail_.id != 0) result_ -- 0表示统计的detail的使用employee不在当前的区域
         group by result_.id, result_.name) entity_




2.这个是一个存储函数, 比较简单, 主要是oracle中connect by prior start with的用法
 create or replace function GET_SALAGROUP_ID1(topgroupid in number,
                                             accountid  in number)
-- 根据给定的账户id找到对应的最上级辖区id, 且该辖区id必须小于给定的topgroupid
 return number is
  Result number;
  cursor c1 is
    select min(s.salegroupid)
      from zx_dm_salegroup s
    connect by prior s.managegroupid = s.salegroupid -- 由子找父
           and s.salegroupid != topgroupid -- 给定向上找的限定条件
     start with s.salegroupid = (select es.salegroupid
                                   from zx_dm_employee_sale es,
                                        ch_account_employee ae,
                                        ch_account          a
                                  where es.employeeid = ae.employee_id
                                    and ae.main_id = a.main_id
                                    and a.id = accountid); -- 取得对应帐号的辖区id
begin
  open c1;
  fetch c1
    into result;
  close c1;
  return(Result);
end GET_SALAGROUP_ID1;



3.用一个字段做累加计算找余额
select remain_.code_id, --费用帐号
       remain_.type_id,
       remain_.type_name,
       current_.amount, -- 当前申请核销金额
       proposal_.amount as proposal_amount, --当前申请拨付金额
       payment_.amount + nvl(current_.amount, 0) as enable_amount, -- 可用核销余额
       remain_.remain + nvl(proposal_.amount, 0) as enable_remain, -- 可用费用余额
       remain_.remain as sum_amount -- 费用总额
  from (select ct.id as type_id,
               ct.type_name,
               max_.id,
               max_.code_id,
               cd.remain - nvl(freeze_.amount, 0) as remain
          from ch_code_deal cd,
               ch_charge_type ct,
               ch_code c,
               (select max(cd.id) as id, c.id as code_id
                  from ch_code c, ch_code_deal cd
                 where c.account_id =
                       (select a2.id
                          from ch_account a1, ch_account a2
                         where a1.id = :accountId
                           and a2.main_id = a1.main_id
                           and a2.account_type = 1)
                   and cd.code_id = c.id
                 group by c.id) max_, -- 从最后一条交易记录中取出帐号的费用余额
               (select cf.code_id, sum(cf.amount) as amount
                  from ch_code_freeze cf
                 where cf.freeze_flag = 1
                 group by cf.code_id) freeze_ -- 处理中(被冻结)的费用   
         where cd.id = max_.id
           and cd.code_id = c.id
           and c.type_id = ct.id
           and cd.code_id = freeze_.code_id(+)) remain_, -- 取出账户的费用余额           
       (select i.type_id, i.amount - nvl(certi_.amount, 0) as amount
          from ch_process_form_item i,
               (select i.type_id, sum(i.amount) amount
                  from ch_process_form_item i, ch_process_form f
                 where f.parent_id = :parentFormId
                   and i.form_id = f.id
                   and f.process_type = :processTypeId
                   and f.process_state != 4 --去掉被取消的
                 group by i.type_id) certi_ -- 已经核销的部分
         where i.form_id = :parentFormId
           and i.type_id = certi_.type_id(+)) payment_, -- 可用核销金额
       (select type_id, code_id, amount
          from ch_process_form_item
         where form_id = :proposalFormId) proposal_, -- 当前拨付申请
       (select type_id, code_id, amount
          from ch_process_form_item
         where form_id = :formId) current_ -- 当前核销申请
 where remain_.type_id = current_.type_id(+)
   and remain_.type_id = payment_.type_id(+)
   and remain_.type_id = proposal_.type_id(+)
   and payment_.amount + nvl(current_.amount, 0) > 0


4.union的使用
select form_.account_id,
       form_.account_name,
       ct.id type_id,
       ct.type_name,
       sum(expense_remain) expense_remain,
       sum(expense_freeze_amount) expense_freeze_amount,
       sum(expense_enable_amount) expense_enable_amount,
       sum(fund_remain) fund_remain,
       sum(fund_freeze_amount) fund_freeze_amount,
       sum(fund_enable_amount) fund_enable_amount
  from (select a.id as account_id,
               a.name as account_name,
               ae.employee_id,
               t.id type_id,
               remain_.remain as expense_remain,
               freeze_.amount as expense_freeze_amount,
               remain_.remain - nvl(freeze_.amount, 0) expense_enable_amount,
               null fund_remain,
               null fund_freeze_amount,
               null fund_enable_amount
          from ch_code c,
               ch_account a,
               ch_account_employee ae,
               ch_charge_type t,
               (select d.code_id, d.remain
                  from ch_code_deal d,
                       (select d.code_id, max(d.id) as id
                          from ch_code_deal d
                         group by d.code_id) last_
                 where d.id = last_.id) remain_, --余额
               (select f.code_id, sum(f.amount) as amount
                  from ch_code_freeze f, ch_account a
                 where f.account_id = a.id
                   and a.account_type = 1
                   and f.freeze_flag = 1 -- 费用冻结
                 group by f.code_id) freeze_ -- 费用账户金额
         where c.id = remain_.code_id(+)
           and c.id = freeze_.code_id(+)
           and c.account_id = a.id
           and a.account_type = 1
           and a.main_id = ae.main_id
           and c.type_id = t.id
           and remain_.remain is not null
        union
        select a.id as account_id,
               a.name as account_name,
               ae.employee_id,
               t.id type_id,
               null expense_remain,
               null expense_freeze_amount,
               null expense_enable_amount,
               remain_.remain fund_remain,
               freeze_.amount fund_freeze_amount,
               remain_.remain - nvl(freeze_.amount, 0) fund_enable_amount
          from ch_code c,
               ch_account a,
               ch_account_employee ae,
               ch_charge_type t,
               (select d.code_id, d.remain
                  from ch_code_deal d,
                       (select d.code_id, max(d.id) as id
                          from ch_code_deal d
                         group by d.code_id) last_
                 where d.id = last_.id) remain_,
               (select f.code_id, sum(f.amount) as amount
                  from ch_code_freeze f, ch_account a
                 where f.account_id = a.id
                   and a.account_type = 2
                   and f.freeze_flag = 3 -- 核销冻结
                 group by f.code_id) freeze_ -- 资金账户金额
         where c.id = remain_.code_id(+)
           and c.id = freeze_.code_id(+)
           and c.account_id = a.id
           and a.account_type = 2
           and a.main_id = ae.main_id
           and c.type_id = t.id
           and remain_.remain is not null) form_,
       ch_charge_type ct
 where ct.id = form_.type_id
   and form_.account_name like :accountName


4.这个是连接的使用(target中的左右连接比较复杂)
select source_.*, target_.target_enable_remain, target_.target_sum_amount
  from (select last_.code_id,
               last_.type_id,
               last_.type_name,
               current_.amount as amount, -- 申请金额
               auditing_.amount as auditing_amount, -- 在批金额
               nvl(last_.remain, 0) - nvl(paying_.amount, 0) as enable_remain -- 可用余额
          from (select cd.code_id, cd.remain, c.type_id, c.type_name
                  from ch_code_deal cd,
                       (select c.id, t.id type_id, t.type_name
                          from ch_code c, ch_account a, ch_charge_type t
                         where a.id = :sourceAccountId
                           and c.account_id = a.id
                           and c.enable_flag = 1
                           and c.type_id = t.id
                           and t.enable_flag = 1) c,
                       (select max(cd.id) as id
                          from ch_code_deal cd
                         group by cd.code_id) max_ -- 支出方费用帐号余额
                 where cd.id = max_.id
                   and cd.code_id = c.id) last_,
               (select cf.code_id, sum(cf.amount) amount
                  from ch_code_freeze cf
                 where cf.account_id = :sourceAccountId
                   and cf.process_type = :processType
                   and cf.form_id != :formId
                 group by cf.code_id) auditing_, --在批金额
               (select cf.code_id, sum(cf.amount) amount
                  from ch_code_freeze cf
                 where cf.account_id = :sourceAccountId
                   and cf.freeze_flag = 1 -- 在拨申请中
                 group by cf.code_id) paying_, --拨付申请金额
               (select i.code_id, i.amount
                  from ch_process_form_item i
                 where form_id = :formId) current_
         where last_.code_id = auditing_.code_id(+)
           and last_.code_id = paying_.code_id(+)
           and last_.code_id = current_.code_id(+)) source_, --支出帐号信息
       (select last_.code_id, last_.type_id,
               nvl(last_.remain, 0) - nvl(paying_.amount, 0) as target_enable_remain, -- 可用余额
               nvl(last_.remain, 0) as target_sum_amount -- 费用总额
          from (select c.id code_id, c.type_id, cd.remain
                  from ch_code_deal cd,
                       (select c.id, t.id type_id
                          from ch_code c, ch_account a, ch_charge_type t
                         where a.id = :targetAccountId
                           and c.account_id = a.id
                           and c.enable_flag = 1
                           and c.type_id = t.id
                           and t.enable_flag = 1) c,
                       (select max(cd.id) as id
                          from ch_code_deal cd
                         group by cd.code_id) max_
                 where cd.id = max_.id(+)
                   and cd.code_id(+) = c.id) last_,
               (select cf.code_id, sum(cf.amount) amount
                  from ch_code_freeze cf
                 where cf.account_id = :targetAccountId
                   and cf.freeze_flag = 1 -- 拨付申请中
                 group by cf.code_id) paying_ -- 拨付申请金额
         where last_.code_id = paying_.code_id(+)) target_ -- 接收帐号信息
 where source_.type_id = target_.type_id


5.这个也是union加聚合函数sum的使用, 用来将两类数据分组为两大类, 然后每一类是一列
select form_.main_id,
       e_.employeeid,
       e_.employeename,
       form_.type_id,
       ct.type_name,
       sum(expense_remain) expense_remain, -- 这里必须使用sum来聚合一下, 否则费用和资金的金额将不会合并, 记录集会增加一倍
       sum(expense_freeze_amount) expense_freeze_amount,
       sum(expense_enable_remain) expense_enable_remain,
       sum(fund_remain) fund_remain,
       sum(fund_freeze_amount) fund_freeze_amount,
       sum(fund_enable_remain) fund_enable_remain
  from (select ae.main_id,
               ae.employee_id,
               remain_.type_id,
               remain_.remain as expense_remain,
               freeze_.amount as expense_freeze_amount,
               remain_.remain - nvl(freeze_.amount, 0) expense_enable_remain,
               null fund_remain,
               null fund_freeze_amount,
               null fund_enable_remain
          from ch_account_employee ae,
               (select d.code_id, c.type_id, a.main_id, d.remain, max(d.id)
                  from ch_code_deal d, ch_account a, ch_code c
                 where d.code_id = c.id
                   and c.account_id = a.id
                   and a.account_type = 1
                 group by d.code_id, c.type_id, a.main_id, d.remain) remain_, --余额
               (select f.code_id, sum(f.amount) as amount
                  from ch_code_freeze f, ch_account a
                 where f.account_id = a.id
                   and a.account_type = 1
                   and f.freeze_flag = 1
                 group by f.code_id) freeze_ -- 待拨费用
         where remain_.code_id = freeze_.code_id(+)
           and remain_.main_id = ae.main_id
        union
        select ae.main_id,
               ae.employee_id,
               remain_.type_id,
               null expense_remain,
               null expense_freeze_amount,
               null expense_enable_remain,
               remain_.remain fund_remain,
               freeze_.amount fund_freeze_amount,
               remain_.remain - nvl(freeze_.amount, 0) fund_enable_remain
          from ch_account_employee ae,
               (select d.code_id, c.type_id, a.main_id, d.remain, max(d.id)
                  from ch_code_deal d, ch_account a, ch_code c
                 where d.code_id = c.id
                   and c.account_id = a.id
                   and a.account_type = 2
                 group by d.code_id, c.type_id, a.main_id, d.remain) remain_,
               (select f.code_id, sum(f.amount) as amount
                  from ch_code_freeze f, ch_account a
                 where f.account_id = a.id
                   and a.account_type = 2
                   and f.freeze_flag = 3 -- 核销冻结
                 group by f.code_id) freeze_ -- 资金账户金额
         where remain_.code_id = freeze_.code_id(+)
           and remain_.main_id = ae.main_id) form_,
       ch_charge_type ct,
       zx_dm_employee e_
 where ct.id = form_.type_id
   and e_.employeeid = form_.employee_id
   and e_.employeename like ?
 group by form_.main_id,
          e_.employeeid,
          e_.employeename,
          form_.type_id,
          ct.type_name
分享到:
评论

相关推荐

    一个项目的存储过程.sql

    在SQL(Structured Query Language)中,存储过程是预编译的SQL语句集合,它封装了数据库中的复杂操作,可以被多次调用,提供了一种更高效、安全且易于维护的方式来执行数据库操作。标题和描述中提到的"一个项目的...

    易买网项目sql数据库版

    【描述】"sql版易买网 个人手工打造 如有雷同 纯属盗版 北大青鸟二期结业项目"这段描述揭示了几个关键点: 1. **SQL版易买网**:这里提到的“SQL版”表明该项目是围绕数据库管理和查询语言SQL构建的,可能涉及到...

    mybatisx,一款打印项目sql在日志中的插件

    1. **SQL日志打印**:插件会在日志中详细输出每个SQL语句,包括原始的SQL模板、实际的参数值以及执行后的结果,这对于理解代码与数据库交互的情况非常有帮助。 2. **执行时间统计**:MybatisX还提供了SQL执行时间的...

    C#+SQL Server项目开发实践

    在这个"C#+SQL Server项目开发实践"中,我们可以学习到以下几个关键知识点: 1. C#基础:包括变量、数据类型、控制结构(如条件语句、循环)、函数、类和对象等。这些都是编写任何C#程序的基础。 2. .NET ...

    达内电信计费项目包括sql语句

    在电信计费系统中,SQL主要用于以下几个关键知识点: 1. 数据库设计:SQL用于创建和管理关系型数据库,如定义表结构、设置字段类型、主键和外键等。在电信计费项目中,可能涉及用户信息表、通话记录表、费用明细表...

    java+sql项目

    在本Java+SQL实训项目中,我们关注的核心是利用JSP(Java Server Pages)技术和SQL数据库来构建一个机票购买系统。这个系统旨在提供一个安全、稳定的服务,让用户能够方便地在线购买机票。以下是对该项目各个方面的...

    一款IDEA插件,可以实现生成Mysql的动态SQL,适合新手使用mybatis来写项目,加快项目落地周期.zip

    标签是Mabits动态SQL中最常用的一个标签之一,它的作用是根据给定条件包含或排除不同的部分,以生成不同的SQL语句。在XML文件中,标签通常被嵌套在其他标签内,如、和等标签内,用于控制生成的SQL语句的结构和内容。...

    C# +sql server项目开发实践

    在"C# + SQL Server项目开发实践"中,我们探讨了如何使用C#编程语言与Microsoft SQL Server数据库系统相结合,来构建高效、稳定的业务应用程序。这个主题涵盖了教务管理系统和人力资源管理系统等多个实际应用场景,...

    项目sql及文件

    在你的"项目sql及文件"压缩包中,显然包含了一个名为"DORMMS"的子文件夹,这可能是代表“Database ORM Management System”(数据库对象关系映射管理系统)或者某种特定项目的代码库。ORM(Object-Relational ...

    WINCC读写SQL数据库的例子

    在提供的压缩包文件"WINCC读写SQL数据库的示例"中,你将找到一个完整的WinCC项目,其中包含了上述概念的实际应用。通过研究这个项目,你可以更直观地学习如何在WinCC中编写和运行VBScript,以及如何与SQL Server...

    C#执行sql文件 运行sql文件

    首先,要实现C#执行SQL文件,我们需要了解几个关键的概念和技术: 1. **ADO.NET**: .NET Framework中的数据访问组件,它为开发者提供了与数据库交互的接口,如SqlConnection、SqlCommand等类。 2. **SqlConnection...

    php mssql扩展SQL查询中文字段名解决方法

    解决这个问题的方法分为几个步骤: 1. **确认数据库编码**:确保你知道SQL Server数据库的字符集,例如,确认它是GBK编码。 2. **检查PHP脚本编码**:了解你的PHP脚本文件的编码,例如,确认它们是UTF-8编码。 3. *...

    ibatis企业开发报表前调试(日志,复杂sql动态拼接)

    本篇将详细介绍如何利用日志来调试Ibatis中的复杂SQL动态拼接。 首先,了解Ibatis的日志系统是关键。Ibatis默认集成了几种常见的日志实现,如Log4j、Logback和Java内置的日志框架。在项目中,我们需要配置相应的...

    谷粒商城(学习笔记) sql文件

    在“谷粒商城”这个项目中,SQL脚本将用于以下几个核心方面: 1. **数据库设计**:首先,SQL脚本会定义各个数据表的结构,包括表名、字段名、字段类型、主键、外键等。例如,可能会有用户表(users)、商品表...

    收集的几个MyEclipse+SQL网站开发源码

    【标题】"收集的几个MyEclipse+SQL网站开发源码"所涵盖的知识点主要集中在MyEclipse集成开发环境和SQL数据库技术在网站开发中的应用。MyEclipse是一款强大的Java集成开发工具,常用于企业级Web应用的开发,它基于...

    SQL Server数据库中成批导入数据的几个常用方法

    在SQL Server数据库中,成批导入数据是一项常见的需求,尤其在软件项目实施过程中,大量数据的迁移至关重要。本文将介绍几种常用的SQL Server批量导入数据的方法。 首先,使用`SELECT INTO`语句是一种直接且简单的...

    经典SQL脚本大全

    │ │ 5.1.1 SET IDENTITY_INSERT 中的几个问题.sql │ │ 5.1.1 修改标识值的示例.sql │ │ 5.1.1 标识列与普通列互相转换的示例.sql │ │ 5.2.1 查表法按日期生成流水号的示例.sql │ │ 5.2.1 查表法生成流水号...

    精通 ASP.NET 2.0 + SQL Server 2005 项目开发

    具体包括以下几个核心知识点: 1. **ASP.NET 2.0控件**:了解各种服务器控件,如Label、TextBox、Button等,以及如何使用它们来构建交互式表单。 2. **数据绑定**:掌握DataSource控件的使用,包括SqlDataSource、...

    SpringBoot整合Mybatis连接SQL Server 跨库批量插入

    总结,Spring Boot整合Mybatis连接SQL Server进行跨库批量插入,主要涉及以下几个步骤: 1. 添加相关依赖。 2. 配置数据库连接信息。 3. 创建Mapper接口和XML文件,定义跨库插入的SQL语句。 4. 在Service层调用...

    Java电信项目-netctoss(工程+sql脚本)

    在学习这个项目时,你需要关注以下几个重点: 1. 系统架构设计:了解netctoss如何组织模块,各模块间的交互方式。 2. 数据库设计:分析SQL脚本,理解电信业务的关键数据模型。 3. 业务逻辑:通过源码阅读理解核心...

Global site tag (gtag.js) - Google Analytics