declare @t1 table(id int,f1 varchar(10),f2 int,f3 varchar(20))
insert into @t1 select 1,rtrim('101 '),0,rtrim('现金 ')
insert into @t1 select 2,rtrim('10101 '),1,rtrim('人民币 ')
insert into @t1 select 3,rtrim('10102 '),1,rtrim('港币 ')
insert into @t1 select 4,rtrim('10103 '),1,rtrim('美金 ')
insert into @t1 select 5,rtrim('102 '),0,rtrim('银行存款')
insert into @t1 select 6,rtrim('10201 '),0,rtrim('中国银行')
insert into @t1 select 7,rtrim('1020101'),1,rtrim('中国银行A支行')
insert into @t1 select 8,rtrim('10202'),1,rtrim('中国工商银行')
insert into @t1 select 9,rtrim('131 '),1,rtrim('材料成本差异')
insert into @t1 select 10,rtrim('405 '),0,rtrim('生产成本')
insert into @t1 select 11,rtrim('40501 '),0,rtrim('辅助费用')
insert into @t1 select 12,rtrim('4050101 '),0,rtrim('辅助费用1')
insert into @t1 select 13,rtrim('405010101 '),1,rtrim('辅助费用11')
insert into @t1 select 14,rtrim('405010102 '),1,rtrim('辅助费用12')
insert into @t1 select 15,rtrim('40502 '),1,rtrim('其它费用')declare @t2 table(id int,f1 varchar(10),f2 int,f3 int)
insert into @t2 select 1,rtrim('10101 '),10 ,30
insert into @t2 select 2,rtrim('10102 '),30 ,null
insert into @t2 select 3,rtrim('10201 '),null,null
insert into @t2 select 4,rtrim('1020101'),40 ,50
insert into @t2 select 5,rtrim('10202 '),60,20
insert into @t2 select 6,rtrim('131 '),null,60
insert into @t2 select 7,rtrim('405010101 '),10,70
insert into @t2 select 8,rtrim('405010102 '),20,30
insert into @t2 select 9,rtrim('40502 '),10,50(1)科目级别=1时
select
min(b.id) as id ,
a.f1,
sum(b.f2) as f2,
sum(b.f3) as f3,
a.f3 as f4
from
@t1 a,@t2 b
where
a.f1=left(b.f1,3) and a.f1<=left(b.f1,3)
group by
a.f1,a.f3
order by
a.f1显示结果:
id f1 f2 f3 f4
1 101 40 30 现金
3 102 100 70 银行存款
6 131 NULL 60 材料成本差异
7 405 40 150 生产成本
显示正确。
(2)、科目级别=2时select
min(b.id) as id ,
a.f1,
sum(b.f2) as f2,
sum(b.f3) as f3,
a.f3 as f4
from
@t1 a,@t2 b
where
a.f1>=left(b.f1,3) and a.f1<=left(b.f1,5)
group by
a.f1,a.f3
order by
a.f1 显示结果:
id f1 f2 f3 f4
1 101 40 30 现金
1 10101 40 30 人民币
2 10102 30 NULL 港币
3 102 100 70 银行存款
3 10201 100 70 中国银行
5 1020101 60 20 中国银行A支行
5 10202 60 20 中国工商银行
6 131 NULL 60 材料成本差异
7 405 40 150 生产成本
7 40501 40 150 辅助费用
9 4050101 10 50 辅助费用1
9 405010101 10 50 辅助费用11
9 405010102 10 50 辅助费用12
9 40502 10 50 其它费用显示不正确,'10101'的f2不是等于‘40’,而是‘10’;'10201'的f2不是等于‘100’,而是‘40’,f3不是等于‘70’,而是'50',因为'10201'的f2是由下一级'1020101'取出来的,'10201'的f3也是由一级来取出来的;'1020101'、'4050101'、'405010101'、'405010102'不能显示出来,由于这三个不是2级科目,是属于3级和4级科目;'40501'的f2和f3分别应该是'30'和'100',而不是'40'和'150'。我希望的结果是这样:
id f1 f2 f3 f4
1 101 40 30 现金
1 10101 10 30 人民币
2 10102 30 NULL 港币
3 102 100 70 银行存款
3 10201 40 50 中国银行
5 10202 60 20 中国工商银行
6 131 NULL 60 材料成本差异
7 405 40 150 生产成本
7 40501 30 100 辅助费用
9 40502 10 50 其它费用
(3)、科目级别=3时:
select
min(b.id) as id ,
a.f1,
sum(b.f2) as f2,
sum(b.f3) as f3,
a.f3 as f4
from
@t1 a,@t2 b
where
a.f1>=left(b.f1,3) and a.f1<=left(b.f1,7)
group by
a.f1,a.f3
order by
a.f1 显示结果:
id f1 f2 f3 f4
1 101 40 30 现金
1 10101 40 30 人民币
2 10102 30 NULL 港币
3 102 100 70 银行存款
3 10201 100 70 中国银行
5 1020101 60 20 中国银行A支行
5 10202 60 20 中国工商银行
6 131 NULL 60 材料成本差异
7 405 40 150 生产成本
7 40501 40 150 辅助费用
9 4050101 10 50 辅助费用1
9 405010101 10 50 辅助费用11
9 405010102 10 50 辅助费用12
9 40502 10 50 其它费用显示不正确,应该这样的
id f1 f2 f3 f4
1 10101 10 30 人民币
2 10102 30 NULL 港币
3 102 100 70 银行存款
3 10201 40 50 中国银行
5 1020101 40 50 中国银行A支行
5 10202 60 20 中国工商银行
6 131 NULL 60 材料成本差异
7 405 40 150 生产成本
7 40501 30 100 辅助费用
9 4050101 30 100 辅助费用1
9 40502 10 50 其它费用请问如何改这个sql语句呢?
insert into @t1 select 1,rtrim('101 '),0,rtrim('现金 ')
insert into @t1 select 2,rtrim('10101 '),1,rtrim('人民币 ')
insert into @t1 select 3,rtrim('10102 '),1,rtrim('港币 ')
insert into @t1 select 4,rtrim('10103 '),1,rtrim('美金 ')
insert into @t1 select 5,rtrim('102 '),0,rtrim('银行存款')
insert into @t1 select 6,rtrim('10201 '),0,rtrim('中国银行')
insert into @t1 select 7,rtrim('1020101'),1,rtrim('中国银行A支行')
insert into @t1 select 8,rtrim('10202'),1,rtrim('中国工商银行')
insert into @t1 select 9,rtrim('131 '),1,rtrim('材料成本差异')
insert into @t1 select 10,rtrim('405 '),0,rtrim('生产成本')
insert into @t1 select 11,rtrim('40501 '),0,rtrim('辅助费用')
insert into @t1 select 12,rtrim('4050101 '),0,rtrim('辅助费用1')
insert into @t1 select 13,rtrim('405010101 '),1,rtrim('辅助费用11')
insert into @t1 select 14,rtrim('405010102 '),1,rtrim('辅助费用12')
insert into @t1 select 15,rtrim('40502 '),1,rtrim('其它费用')declare @t2 table(id int,f1 varchar(10),f2 int,f3 int)
insert into @t2 select 1,rtrim('10101 '),10 ,30
insert into @t2 select 2,rtrim('10102 '),30 ,null
insert into @t2 select 3,rtrim('10201 '),null,null
insert into @t2 select 4,rtrim('1020101'),40 ,50
insert into @t2 select 5,rtrim('10202 '),60,20
insert into @t2 select 6,rtrim('131 '),null,60
insert into @t2 select 7,rtrim('405010101 '),10,70
insert into @t2 select 8,rtrim('405010102 '),20,30
insert into @t2 select 9,rtrim('40502 '),10,50(1)科目级别=1时
select
min(b.id) as id ,
a.f1,
sum(b.f2) as f2,
sum(b.f3) as f3,
a.f3 as f4
from
@t1 a,@t2 b
where
a.f1=left(b.f1,3) and a.f1<=left(b.f1,3)
group by
a.f1,a.f3
order by
a.f1显示结果:
id f1 f2 f3 f4
1 101 40 30 现金
3 102 100 70 银行存款
6 131 NULL 60 材料成本差异
7 405 40 150 生产成本
显示正确。
(2)、科目级别=2时select
min(b.id) as id ,
a.f1,
sum(b.f2) as f2,
sum(b.f3) as f3,
a.f3 as f4
from
@t1 a,@t2 b
where
a.f1>=left(b.f1,3) and a.f1<=left(b.f1,5)
group by
a.f1,a.f3
order by
a.f1 显示结果:
id f1 f2 f3 f4
1 101 40 30 现金
1 10101 40 30 人民币
2 10102 30 NULL 港币
3 102 100 70 银行存款
3 10201 100 70 中国银行
5 1020101 60 20 中国银行A支行
5 10202 60 20 中国工商银行
6 131 NULL 60 材料成本差异
7 405 40 150 生产成本
7 40501 40 150 辅助费用
9 4050101 10 50 辅助费用1
9 405010101 10 50 辅助费用11
9 405010102 10 50 辅助费用12
9 40502 10 50 其它费用显示不正确,'10101'的f2不是等于‘40’,而是‘10’;'10201'的f2不是等于‘100’,而是‘40’,f3不是等于‘70’,而是'50',因为'10201'的f2是由下一级'1020101'取出来的,'10201'的f3也是由一级来取出来的;'1020101'、'4050101'、'405010101'、'405010102'不能显示出来,由于这三个不是2级科目,是属于3级和4级科目;'40501'的f2和f3分别应该是'30'和'100',而不是'40'和'150'。我希望的结果是这样:
id f1 f2 f3 f4
1 101 40 30 现金
1 10101 10 30 人民币
2 10102 30 NULL 港币
3 102 100 70 银行存款
3 10201 40 50 中国银行
5 10202 60 20 中国工商银行
6 131 NULL 60 材料成本差异
7 405 40 150 生产成本
7 40501 30 100 辅助费用
9 40502 10 50 其它费用
(3)、科目级别=3时:
select
min(b.id) as id ,
a.f1,
sum(b.f2) as f2,
sum(b.f3) as f3,
a.f3 as f4
from
@t1 a,@t2 b
where
a.f1>=left(b.f1,3) and a.f1<=left(b.f1,7)
group by
a.f1,a.f3
order by
a.f1 显示结果:
id f1 f2 f3 f4
1 101 40 30 现金
1 10101 40 30 人民币
2 10102 30 NULL 港币
3 102 100 70 银行存款
3 10201 100 70 中国银行
5 1020101 60 20 中国银行A支行
5 10202 60 20 中国工商银行
6 131 NULL 60 材料成本差异
7 405 40 150 生产成本
7 40501 40 150 辅助费用
9 4050101 10 50 辅助费用1
9 405010101 10 50 辅助费用11
9 405010102 10 50 辅助费用12
9 40502 10 50 其它费用显示不正确,应该这样的
id f1 f2 f3 f4
1 10101 10 30 人民币
2 10102 30 NULL 港币
3 102 100 70 银行存款
3 10201 40 50 中国银行
5 1020101 40 50 中国银行A支行
5 10202 60 20 中国工商银行
6 131 NULL 60 材料成本差异
7 405 40 150 生产成本
7 40501 30 100 辅助费用
9 4050101 30 100 辅助费用1
9 40502 10 50 其它费用请问如何改这个sql语句呢?
解决方案 »
- |zyciis| 如何查询出数据库中的全有我分立的表约束
- 先安装sql2005后装了sql2000结果2000不能运行,如何解决呀?
- 如何处理大型数据库的历史数据?
- 关于 FTP 服务器 多级文件拷贝出现的错误(20层)
- 寻求《数据库系统实现》《数据库系统概念》的电子书(都是杨冬青译)
- 求教:装不了SQL Server的服务器组件,怎么办?
- 请问MSSQLSERVER服务自动关闭 事件跟踪里出现一个错误ID17055,一个警告ID19011怎么回事?
- 为什么group by 和order by 无法同时使用?
- 还原数据库一共有几种方法
- 奇怪,简单的问题,能帮忙吗?
- SQL-DMO对象中的路径问题
- sql server中我想得到某个表刚新增加的记录的ID,大家讨论一下哪种方式最好??
set @level=3select
a.f1,sum(b.f2),sum(b.f3),f4=a.f3
from
@t1 a,@t2 b
where
b.f1 like a.f1+'%' and (len(a.f1)-1)/2<=@level
group by
a.id,a.f1,a.f3
order by
a.id