我有两条SQL语句:
select employeeid,sum(amount) from transport_baseinfo where departmentid='102' and statusid!=48 and statusid!=45 and (substring(convert(varchar(10),[stampdate],120),1,7)='2005-10')
group by employeeid
上面这个语句得到的结果:
employeeid 无列名
0001 45
0002 90和select infor_id,0 from hr_userinfo where infor_unitid = 102
and ((infor_status = '在职' and (LEFT(infor_ruzhishijian,7)<= '2005-10')) or (infor_id in (select infor_id from hr_userinfo 
where infor_unitid = 102 and ((infor_status = '在职' or (LEFT(infor_lizhishijian, 7) >= '2005-10')) 
and (LEFT(infor_ruzhishijian,7)<= '2005-10'))))) 
上面这个语句得到的结果:
infor_id 无列名
0001 0
0002 0
0003 0
我希望把上面的两个查询语句并在一起但是其第一项个字段相等的话只要其一;
结果即是:
无列名 无列名
0001 45
0002 90
0003 0要如何实现?请教各位,帮帮忙!

解决方案 »

  1.   

    select id,max(amount) from
    (
    select employeeid id,sum(amount) amount from transport_baseinfo where departmentid='102' and statusid!=48 and statusid!=45 and (substring(convert(varchar(10),[stampdate],120),1,7)='2005-10')
    group by employeeid

    union

    select infor_id id,0 amount from hr_userinfo where infor_unitid = 102
    and ((infor_status = '在职' and (LEFT(infor_ruzhishijian,7)<= '2005-10')) or (infor_id in (select infor_id from hr_userinfo 
    where infor_unitid = 102 and ((infor_status = '在职' or (LEFT(infor_lizhishijian, 7) >= '2005-10')) 
    and (LEFT(infor_ruzhishijian,7)<= '2005-10'))))) 
    )
    group by id
    这样行不?
      

  2.   

    select id,max(amount) from
    (
    (select employeeid id,sum(amount) amount from transport_baseinfo where departmentid='102' and statusid!=48 and statusid!=45 and (substring(convert(varchar(10),[stampdate],120),1,7)='2005-10')
    group by employeeid)

    union

    (select infor_id id,0 amount from hr_userinfo where infor_unitid = 102
    and ((infor_status = '在职' and (LEFT(infor_ruzhishijian,7)<= '2005-10')) or (infor_id in (select infor_id from hr_userinfo 
    where infor_unitid = 102 and ((infor_status = '在职' or (LEFT(infor_lizhishijian, 7) >= '2005-10')) 
    and (LEFT(infor_ruzhishijian,7)<= '2005-10'))))) )
    )
    group by id
    刚才作union操作的两个表没有加括号,现在应该没有问题了吧
    出了错误你吧错误贴给我看