有人可以解释为什么如果我在下面运行查询时,未从联合的上半部分正确添加行吗?我以为工会只能消除重复,而不是价值观?如果我将其全部合并,它将对其进行修复,我只是不确定为什么需要它?谢谢,jb
CREATE TABLE #c3 (idNbr int, idValue int, idP int);
insert into #c3 (idNbr, idValue, idP) select 1 , 1 , 1 ;
insert into #c3 (idNbr, idValue, idP) select 1 , 1 , 1 ;
insert into #c3 (idNbr, idValue, idP) select 1 , 1 , 1 ;
CREATE TABLE #c4 (idNbr int, idValue int, idP int);
insert into #c4 (idNbr, idValue, idP) select 1 , 1 , 2 ;
select rs.idNbr, sum(rs.id1Tot)idTot, sum(rs.id2Tot) idTot2 from (
select idNbr, idvalue id1Tot, 0 id2Tot from #c3 where idP=1
union
select idNbr, 0 idTot, idvalue id2Tot from #c4 where idP=1
)rs group by rs.idNbr
drop table #c3
drop table #c4