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语句本身写的就有问题~

解决方案 »

  1.   

    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这里代表一个查询结构,上面代表一个查询,两个查询结构合并,
    如果你用括号弄起来加个别名,肯定错了,
      

  2.   

    哦 那请问下 现在我现在要得到的数据是  调出-调入+ gskdbqichu.IQuantity  请问下应该怎么写?
      

  3.   

    晕,看错了,原来后面还连接呀,用连接查询select dc.cInvcode,dc.调出-dr.调入 as dcr,dc.cFree1 INTO #T
        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列,而且类型要一样,至少可以默认转换
      

  4.   

    晕,原来是这个意思啊,那根本不是你的UNION ALL吧select qm.cInvcode,qm.cFree,qm.dcr+G.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  , gskdbqichu  Gwhere qm.cInvcode = G.cInvcode and qm.cFree1 = G.cFree1