首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

三个表的一个计算难住了.高手帮忙解决办法

2012-04-04 
三个表的一个计算难住了.高手帮忙三个表1.工资表 a表km je1 52 103 154 202.物资表 b表bh lx(0,1)3.使用表

三个表的一个计算难住了.高手帮忙
三个表
1.工资表 a表
km je
1 5
2 10
3 15
4 20
2.物资表 b表
bh lx(0,1)
3.使用表 c表
bh sl(+,-)

需求:
b表的lx为0时,c表的sl为+就把ABC(sl)*a表km为1的je
b表的lx为0时,c表的sl为-就把ABC(sl)*a表km为2的je
b表的lx为1时,c表的sl为+就把ABC(sl)*a表km为3的je
b表的lx为1时,c表的sl为-就把ABC(sl)*a表km为4的je
b.bh=c.bh

联合语句写条件,把这个使用过的物质金额算出来.



[解决办法]
把表的结构和结果贴好点。这样看不明白
[解决办法]
这么复杂 0 0
[解决办法]

SQL code
--> 测试数据: @a表declare @a表 table (km int,je int)insert into @a表select 1,5 union allselect 2,10 union allselect 3,15 union allselect 4,20--> 测试数据: @b表declare @b表 table (bh int,lx int)insert into @b表select 1,0 union allselect 2,1 union allselect 3,1 union allselect 4,0 union allselect 5,1--> 测试数据: @c表declare @c表 table (bh int,sl varchar(2))insert into @c表select 1,'+' union allselect 2,'-' union allselect 3,'+' union allselect 4,'-' union allselect 5,'-'select c.*,b.lx,case when c.sl='+' and b.lx=0 then (select je from @a表 where km=1)when c.sl='-' and b.lx=0 then (select je from @a表 where km=2)when c.sl='+' and b.lx=1 then (select je from @a表 where km=3)when c.sl='-' and b.lx=1 then (select je from @a表 where km=4)end as jefrom @c表 c left join @b表 b on c.bh=b.bh/*bh          sl   lx          je----------- ---- ----------- -----------1           +    0           52           -    1           203           +    1           154           -    0           105           -    1           20*/ 

热点排行