临时表的问题 请教高手

来源:百度知道 编辑:UC知道 时间:2024/05/11 00:50:29
select (a1-b1),(a2-b2),(a3-b3) from
(
select (itemcode)a1,sum(iscommited)a2,sum(onorder)a3 from oitw a group by itemcode
union
select b1,b2,b3 from
(
select (itemcode)b1,(openqty)b2,(0)b3 from rdr1 where linestatus<>'R' group by itemcode,openqty
UNION
select (itemcode)b1,sum(plannedqty-issuedqty)b2,(0)b3 from wor1 where docentry not in (select docentry from owor where status='R') group by itemcode
UNION
select (itemcode)b1,(0)b2,sum(plannedqty-cmpltqty)b3 from owor where status='R' group by itemcode
UNION
select (itemcode)b1,(0)b2,sum(openqty)b3 from por1 where linestatus<>'c' group by itemcode
)b
)c
中间部分运行正常,执行后报错:
消息 207,级别 16,状态 1,第 1 行
列名 'b1' 无效。
消息 207,级别 16,状态 1,第 1 行
列名 'b2' 无效。
消息 207,级别 16,状态 1,第 1 行
列名 'b3' 无效。
这不需要建视图的, 亏好去掉还是同样的报错,甚至我加b.都错。
希望能有高手解决。我提高悬赏。

select (itemcode) AS a1,SUM(iscommited) AS a2, SUM(onorder) AS a3
from ...
union
select b1,b2,b3
from ...

如上语句产生的结果集,只包含a1, a2, a3列。不存在b1, b2, b3,你查一下UNION的用法就知道了。

b1 b2 b3 没定义 或者不在这个表中
建立个视图 再查
我看不懂

除了sum保留括号,其他的b1,b2,b3前面的括号去掉