select b.*,扣款合计=b.节时数*a.扣款标准
from 表1 a join(
select 培养层次,项目,节时数=sum(节时数)
from 表2
where 学号='0001000201' and 培养层次='08'
group by 培养层次,项目
)b on a.项目=b.项目 and a.培养目标=b.培养层次
where a.阀值>=b.培养层次 and a.阀值<=(
select min(阀值) from 表1
where 项目=a.项目 and 培养目标=a.培养目标
and 阀值>b.培养层次)
from 表1 a join(
select 培养层次,项目,节时数=sum(节时数)
from 表2
where 学号='0001000201' and 培养层次='08'
group by 培养层次,项目
)b on a.项目=b.项目 and a.培养目标=b.培养层次
where a.阀值>=b.培养层次 and a.阀值<=(
select min(阀值) from 表1
where 项目=a.项目 and 培养目标=a.培养目标
and 阀值>b.培养层次)
create table 表1(项目 char(6),培养目标 char(2),阀值 int,扣款标准 decimal(10,1))
insert 表1 select '040000','08',1, 0.2
union all select '040000','08',19,0.5
union all select '040000','08',39,1.0
union all select '040000','08',49,1.5
union all select '050100','08',1, 0.3
union all select '050000','08',19,0.6
union all select '050000','08',29,0.9
union all select '050000','08',39,1.5
union all select '060000','08',1, 1.6
union all select '040000','07',1, 0.6create table 表2(学号 char(10),培养层次 char(2),项目 char(6),节时数 int)
insert 表2 select '0001000201','08','050000',9
union all select '0001000201','08','050000',5
union all select '0001000201','08','050000',9
union all select '0001000201','08','050000',5
go--查询
select b.*,扣款合计=b.节时数*a.扣款标准
from 表1 a join(
select 培养层次,项目,节时数=sum(节时数)
from 表2
where 学号='0001000201' and 培养层次='08'
group by 培养层次,项目
)b on a.项目=b.项目 and a.培养目标=b.培养层次
where a.阀值>=b.培养层次 and a.阀值<=(
select min(阀值) from 表1
where 项目=a.项目 and 培养目标=a.培养目标
and 阀值>b.培养层次)
go--删除测试
drop table 表1,表2/*--测试结果培养层次 项目 节时数 扣款合计
---- ------ ----------- -----------------------
08 050000 28 16.8(所影响的行数为 1 行)
--*/
from 表1 a join(
select 培养层次,项目,节时数=sum(节时数)
from 表2
where 学号='0001000201' and 培养层次='08'
group by 培养层次,项目
)b on a.项目=b.项目 and a.培养目标=b.培养层次
where b.节时数 between a.阀值 and(
select min(阀值) from 表1
where 项目=a.项目 and 培养目标=a.培养目标
and 阀值>b.节时数)
select b.*,扣款合计=b.节时数*a.扣款标准
from 表1 a join(
select 培养层次,项目,节时数=sum(节时数)
from 表2
where 学号='0001000201' and 培养层次='08'
group by 培养层次,项目
)b on a.项目=b.项目 and a.培养目标=b.培养层次
and b.节时数 between a.阀值上限 and a.a.阀值下限
from 表1 a join(
select 培养层次,项目,节时数=sum(节时数)
from 表2
where 学号='0001000201' and 培养层次='08'
group by 培养层次,项目
)b on a.项目=b.项目 and a.培养目标=b.培养层次
and b.节时数 between a.阀值上限 and a.阀值下限 --上面多了一点a.
select b.*,扣款合计=b.节时数*a.扣款标准
from 表1 a join(
select 培养层次,项目,节时数=sum(节时数)/节时数 在表二中
from 表2
where 学号='0001000201' and 培养层次='08')/学号 在表二中
group by 培养层次,项目