日期:2014-05-17 浏览次数:20723 次
表A:计划表
LX MMMC FS JHJE
1 XM1 A 1000.00
1 XM1 B 500.00
1 XM2 A 600.00
1 XM3 A 800.00
1 XM4 A 700.00
表B:执行表
LX MMMC FS ZXJE
1 XM1 A 500.00
1 XM1 B 200.00
1 XM2 A 300.00
1 XM3 A 400.00
结果表:
LX MMMC FS JYJE
1 XM1 A 500.00
1 XM1 B 300.00
1 XM2 A 300.00
1 XM3 A 400.00
1 XM4 A 700.00
如果没有执行,那么在表B:执行表 是不会出现数据的,如:XM4
但是结果表,是需要没有执行的结余数据的,如:XM4 结余金额700.00
求个结果表SQL:
if(object_id('a')is not null) drop table a
go
create table a
(
LX INT,
MMMC VARCHAR(3),
FS VARCHAR(1),
JHJE DECIMAL(18,2)
)
GO
INSERT INTO A
SELECT 1,'XM1','A',1000.00 UNION ALL
SELECT 1,'XM1','B',500.00 UNION ALL
SELECT 1,'XM2','A',600.00 UNION ALL
SELECT 1,'XM3','A',800.00 UNION ALL
SELECT 1,'XM4','A',700.00
GO
IF(OBJECT_ID('B')IS NOT NULL) DROP TABLE B
GO
create table B
(
LX INT,
MMMC VARCHAR(3),
FS VARCHAR(1),
ZXJE DECIMAL(18,2)
)
GO
INSERT INTO B
SELECT 1,'XM1','A',500.00 UNION ALL
SELECT 1,'XM1','B',200.00 UNION ALL
SELECT 1,'XM2','A',300.00 UNION ALL
SELECT 1,'XM3','A',400.00
go
select a.lx,a.mmmc,a.fs,case when b.zxje is not null then b.zxje else a.jhje end as JYJE
from a left outer join b&