三个数据表aa(含字段bh ,mc,sl,rksj)、bb(含字段 bh ,mc,sl,rksj,cksj)cc(含字段 bh ,mc,sl,rksj,cksj),想实现以下功能
1、insert into cc(bh ,mc,sl,rksj)select * from aa  where rksj between '2009-02-01' and '2009-02-28';
2、insert into cc select * from bb a where  exists(select 1 from aa b where a.bh=b.bh and a.rksj=b.rksj and    b.rksj between '2009-02-01' and '2009-02-28') and a.cksj between '2009-02-01' and '2009-05-31'
3、对cc表中的相同编号进行整合,同时所有记录的sl项进行sum(sl)以上这几个功能能否用一条sql语句实现

解决方案 »

  1.   

    --1,2和并insert into cc 
    select * from bb a 
    where exists(select 1 from aa b where a.bh=b.bh and a.rksj=b.rksj and b.rksj between '2009-02-01' and '2009-02-28') and a.cksj between '2009-02-01' and '2009-05-31'
    union all
    select *,null from aa where rksj between '2009-02-01' and '2009-02-28'
      

  2.   

    insert into cc select bh,mc,sum(sl),rksj,cksj from
    (
    select *,null from aa where rksj between '2009-02-01' and '2009-02-28'
    union all 
    select * from bb a where exists(select 1 from aa b where a.bh=b.bh and a.rksj=b.rksj and b.rksj between '2009-02-01' and '2009-02-28') and a.cksj between '2009-02-01' and '2009-05-31')
    )
    group by bh,mc,rksj,cksj
      

  3.   

    insert into cc select bh,mc,sum(sl),rksj,cksj from
    (
    select *,null from aa where rksj between '2009-02-01' and '2009-02-28'
    union all 
    select * from bb a where exists(select 1 from aa b where a.bh=b.bh and a.rksj=b.rksj and b.rksj between '2009-02-01' and '2009-02-28') and a.cksj between '2009-02-01' and '2009-05-31')
    )
    group by bh,mc,rksj,cksj 楼上的高手们,怎么老提示“第 6 行: ')' 附近有语法错误”
      

  4.   

    insert into cc select bh,mc,sum(sl),rksj,cksj from
    (
    select *,null from aa where rksj between '2009-02-01' and '2009-02-28'
    union all 
    select * from bb a where exists(select 1 from aa b where a.bh=b.bh and a.rksj=b.rksj and b.rksj between '2009-02-01' and '2009-02-28') and a.cksj between '2009-02-01' and '2009-05-31')
    )AS T
    group by bh,mc,rksj,cksj加个别名
      

  5.   

    insert into cc select bh,mc,sum(sl),rksj,cksj from
    (
    select *,null from aa where rksj between '2009-02-01' and '2009-02-28'
    union all 
    select * from bb a where 
    exists(select 1 from aa b where a.bh=b.bh and a.rksj=b.rksj and b.rksj between '2009-02-01' and '2009-02-28') and a.cksj between '2009-02-01' and '2009-05-31'
    )AS T
    group by bh,mc,rksj,cksj楼主仔细检查一下呀,你后面多了一个括号了,
      

  6.   

    SQL77:
    insert into cc select bh,mc,sum(sl),rksj,cksj from
    (
    select *,null from aa where rksj between '2009-02-01' and '2009-02-28'
    union all 
    select * from bb a where 
    exists(select 1 from aa b where a.bh=b.bh and a.rksj=b.rksj and b.rksj between '2009-02-01' and '2009-02-28') and a.cksj between '2009-02-01' and '2009-05-31'
    )AS T
    group by bh,mc,rksj,cksj
    没有为第 5 列(属于 'T')指定列,去掉as T后提示“在关键字 'group' 附近有语法错误”
      

  7.   

    insert into cc select bh,mc,sum(sl),rksj,cksj from
    (
    select *,null AS cksj from aa where rksj between '2009-02-01' and '2009-02-28'
    union all 
    select * from bb a where 
    exists(select 1 from aa b where a.bh=b.bh and a.rksj=b.rksj and b.rksj between '2009-02-01' and '2009-02-28') and a.cksj between '2009-02-01' and '2009-05-31'
    )AS T
    group by bh,mc,rksj,cksj没测试,好多小问题看不出来,哎,功力不够啊我
      

  8.   

    SQL77:
    你谦虚了,在我们心目中就是高高人,因本人初学,斗胆再问一句,如果sum(sl)改为两个表查询的结果记录中sl字段相减又应如何改
      

  9.   

    你可以类似SELECT A,B FROM A
    UNION ALL
    SELECT -A,B FROM B
    在最后SUM的时候就变成减了呀,