0

我得到加入 2 个表,但使用 3 个表。我不需要重复值。但我尝试加入有重复。

peopleAll

pNo    pName
-------------
00001  Sang
00002  Janta
00003  Els
00004  Est
00005  Sam
00006  John
00007  Misan
00008  Wila
00009  light
00010  Smith
00011  Ritpo

fTime

cNo    cDate                     cIn                        cOut
-----------------------------------------------------------------------------------
00001  2012-10-22 00:00:00.000   2012-10-22 07:59:00.000    2012-10-22 20:34:00.000
00002  2012-10-22 00:00:00.000   2012-10-22 12:50:00.000    2012-10-22 19:50:00.000
00003  2012-10-22 00:00:00.000   2012-10-22 12:01:00.000    2012-10-22 20:30:00.000
00004  2012-10-22 00:00:00.000   2012-10-22 07:55:00.000    2012-10-22 20:30:00.000
00005  2012-10-22 00:00:00.000   2012-10-22 07:27:00.000    2012-10-22 20:31:00.000
00010  2012-10-22 00:00:00.000   2012-10-22 07:12:00.000    2012-10-22 20:22:00.000

Leave

lNo    lDate                     lStart                     lStop
-----------------------------------------------------------------------------------
00002  2012-10-22 00:00:00.000   2012-10-22 08:00:00.000    2012-10-22 12:00:00.000
00003  2012-10-22 00:00:00.000   2012-10-22 08:00:00.000    2012-10-22 12:00:00.000
00011  2012-10-22 00:00:00.000   NULL                       NULL

此 SQL 代码:

SELECT lr.lNo, lr.lDate, lr.lStart, lr.lStop 
FROM Leave lr 
Where lr.cStart = '2012-10-22'

UNION ALL

SELECT pa.pNo, ISNULL(tf.cDate, Convert(nvarchar(10),'2012-10-22',114)),tf.cIn, tf.cOut
FROM fTime tf FULL OUTER join peopleAll pa On tf.cNo = pa.pNo AND 
tf.cDate = Convert(nvarchar(10),'2012-10-22',114)

返回此输出:

lNo    lDate                     lStart                     lStop
-----------------------------------------------------------------------------------
00001  2012-10-22 00:00:00.000   2012-10-22 07:59:00.000    2012-10-22 20:34:00.000
00002  2012-10-22 00:00:00.000   2012-10-22 08:00:00.000    2012-10-22 12:00:00.000
00002  2012-10-22 00:00:00.000   2012-10-22 12:50:00.000    2012-10-22 19:50:00.000
00003  2012-10-22 00:00:00.000   2012-10-22 08:00:00.000    2012-10-22 12:00:00.000
00003  2012-10-22 00:00:00.000   2012-10-22 12:01:00.000    2012-10-22 20:30:00.000
00004  2012-10-22 00:00:00.000   2012-10-22 07:55:00.000    2012-10-22 20:30:00.000
00005  2012-10-22 00:00:00.000   2012-10-22 07:27:00.000    2012-10-22 20:31:00.000
00006  2012-10-22 00:00:00.000   NULL                       NULL
00007  2012-10-22 00:00:00.000   NULL                       NULL
00008  2012-10-22 00:00:00.000   NULL                       NULL
00009  2012-10-22 00:00:00.000   NULL                       NULL
00010  2012-10-22 00:00:00.000   2012-10-22 07:12:00.000    2012-10-22 20:22:00.000
00011  2012-10-22 00:00:00.000   NULL                       NULL

FULL OUTER JOIN然后我尝试更改LEFT OUTER JOIN并得到输出:

lNo    lDate                     lStart                     lStop
-----------------------------------------------------------------------------------
00002  2012-10-22 00:00:00.000   2012-10-22 08:00:00.000    2012-10-22 12:00:00.000
00002  2012-10-22 00:00:00.000   2012-10-22 12:50:00.000    2012-10-22 19:50:00.000
00003  2012-10-22 00:00:00.000   2012-10-22 08:00:00.000    2012-10-22 12:00:00.000
00003  2012-10-22 00:00:00.000   2012-10-22 12:01:00.000    2012-10-22 20:30:00.000
00004  2012-10-22 00:00:00.000   2012-10-22 07:55:00.000    2012-10-22 20:30:00.000
00007  2012-10-22 00:00:00.000   NULL                       NULL
00008  2012-10-22 00:00:00.000   NULL                       NULL
00010  2012-10-22 00:00:00.000   2012-10-22 07:12:00.000    2012-10-22 20:22:00.000
00011  2012-10-22 00:00:00.000   NULL                       NULL

LEFT OUTER join 错过了一些字段??

我需要这个输出:

lNo    lDate                     lStart                     lStop
-----------------------------------------------------------------------------------
00001  2012-10-22 00:00:00.000   2012-10-22 07:59:00.000    2012-10-22 20:34:00.000
00002  2012-10-22 00:00:00.000   2012-10-22 12:50:00.000    2012-10-22 19:50:00.000 
00003  2012-10-22 00:00:00.000   2012-10-22 12:01:00.000    2012-10-22 20:30:00.000
00004  2012-10-22 00:00:00.000   2012-10-22 07:55:00.000    2012-10-22 20:30:00.000
00005  2012-10-22 00:00:00.000   2012-10-22 07:27:00.000    2012-10-22 20:31:00.000
00006  2012-10-22 00:00:00.000   NULL                       NULL
00007  2012-10-22 00:00:00.000   NULL                       NULL
00008  2012-10-22 00:00:00.000   NULL                       NULL
00009  2012-10-22 00:00:00.000   NULL                       NULL
00010  2012-10-22 00:00:00.000   2012-10-22 07:12:00.000    2012-10-22 20:22:00.000
00011  2012-10-22 00:00:00.000   NULL                       NULL

(00002,00003 from fTime)

请帮我。谢谢你的时间。:)

4

2 回答 2

1
Select p.pNo
,Coalesce(l.cDate,t.lDate,'2012-10-22') as ldate
,Coalesce(l.lStart,t.cIn) as lstart
,Coalesce(l.lStop,t.cOut  ) as lstop
from peopleall p
left join leave l on l.lNo=p.Pno and l.ldate='2012-10-22'
left join fTime t on t.tNo=p.Pno and t.cDate='2012-10-22'
于 2012-11-04T18:04:19.110 回答
0

试试这个

Select p.pNo , 
Coalesce(l.lDate , ftemp.cDate , NULL) as lDate , 
Coalesce(l.lStart,ftemp.cIn , NULL)  as lStart ,
Coalesce(l.lStop , ftemp.cOut, NULL ) as lStop 
from peopleAll p  
left join Leave l  on (p.pNo = l.lNo ) 
left join 
  (select cNo, cDate ,cIn, cOut 
     from  fTime ft where not exists (select 1 from Leave l2 
                                      where ft.cNo = l2.lNo )) 
     ftemp
on (ftemp.cNo = p.pNo ) 

如果你得到一个错误,你能把输出发给我吗?

于 2012-11-04T18:07:32.573 回答