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 結合で一部のフィールドが欠落していますか??

この出力が必要です:

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 に答える