建工发债sql

管理费用

为了得到科目名称,只好再从外面写一层

select a.*,
(select b.subjname from bd_accsubj b where b.subjcode=a.scode  and b.pk_glorgbook='0001E1100000000000MX') 项目--从总部机关的会计科目得到名称
 
 from (
   select substr(bd_accsubj.subjcode,1,6)scode,     
             sum( case when gl_balance.year= '2014' then  gl_balance.debitamount else 0 end) "2014发生额", 
             sum( case when gl_balance.year= '2013' then  gl_balance.debitamount else 0 end) "2013发生额",  
             sum( case when gl_balance.year= '2012' then  gl_balance.debitamount else 0 end) "2012发生额" 
      from gl_balance, bd_accsubj, bd_glorgbook
    where gl_balance.pk_accsubj = bd_accsubj.pk_accsubj
       and bd_glorgbook.pk_glorgbook = gl_balance.pk_glorgbook
       and bd_glorgbook.glorgbookcode like '01%-0001'
       and (bd_accsubj.subjcode like '5502%' )
       and gl_balance.period<>'00'
       and gl_balance.year in('2012','2013','2014')
      group by  substr(bd_accsubj.subjcode,1,6)
    order by  substr(bd_accsubj.subjcode,1,6))a 
    order by  a.scode
 

 去除2012总一,2013总五 case 增加条件,完美

select a.*,b.subjname from (
   select       substr(bd_accsubj.subjcode,1,6)scode,
           sum( case when gl_balance.year= '2012' and bd_glorgbook.glorgbookcode<>'015101-0001'   then  gl_balance.debitamount else 0 end) "2012发生额",
           sum( case when gl_balance.year= '2013' and bd_glorgbook.glorgbookcode<>'015501-0001'  then  gl_balance.debitamount else 0 end) "2013发生额",
           sum( case when gl_balance.year= '2014' then  gl_balance.debitamount else 0 end) "2014发生额"

      from gl_balance, bd_accsubj, bd_glorgbook
    where gl_balance.pk_accsubj = bd_accsubj.pk_accsubj
       and bd_glorgbook.pk_glorgbook = gl_balance.pk_glorgbook
       and bd_glorgbook.glorgbookcode like '01%-0001'
       and (bd_accsubj.subjcode like '5502%' )
       and gl_balance.period<>'00'
       and gl_balance.year in('2012','2013','2014')
      group by  substr(bd_accsubj.subjcode,1,6)

    order by  substr(bd_accsubj.subjcode,1,6))a,bd_accsubj b
    where a.scode=b.subjcode
    and b.pk_glorgbook='0001E1100000000000MX'
    order by a.scode

 

 

银行存款的发生额

 
   select  bd_accsubj.subjcode,bd_accsubj.subjname,     
             sum( case when gl_balance.year= '2014' then  gl_balance.debitamount else 0 end) "2014借方发生额", 
              sum( case when gl_balance.year= '2014' then  gl_balance.creditamount else 0 end) "2014贷方发生额", 
             sum( case when gl_balance.year= '2013' then  gl_balance.debitamount else 0 end) "2013借方发生额",  
             sum( case when gl_balance.year= '2013' then  gl_balance.creditamount else 0 end) "2013贷方发生额",  
             sum( case when gl_balance.year= '2012' then  gl_balance.debitamount else 0 end) "2012借方发生额" ,
             sum( case when gl_balance.year= '2012' then  gl_balance.creditamount else 0 end) "2012贷方发生额" 
      from gl_balance, bd_accsubj, bd_glorgbook
    where gl_balance.pk_accsubj = bd_accsubj.pk_accsubj
       and bd_glorgbook.pk_glorgbook = gl_balance.pk_glorgbook
       and bd_glorgbook.glorgbookcode = '030101-0001'
       and (bd_accsubj.subjcode like '1002%' )
       and gl_balance.period<>'00'
       and gl_balance.year in('2012','2013','2014')
      group by bd_accsubj.subjcode,bd_accsubj.subjname 
    order by  bd_accsubj.subjcode 

 去除年末调账的项目成本和结算(NC科目余额和投标报表数字不一致)

select glorgbookcode,  nvl(replace(glorgbookname,'集团基准账薄',''),'小计')公司名称,
valuecode,valuename,sum(借方)成本,sum(贷方)收入 from

( select gl_freevalue.valuecode,
      gl_freevalue.valuename,
gl_detail.prepareddatev 制单日期 , 
       gl_voucher.no 凭证号, 
 gl_detail.explanation,
      bd_accsubj.dispname,       
       gl_detail.debitamount 借方,   
        gl_detail.creditamount 贷方,
        gl_detail.pk_systemv,
        gl_voucher.pk_voucher,
        bd_glorgbook.glorgbookcode,
        bd_glorgbook.glorgbookname           
      
         from gl_detail, bd_accsubj, bd_glorgbook,gl_freevalue,gl_voucher
 where gl_detail.pk_accsubj = bd_accsubj.pk_accsubj
   and gl_detail.pk_glorgbook = bd_glorgbook.pk_glorgbook
   and   gl_detail.assid=gl_freevalue.freevalueid
   and gl_detail.pk_voucher=gl_voucher.pk_voucher
   and gl_detail.dr = '0'
   and gl_detail.yearv='2013'
   and bd_glorgbook.glorgbookcode = '010201-0001'
   and length(gl_freevalue.valuecode)=10
   and (bd_accsubj.subjcode like '2123%' or bd_accsubj.subjcode like '4104%')--只要工程施工和工程结算
   and gl_voucher.discardflag='N'--不能是作废的凭证
   and gl_detail.pk_systemv not in('gl','TR') --去除gl(小写 期初),大写手工录入,TR自定义结转
   and gl_detail.pk_voucher not in('1038N51000000000RIGV','1038N51000000000RIKG','1038N51000000000RIM3')
   -- 去除北京分包项目结算('1038N51000000000RIGV')、税金('1038N51000000000RIKG')、成本('1038N51000000000RIM3')
   order by gl_detail.yearv,gl_detail.periodv,bd_accsubj.dispname,gl_voucher.no )
  
  group by glorgbookcode,rollup((glorgbookname, valuecode,valuename))
  order by glorgbookcode,valuecode

 2015-11-13 09:38:00

审计要的项目成本二级明细

select glorgbookcode, glorgbookname,valuecode,valuename,
substr(subjcode,1,8) 科目编码,subjname 科目名称,sum(借方)成本 from

(    select gl_freevalue.valuecode,
      gl_freevalue.valuename,
gl_detail.prepareddatev 制单日期 , 
       gl_voucher.no 凭证号, 
 gl_detail.explanation,
      bd_accsubj.subjcode,   
      bd_accsubj.subjname,    
       gl_detail.debitamount 借方,   
        gl_detail.creditamount 贷方,
        gl_detail.pk_systemv,
        gl_voucher.pk_voucher,
        bd_glorgbook.glorgbookcode,
        bd_glorgbook.glorgbookname           
      
         from gl_detail, bd_accsubj, bd_glorgbook,gl_freevalue,gl_voucher
 where gl_detail.pk_accsubj = bd_accsubj.pk_accsubj
   and gl_detail.pk_glorgbook = bd_glorgbook.pk_glorgbook
   and   gl_detail.assid=gl_freevalue.freevalueid
   and gl_detail.pk_voucher=gl_voucher.pk_voucher
   and gl_detail.dr = '0'
   and gl_detail.yearv='2013'
   and bd_glorgbook.glorgbookcode like '01%-0001'  
   and length(gl_freevalue.valuecode)=10
   and (bd_accsubj.subjcode like '410401%')--工程施工总包二级明细
   and gl_voucher.discardflag='N'--不能是作废的凭证
   and gl_detail.pk_systemv not in('gl','TR') --去除gl(小写 期初),大写手工录入,TR自定义结转
  
   order by gl_detail.yearv,gl_detail.periodv,gl_voucher.no   )
  
  group by glorgbookcode,glorgbookname, valuecode,valuename,substr(subjcode,1,8),subjname
  order by glorgbookcode,valuecode,substr(subjcode,1,8)

 三年合一

  select bd_glorgbook.glorgbookcode,
        bd_glorgbook.glorgbookname,   
   gl_freevalue.valuecode,
     gl_freevalue.valuename, 
      substr(subjcode, 1,8),  
      bd_accsubj.subjname,          
            sum( case when gl_detail.yearv='2012' then  gl_detail.debitamount else 0 end) "2012发生额",
           sum( case when gl_detail.yearv='2013' then   gl_detail.debitamount else 0 end) "2013发生额",
           sum( case when gl_detail.yearv='2014' then   gl_detail.debitamount else 0 end) "2014发生额"                  
      
         from gl_detail, bd_accsubj, bd_glorgbook,gl_freevalue,gl_voucher
 where gl_detail.pk_accsubj = bd_accsubj.pk_accsubj
   and gl_detail.pk_glorgbook = bd_glorgbook.pk_glorgbook
   and   gl_detail.assid=gl_freevalue.freevalueid
   and gl_detail.pk_voucher=gl_voucher.pk_voucher
   and gl_detail.dr = '0'   
   and bd_glorgbook.glorgbookcode like '01%-0001'  
   and length(gl_freevalue.valuecode)=10
   and (bd_accsubj.subjcode like '410401%')--工程施工总包二级明细
   and gl_voucher.discardflag='N'--不能是作废的凭证
   and gl_detail.pk_systemv not in('gl','TR') --去除gl(小写 期初),大写手工录入,TR自定义结转 
  
  group by glorgbookcode,glorgbookname, valuecode,valuename, substr(bd_accsubj.subjcode, 1,8)  ,subjname
  order by glorgbookcode,valuecode,substr(subjcode,1,8)

 

 

 

税务局审计要的营业外支出

  select     bd_glorgbook.glorgbookcode,
        bd_glorgbook.glorgbookname,   
gl_detail.prepareddatev 制单日期 , 
       gl_voucher.no 凭证号, 
 gl_detail.explanation 摘要,
   bd_accsubj.dispname 科目,       
       gl_detail.debitamount 发生额        
          
      
         from gl_detail, bd_accsubj, bd_glorgbook, gl_voucher
 where gl_detail.pk_accsubj = bd_accsubj.pk_accsubj
   and gl_detail.pk_glorgbook = bd_glorgbook.pk_glorgbook
 
   and gl_detail.pk_voucher=gl_voucher.pk_voucher
   and gl_detail.dr = '0'
   and gl_detail.yearv in ('2011','2012','2013','2014')
   and bd_glorgbook.glorgbookcode like '01%01-0001'  
 
   and  bd_accsubj.subjcode like '5601%'  
   and gl_voucher.discardflag='N'--不能是作废的凭证
   and gl_detail.pk_systemv not in('gl','TR') --去除gl(小写 期初),大写手工录入,TR自定义结转
 
   order by gl_detail.yearv,bd_glorgbook.glorgbookcode,gl_detail.periodv,gl_voucher.no 
 

 改进的工程收款sql:至本年末收款等(强大大大大大大)

select bd_glorgbook.glorgbookcode,
        bd_glorgbook.glorgbookname,   
   gl_freevalue.valuecode,
     gl_freevalue.valuename, 
           sum( case when gl_detail.yearv<=2014 then   gl_detail.creditamount else 0 end) "至本年末收款",  
            sum( case when gl_detail.yearv<2014 then  gl_detail.creditamount else 0 end) "至上年末收款",
           sum( case when gl_detail.yearv=2014 then   gl_detail.creditamount else 0 end) "本年收款"                  
      
         from gl_detail, bd_accsubj, bd_glorgbook,gl_freevalue,gl_voucher
 where gl_detail.pk_accsubj = bd_accsubj.pk_accsubj
   and gl_detail.pk_glorgbook = bd_glorgbook.pk_glorgbook
   and   gl_detail.assid=gl_freevalue.freevalueid
   and gl_detail.pk_voucher=gl_voucher.pk_voucher
   and gl_detail.dr = '0'   
   and bd_glorgbook.glorgbookcode like '01%-0001'  
   and length(gl_freevalue.valuecode)=10
   and (bd_accsubj.subjcode like '2123%')--工程施工总包二级明细
   and gl_voucher.discardflag='N'--不能是作废的凭证
   and gl_detail.pk_systemv not in('gl','TR') --去除gl(小写 期初),大写手工录入,TR自定义结转 
     group by glorgbookcode,glorgbookname, valuecode,valuename
  order by glorgbookcode,valuecode

 收入成本合成

select * from (
select bd_glorgbook.glorgbookcode,
        bd_glorgbook.glorgbookname,   
   gl_freevalue.valuecode,
     gl_freevalue.valuename, 
        
           sum( case when gl_detail.yearv=2012 then   gl_detail.creditamount else 0 end) INCOME,    
           sum( case when gl_detail.yearv=2012 then   gl_detail.debitamount else 0 end) COST                
      
         from gl_detail, bd_accsubj, bd_glorgbook,gl_freevalue,gl_voucher
 where gl_detail.pk_accsubj = bd_accsubj.pk_accsubj
   and gl_detail.pk_glorgbook = bd_glorgbook.pk_glorgbook
   and   gl_detail.assid=gl_freevalue.freevalueid
   and gl_detail.pk_voucher=gl_voucher.pk_voucher
   and gl_detail.dr = '0'   
   and bd_glorgbook.glorgbookcode like '03%-0001'  
   and length(gl_freevalue.valuecode)=10
   and (bd_accsubj.subjcode like '2123%' or bd_accsubj.subjcode like '4104%' )--工程施工总包二级明细
   and gl_voucher.discardflag='N'--不能是作废的凭证
   and gl_detail.pk_systemv not in('gl','TR') --去除gl(小写 期初),大写手工录入,TR自定义结转 
  
     group by glorgbookcode,glorgbookname, valuecode,valuename)
     where INCOME<>0 OR COST<>0
  order by glorgbookcode,valuecode

建工发债sql_第1张图片

各种税(使用余额表)

   select     bd_accsubj.subjcode,bd_accsubj.dispname,
           sum( case when gl_balance.year= '2012'     then  gl_balance.debitamount else 0 end) "2012发生额",
           sum( case when gl_balance.year= '2013'    then  gl_balance.debitamount else 0 end) "2013发生额",
           sum( case when gl_balance.year= '2014' then  gl_balance.debitamount else 0 end) "2014发生额"

      from gl_balance, bd_accsubj, bd_glorgbook
    where gl_balance.pk_accsubj = bd_accsubj.pk_accsubj
       and bd_glorgbook.pk_glorgbook = gl_balance.pk_glorgbook
       and bd_glorgbook.glorgbookcode like '01%-0001'
       and (bd_accsubj.subjcode like '2171%' or bd_accsubj.subjcode like '2176%' )
       and gl_balance.period<>'00'
       and gl_balance.year in('2012','2013','2014')
      group by   bd_accsubj.subjcode, bd_accsubj.dispname

    order by  bd_accsubj.subjcode

 完成每个公司前100付款客商

select * from (
select TA.*,rank()over(partition by 付款单位 order by 付款总金额 desc) rn from
(
select distinct bd_corp.unitcode, bd_corp.unitname as 付款单位,
bd_cubasdoc.custcode as 收款单位编码 ,bd_cubasdoc.custname as 收款单位名称, 
sum(arap_djfb.bbye) over(partition by bd_corp.unitcode,bd_cubasdoc.custcode )as 付款总金额 

from bd_cubasdoc,arap_djfb,bd_cumandoc,arap_djzb,bd_corp
where arap_djfb.ksbm_cl=bd_cumandoc.pk_cumandoc 
and bd_cumandoc.pk_cubasdoc= bd_cubasdoc.pk_cubasdoc 
and arap_djfb.vouchid= arap_djzb.vouchid
and bd_corp.pk_corp=arap_djzb.dwbm
and arap_djfb.payflag in('1','2')
and arap_djfb.dr='0' 
and length(bd_cubasdoc.custname)>3
and length(bd_cubasdoc.custcode)<>6
and bd_corp.unitcode not in('0100','010101'))TA)
where rn<=100

直接order by 的话出现的结果只有钢结构,因此需要partition 才能出现各个公司的前100

使用partition 必须order by,并且从大到小排练还要desc(默认asc从小到大)

建工发债sql_第2张图片

 供应商top

select  
bd_cubasdoc.custcode as 收款单位编码 ,bd_cubasdoc.custname as 收款单位名称, 
sum(arap_djfb.bbye) 收款总金额

from bd_cubasdoc,bd_cumandoc,arap_djfb,arap_djzb 
where arap_djfb.ksbm_cl=bd_cumandoc.pk_cumandoc 
and bd_cumandoc.pk_cubasdoc= bd_cubasdoc.pk_cubasdoc 
and arap_djfb.vouchid= arap_djzb.vouchid
 
and arap_djfb.payflag in('1','2')
and arap_djfb.dr='0' 
and length(bd_cubasdoc.custname)>3
and length(bd_cubasdoc.custcode) not in (6,4)
and arap_djzb.djkjnd=2014
group by bd_cubasdoc.custcode,bd_cubasdoc.custname ,arap_djfb.bbye
order by sum(arap_djfb.bbye) desc

 

增加日期:and arap_djzb.djrq between  '2015-01-01'and '2015-09-31'

Oracle中rank() over, dense_rank(), row_number() 的区别

假设现在有一张学生表student,学生表中有姓名、分数、课程编号,现在我需要按照课程对学生的成绩进行排序。

select * from student

建工发债sql_第3张图片

1. rank over ()可以实现对学生排名,特点是成绩相同的两名是并列,如下1 2 2 4 5

select name,
      course,
      rank() over(partition by course order by score desc) as rank
  from student;

建工发债sql_第4张图片

2. dense_rank()和rank over()很像,但学生成绩并列后并不会空出并列所占的名次,如下1 2 2 3 4

select name,
      course,
      dense_rank() over(partition by course order by score desc) as rank
  from student;

建工发债sql_第5张图片

3. row_number这个函数不需要考虑是否并列,那怕根据条件查询出来的数值相同也会进行连续排名

select name,
      course,
      row_number() over(partition by course order by score desc) as rank
  from student;

建工发债sql_第6张图片


答疑:

1. partition by用于给结果集进行分区。

2. partition by和group by有何区别?

partition by只是将原始数据进行名次排列(记录数不变)


group by是对原始数据进行聚合统计(记录数可能变少, 每组返回一条)

3. 使用rank over()的时候,空值是最大的,如果排序字段为null, 可能造成null字段排在最前面,影响排序结果。

可以这样: rank over(partition by course order by score desc nulls last)

你可能感兴趣的:(建工发债sql)