declare @a table
(
  esta varchar(20),
  pp int,
  lh decimal(12,1)
)
declare @b table
(
  odept varchar(20),
  hours decimal(12,1)
)
insert @b
select '加工課',   8.0 union all
select '間隙組',   84.0insert @a
select '切割/拆箱',77, 808.5 union all
select '加工課', 14, 147.0 union all
select '去毛邊組', 51, 535.5 union all
select '拋光組', 56 ,588.0 union all
select '研磨組', 217, 2278.5 union all
select '培訓小組', 3 ,31.5 union all
select '間隙組', 27 ,283.5 union all
select '塗裝組', 49 ,514.5--查询
select a.*
       ,a.esta
       ,b.hours
from @a a
left join @b b on a.esta=b.odept--结果
/*esta        pp          lh             esta           hours  
-------------------- ----------- -------------- ---------------
切割/拆箱   77          808.5          切割/拆箱       NULL
加工課      14          147.0          加工課          8.0
去毛邊組    51          535.5          去毛邊組        NULL
拋光組      56          588.0          拋光組          NULL
研磨組      217         2278.5         研磨組          NULL
培訓小組    3           31.5           培訓小組        NULL
間隙組      27          283.5          間隙組         84.0
塗裝組      49          514.5          塗裝組         NULL(所影响的行数为 8 行)
*/

解决方案 »

  1.   

    写成一句就可以了:
    select a.esta,a.pp,a.lh,b.odept,b.hours
    from
    (select esta,count(esta) as pp,count(esta)*10.5 lh from ut_e001 group by esta) a
    full join (select odept,sum(hours) as hours from ut_e012 group by odept) b
    on a.esta=b.odept
      

  2.   

    select
        a.*,
        odept = isnull(b.odept,a.esta),
        b.hours
    from
        (select esta,count(esta) as pp,count(esta)*10.5 lh from ut_e001 group by esta) a
    left join
        (select odept,sum(hours) as hours from ut_e012 group by odept) b
    on
        a.esta = b.odept
      

  3.   

    select a.*,odept=a.esta,b.hours from 
    (select esta,count(esta) as pp,count(esta)*10.5 lh from ut_e001 group by esta) a
    left join 
    (select odept,sum(hours) as hours from ut_e012 group by odept ) b
     on a.esta=b.odept
      

  4.   

    to   vivianfdlpw() :為什麼用我的方法,不行呢?因為記錄是不確定的,會變動,所以我不能象你那樣寫。
    declare @t1 table(odept varchar(15),pp smallint,lrhour dec(10,2))
    insert @t1 select esta,count(esta) as pp,count(esta)*10.5 lh from ut_e001 group by estato  pbsql(风云), libin_ftsafe(子陌红尘) ,samfeng_2003(风云)
    你們三的語句顯示的結果是一樣的,都是:
    切割/拆箱 77 808.5 切割/拆箱 NULL
    加工課 14 147.0 加工課 NULL
    去毛邊組 51 535.5 去毛邊組 NULL
    拋光組 56 588.0 拋光組 NULL
    研磨組 217 2278.5 研磨組 NULL
    培訓小組 3 31.5 培訓小組 NULL
    間隙組 27 283.5 間隙組 NULL
    塗裝組 49 514.5 塗裝組 NULL錯在哪呢?
      

  5.   

    錯在哪呢?
    ---------------------------------------------------------------------------
    esta 和 odept 的值看似相同,实际上有不可见字符的差异,比如包含与不包含空格:select
        a.*,
        odept = isnull(b.odept,a.esta),
        b.hours
    from
        (select esta,count(esta) as pp,count(esta)*10.5 lh from ut_e001 group by esta) a
    left join
        (select odept,sum(hours) as hours from ut_e012 group by odept) b
    on
        rtrim(a.esta) = rtrim(b.odept)
      

  6.   


    用左等就行了呀!?  如 a.esta*=b.odept  只要在where 条件中用左等就行了呀!加“*”号
      

  7.   

    首先樓主先要對left join ,right join,full join的用法了解
    可以通過聯機幫助。
      

  8.   

    来慢了,估计宵夜吃的太饱了我没有每个回帖看完,觉得不就是left join,好像大家讨论到什么很难的东西,我没有看明白