需求:
步骤:
1,自建和集团分配科目的字段 pk_create_glorgbook,事实证明bd_accsubj中的createcorp没用,科目表中的创建主体应该是pk_create_glorgbook
也就是bd_glorgbook中的pk_glorgbook
2,之后通过平级的subjcode来统计pk_create_glorgbook的不同数量,如果>=2则满足要求
附注:平级subjcode来统计分析不同的pk_create_glorgbook,分组需要是上一级的科目,因为科目编码是2位数一个级次,因此可以
count(distinct pk_create_glorgbook) over (partition by glorgbookname,substr(subjcode,1,length(subjcode)-2))
substr(subjcode,1,length(subjcode)-2)通过这个找到上一级,确实是秒啊
这里partition加上了glorgbookname,是为了要统计集团所有的,where条件里面可以去掉单个会计账簿
sql如下:
select * from (select count(distinct pk_create_glorgbook) over (partition by glorgbookname,substr(subjcode,1,length(subjcode)-2)) rn,t.* from (select bd_glorgbook.glorgbookname, bd_accsubj.subjcode, bd_accsubj.dispname, bd_accsubj.pk_accsubj, bd_accsubj.balanorient, bd_accsubj.pk_create_glorgbook,--创建会计主体 wmsys.wm_concat(bd_bdinfo.bdname) 辅助项, bd_accsubj.ctlsystem from bd_accsubj left join bd_subjass on bd_accsubj.pk_accsubj = bd_subjass.pk_accsubj left join bd_bdinfo on bd_subjass.pk_bdinfo = bd_bdinfo.pk_bdinfo join bd_glorgbook on bd_glorgbook.pk_glorgbook = bd_accsubj.pk_glorgbook --where bd_glorgbook.glorgbookcode = '010201-0001' group by bd_accsubj.subjcode, bd_accsubj.dispname, bd_accsubj.pk_accsubj,bd_accsubj.balanorient, bd_accsubj.pk_create_glorgbook, bd_accsubj.ctlsystem,bd_glorgbook.glorgbookname order by bd_accsubj.subjcode)t ) where rn>=2