--类似这样就行了(其他字段不知道你要怎么赋值?) insert kmb(kmbm,kmmc,jldw,zwm,sjmj) select kmbm,jmmc,jldw ,case hsdm when '1' then '资产' when '2' then '负债' when '3' then '权益' when '4' then '成本' when '5' then '损益' end ,case when exists(select * from km where kmbm like a.kmbm+'_%') then 0 else 1 end from km a
感觉又是一个N叉树的深度遍历问题.级次就是kmbm的长度.根据你说的3223规则那么 case when len(kmbm)=3 then kmjc='1' case when len(kmbm)=5 then kmjc='2' case when len(kmbm)=7 then kmjc='3' case when len(kmbm)=10 then kmjc='4' 但怎么判断是不是末级,好象感觉要写函数了.
insert kmb(km,km_name,jl,zwm,engl,sfmj,bmjc) --bgs无法从km表中取,所以你得单独处理,不要写在列表中 select kmbh,kmmc,jldw ,case hsdm --对应 zwm when '1' then '资产' when '2' then '负债' when '3' then '权益' when '4' then '成本' when '5' then '损益' end ,case hsdm --对应 engl when '1' then '资产' when '2' then '负债' when '3' then '权益' when '4' then '成本' when '5' then '损益' end ,case --对应 sfmj when exists(select * from km where kmbm like a.kmbm+'_%') then 0 else 1 end ,case len(kmbh) --对应 bmjc when 3 then 1 when 5 then 2 when 7 then 3 when 10 then 4 end from km a
建议楼主还是要看邹老师的思路,而细节问题应该你自己来解决. 邹老师已经用 ,case len(kmbh) --对应 bmjc when 3 then 1 when 5 then 2 when 7 then 3 when 10 then 4帮你解决了级次问题. 又用 case --对应 sfmj when exists(select * from km where kmbm like a.kmbm+'_%') 帮你解决了是否是末级别的问题. 你只要验证就可以了.
Rose,我写出来了,你测试下行不行1、要根据kmbm的长度来判断记录的编码级次(bmjc)并写入数据。如:编码规则(3223),kmbm=101,则bmjc=1;10101为二级,bmjc=2,则1010101001为四级。update kmb set bmjc=(case when len(a.kmbm)=3 then 1 when len(a.kmbm)=5 then 2 when len(a.kmbm)=7 then 3 when len(a.kmbm)=10 then 4 end) from kmb a2.如何根据kmbm来判断其是否属于末级(sfmj)后写入数据,如:kmbm以101开头的有两条记录,分别是101,10101,则101为非末级,10101为末级。 update kmb set sfmj=1 from kmb where bmjc=4 or (bmjc=3 and kmbm not in (select a.kmbm from kmb a,kmb b where a.bmjc=3 and b.bmjc=4 and a.kmbm=left(b.kmbm,7))) or (bmjc=2 and kmbm not in (select a.kmbm from kmb a,kmb b where a.bmjc=2 and b.bmjc=3 and a.kmbm=left(b.kmbm,5))) or (bmjc=1 and kmbm not in (select a.kmbm from kmb a,kmb b where a.bmjc=1 and b.bmjc=2 and a.kmbm=left(b.kmbm,3)))注意,要先做第一步后才能做第二步,因为第二步里我用了第一步中更新过的栏位 第二步的思路如下首先bmjc=4的肯定是末级,对应的sfmj=1 然后判断bmjc=3而且没有其他前7位与它相同的,则sfmj=1 然后判断bmjc=2而且没有其他前5位与它相同的,则sfmj=1 然后判断bmjc=2而且没有其他前3位与它相同的,则sfmj=1也就是采用从后往前推的方法,昨晚一开始我一直试图从前往后推,所以一下子卡住了,呵呵
好像还了个条件可以去掉update kmb set sfmj=1 from kmb where bmjc=4 or (bmjc=3 and kmbm not in (select a.kmbm from kmb a,kmb b where b.bmjc=4 and a.kmbm=left(b.kmbm,7))) or (bmjc=2 and kmbm not in (select a.kmbm from kmb a,kmb b where b.bmjc=3 and a.kmbm=left(b.kmbm,5))) or (bmjc=1 and kmbm not in (select a.kmbm from kmb a,kmb b where b.bmjc=2 and a.kmbm=left(b.kmbm,3)))
:(((
简单点,楼主给出一些实际的数据,然后列出你想要的结果
insert kmb(kmbm,kmmc,jldw,zwm,sjmj)
select kmbm,jmmc,jldw
,case hsdm
when '1' then '资产'
when '2' then '负债'
when '3' then '权益'
when '4' then '成本'
when '5' then '损益'
end
,case when exists(select * from km where kmbm like a.kmbm+'_%') then 0 else 1 end
from km a
case when len(kmbm)=3 then kmjc='1'
case when len(kmbm)=5 then kmjc='2'
case when len(kmbm)=7 then kmjc='3'
case when len(kmbm)=10 then kmjc='4'
但怎么判断是不是末级,好象感觉要写函数了.
km
kmbh kmmc jldw hsdm .....
(科目编码)(科目名称)(计量单位)(科目性质编码) .....
101 现金 1
10101 人民币 1
12401 库存商品 册 2kmb(空表)
kmbm kmmc jldw zwm engl sfmj bgs bmjc ....
(科目编码)(科目名称)(计量单位)(中文名)(英文名)(是否末级)(帐表格式)(编码级次) ....将km表数据导入kmb表后结果要求如下:
kmb
kmbm kmmc jldw zwm engl sfmj bgs bmjc ....
(科目编码)(科目名称)(计量单位)(中文名)(英文名)(是否末级)(帐表格式)(编码级次) ....
101 现金 资产 ZC 0 金额式 1
10101 人民币 资产 ZC 1 金额式 2
12401 库存商品 册 资产 ZC 1 数量金额式 2 ....
.......
km
kmbh kmmc jldw hsdm .....
(科目编码)(科目名称)(计量单位)(科目性质编码) .....
101 现金 1
10101 人民币 1
12401 库存商品 册 2kmb(空表)
km km_name jl zwm engl sfmj bgs bmjc ....
(科目编码)(科目名称)(计量单位)(中文名)(英文名)(是否末级)(帐表格式)(编码级次) ....将km表数据导入kmb表后结果要求如下:
kmb
km km_name jl zwm engl sfmj bgs bmjc ....
(科目编码)(科目名称)(计量单位)(中文名)(英文名)(是否末级)(帐表格式)(编码级次) ....
101 现金 资产 ZC 0 金额式 1
10101 人民币 资产 ZC 1 金额式 2
12401 库存商品 册 资产 ZC 1 数量金额式 2 ....
insert kmb(km,km_name,jl,zwm,engl,sfmj,bmjc) --bgs无法从km表中取,所以你得单独处理,不要写在列表中
select kmbh,kmmc,jldw
,case hsdm --对应 zwm
when '1' then '资产'
when '2' then '负债'
when '3' then '权益'
when '4' then '成本'
when '5' then '损益'
end
,case hsdm --对应 engl
when '1' then '资产'
when '2' then '负债'
when '3' then '权益'
when '4' then '成本'
when '5' then '损益'
end
,case --对应 sfmj
when exists(select * from km where kmbm like a.kmbm+'_%') then 0
else 1
end
,case len(kmbh) --对应 bmjc
when 3 then 1
when 5 then 2
when 7 then 3
when 10 then 4
end
from km a
邹老师已经用
,case len(kmbh) --对应 bmjc
when 3 then 1
when 5 then 2
when 7 then 3
when 10 then 4帮你解决了级次问题.
又用
case --对应 sfmj
when exists(select * from km where kmbm like a.kmbm+'_%')
帮你解决了是否是末级别的问题.
你只要验证就可以了.
when len(a.kmbm)=5 then 2
when len(a.kmbm)=7 then 3
when len(a.kmbm)=10 then 4 end)
from kmb a2.如何根据kmbm来判断其是否属于末级(sfmj)后写入数据,如:kmbm以101开头的有两条记录,分别是101,10101,则101为非末级,10101为末级。
update kmb set sfmj=1
from kmb
where bmjc=4
or
(bmjc=3 and kmbm not in
(select a.kmbm from kmb a,kmb b
where a.bmjc=3 and b.bmjc=4 and a.kmbm=left(b.kmbm,7)))
or
(bmjc=2 and kmbm not in
(select a.kmbm from kmb a,kmb b
where a.bmjc=2 and b.bmjc=3 and a.kmbm=left(b.kmbm,5)))
or
(bmjc=1 and kmbm not in
(select a.kmbm from kmb a,kmb b
where a.bmjc=1 and b.bmjc=2 and a.kmbm=left(b.kmbm,3)))注意,要先做第一步后才能做第二步,因为第二步里我用了第一步中更新过的栏位
第二步的思路如下首先bmjc=4的肯定是末级,对应的sfmj=1
然后判断bmjc=3而且没有其他前7位与它相同的,则sfmj=1
然后判断bmjc=2而且没有其他前5位与它相同的,则sfmj=1
然后判断bmjc=2而且没有其他前3位与它相同的,则sfmj=1也就是采用从后往前推的方法,昨晚一开始我一直试图从前往后推,所以一下子卡住了,呵呵
from kmb
where bmjc=4
or
(bmjc=3 and kmbm not in
(select a.kmbm from kmb a,kmb b
where b.bmjc=4 and a.kmbm=left(b.kmbm,7)))
or
(bmjc=2 and kmbm not in
(select a.kmbm from kmb a,kmb b
where b.bmjc=3 and a.kmbm=left(b.kmbm,5)))
or
(bmjc=1 and kmbm not in
(select a.kmbm from kmb a,kmb b
where b.bmjc=2 and a.kmbm=left(b.kmbm,3)))