select qm.cInvcode,qm.cFree,qm.dcr+qc.IQuantity
from
(
select dc.cInvcode,dc.调出-dr.调入 as dcr,dc.cFree1
from
(
select cInvcode,SUM(IQuantity) as 调出,cFree1 from RdRecords where [id] in
(select [id] FROM RdRecord where crdcode='22' and dDate<'2009-12-30' and cwhcode='01') group by cInvcode,cFree1 ) dc
left join
(
select cInvcode,SUM(IQuantity) as 调入,cFree1 from RdRecords where [id] in
(select [id] FROM RdRecord where crdcode='12' and dDate<'2009-12-30' and cwhcode='01') group by cInvcode,cFree1 ) dr
on dc.cInvcode=dr.cInvcode
) qm
union all
(select * from gskdbqichu where qm.cInvcode = gskdbqichu.cInvcode and qm.cFree1 = gskdbqichu.cFree1) as qc
我是初学者 有点不明白 为什么在union all 后面的查询语句 加上() 就会报错 是语法错误 在关键字 'as' 附近有语法错误。如果不加的话 那么没办法调用union all后面表中的字段 列前缀 'gskdbqichu' 与查询中所用的表名或别名不匹配。麻烦大家帮下忙 看是不是我这个SQL语句本身写的就有问题~
from
(
select dc.cInvcode,dc.调出-dr.调入 as dcr,dc.cFree1
from
(
select cInvcode,SUM(IQuantity) as 调出,cFree1 from RdRecords where [id] in
(select [id] FROM RdRecord where crdcode='22' and dDate<'2009-12-30' and cwhcode='01') group by cInvcode,cFree1 ) dc
left join
(
select cInvcode,SUM(IQuantity) as 调入,cFree1 from RdRecords where [id] in
(select [id] FROM RdRecord where crdcode='12' and dDate<'2009-12-30' and cwhcode='01') group by cInvcode,cFree1 ) dr
on dc.cInvcode=dr.cInvcode
) qm
union all
(select * from gskdbqichu where qm.cInvcode = gskdbqichu.cInvcode and qm.cFree1 = gskdbqichu.cFree1) as qc
我是初学者 有点不明白 为什么在union all 后面的查询语句 加上() 就会报错 是语法错误 在关键字 'as' 附近有语法错误。如果不加的话 那么没办法调用union all后面表中的字段 列前缀 'gskdbqichu' 与查询中所用的表名或别名不匹配。麻烦大家帮下忙 看是不是我这个SQL语句本身写的就有问题~
解决方案 »
- 什么 delete语句,求达人解惑
- DISTINCT 与 ORDER BY NEWID()的问题
- 关于update问题,在线等
- 如何合并大部分字段内容相同,少许字段内容不同的记录?
- 如何动态添加科目名称?详见内容
- 怎么在Sql函数里面写一个递归?没思路!!!
- 如何向不是PK的字段插入不重复纪录
- 请教一下,我在SQL Server查询分析器里面执行一个存储过程5秒就可以,怎么用ADO在程序中执行有时候总是在等待,始终不返回,还没有错误返
- 关于复制,分布式数据库的若干问题,回答有效任何一个问题者,均给分啊,请高手指教!!!
- vs2010连接sql2008r2,服务器名为ip地址就无法连接,主机名就可以
- round问题
- 高分求,一个有难度的sql语句
from
(
select dc.cInvcode,dc.调出-dr.调入 as dcr,dc.cFree1
from
(
select cInvcode,SUM(IQuantity) as 调出,cFree1 from RdRecords where [id] in
(select [id] FROM RdRecord where crdcode='22' and dDate<'2009-12-30' and cwhcode='01') group by cInvcode,cFree1 ) dc
left join
(
select cInvcode,SUM(IQuantity) as 调入,cFree1 from RdRecords where [id] in
(select [id] FROM RdRecord where crdcode='12' and dDate<'2009-12-30' and cwhcode='01') group by cInvcode,cFree1 ) dr
on dc.cInvcode=dr.cInvcode
) qm
union all
select * from gskdbqichu where qm.cInvcode = gskdbqichu.cInvcode and qm.cFree1 = gskdbqichu.cFree1这里代表一个查询结构,上面代表一个查询,两个查询结构合并,
如果你用括号弄起来加个别名,肯定错了,
from
(
select cInvcode,SUM(IQuantity) as 调出,cFree1 from RdRecords where [id] in
(select [id] FROM RdRecord where crdcode='22' and dDate<'2009-12-30' and cwhcode='01') group by cInvcode,cFree1 ) dc
left join
(
select cInvcode,SUM(IQuantity) as 调入,cFree1 from RdRecords where [id] in
(select [id] FROM RdRecord where crdcode='12' and dDate<'2009-12-30' and cwhcode='01') group by cInvcode,cFree1 ) dr
on dc.cInvcode=dr.cInvcode
select qm.cInvcode,qm.cFree,qm.dcr+qc.IQuantity
from #T
union all
select * from gskdbqichu ,#T QM
where qm.cInvcode = gskdbqichu.cInvcode
and qm.cFree1 = gskdbqichu.cFree1列数要一样,4列,而且类型要一样,至少可以默认转换
from
(
select dc.cInvcode,dc.调出-dr.调入 as dcr,dc.cFree1
from
(
select cInvcode,SUM(IQuantity) as 调出,cFree1 from RdRecords where [id] in
(select [id] FROM RdRecord where crdcode='22' and dDate<'2009-12-30' and cwhcode='01') group by cInvcode,cFree1 ) dc
left join
(
select cInvcode,SUM(IQuantity) as 调入,cFree1 from RdRecords where [id] in
(select [id] FROM RdRecord where crdcode='12' and dDate<'2009-12-30' and cwhcode='01') group by cInvcode,cFree1 ) dr
on dc.cInvcode=dr.cInvcode
) qm , gskdbqichu Gwhere qm.cInvcode = G.cInvcode and qm.cFree1 = G.cFree1