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 行)
*/
(
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 行)
*/
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
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
(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
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錯在哪呢?
---------------------------------------------------------------------------
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)
用左等就行了呀!? 如 a.esta*=b.odept 只要在where 条件中用左等就行了呀!加“*”号
可以通過聯機幫助。