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)
私を助けてください。お時間をいただきありがとうございます。:)