Reputation: 11992
Using SQL Server 2000
Table1
ID Name, Date, TimeIn, TimeOut, DateIn, Dateout
AEAA00294 Alexander 13/10/2008 09:00:00 18:00:00 13/10/2008 13/10/2008
AEAA00294 Alexander 14/10/2008 16:00:00 02:00:00 14/10/2008 15/10/2008
AEAA00294 Alexander 16/10/2008 09:00:00 18:00:00 16/10/2008 16/10/2008
So on…,
Table2
ID Date
DATE, TIME, ID
20081013 103417 AEAA00294
20081013 151552 AEAA00294
20081013 170836 AEAA00294
20081013 170909 AEAA00294
20081013 171015 AEAA00294
20081014 163648 AEAA00294
20081014 030838 AEAA00294
20081015 144708 AEAA00294
20081015 151133 AEAA00294
20081016 095211 AEAA00294
So on…,
From the above two table I am taking Intime that is min (time) as Intime on the date and max (time) as Outtime on the same date from table2 where table1.personid = table2.personid.
In table1 DateIn and Dateout Date is same, it should take min (time) and Max (time) on the same date from the table2
Suppose table1 DateIn and Dateout Date is different, it should take min (time) and Max (time) from table2 compare with table1.dateIn and table2.Dateout
Expected Output
ID Date Intime Outtime
AEAA00294 20081013 103417 171015
AEAA00294 20081014 030838 151133
So on…,
My Query
SELECT DISTINCT DERIVEDTBL.PERSONID, DERIVEDTBL.CARDEVENTDATE, MIN(DERIVEDTBL.CARDEVENTTIME) AS INTIME, MAX(DERIVEDTBL.CARDEVENTTIME) AS OUTTIME, tmp_Cardevent1.Normal_Intime, tmp_Cardevent1.Normal_Outtime, tmp_Cardevent1.CardEventDate AS Expr1, tmp_Cardevent1.DateIn, tmp_Cardevent1.DateOut FROM (SELECT T_PERSON.PERSONID, T_CARDEVENT.CARDEVENTDATE, CONVERT(VARCHAR(10), SUBSTRING(T_CARDEVENT.CARDEVENTTIME, 1, 2) + ':' + SUBSTRING(T_CARDEVENT.CARDEVENTTIME, 3, 2) + ':' + SUBSTRING(T_CARDEVENT.CARDEVENTTIME, 5, 2), 8) AS CARDEVENTTIME
FROM T_CARDEVENT LEFT JOIN T_PERSON ON T_CARDEVENT.PERSONID = T_PERSON.PERSONID) DERIVEDTBL INNER JOIN tmp_Cardevent1 ON DERIVEDTBL.PERSONID = tmp_Cardevent1.PERSONID AND DERIVEDTBL.CARDEVENTDATE = tmp_Cardevent1.CardEventDate GROUP BY DERIVEDTBL.CARDEVENTDATE, DERIVEDTBL.PERSONID, tmp_Cardevent1.Normal_Intime, tmp_Cardevent1.Normal_Outtime, tmp_Cardevent1.CardEventDate, tmp_Cardevent1.DateIn, tmp_Cardevent1.DateOut
Table1 as tmp_cardevent1 and Table2 as Derivedtbl in my query.
From the above i want to compare derivedtbl date with tmp_cardevent1 dateIn and DateOut values.
Need Query Help.
How to make a query for this condition?
Upvotes: 0
Views: 147
Reputation: 166576
Try something like this
DECLARE @Table1 TABLE(
ID VARCHAR(50),
PNAME VARCHAR(50),
Date DATETIME,
TimeIN VARCHAR(10),
[TimeOut] VARCHAR(10),
DateIn DATETIME,
DateOut DATETIME
)
INSERT INTO @Table1 (ID,PNAME,Date,TimeIN,[TimeOut],DateIn,DateOut)
SELECT 'AEAA00294','Alexander','13 Oct 2008', '09:00:00', '18:00:00','13 Oct 2008','13 Oct 2008'
INSERT INTO @Table1 (ID,PNAME,Date,TimeIN,[TimeOut],DateIn,DateOut)
SELECT 'AEAA00294','Alexander','14 Oct 2008', '16:00:00', '02:00:00','14 Oct 2008','15 Oct 2008'
INSERT INTO @Table1 (ID,PNAME,Date,TimeIN,[TimeOut],DateIn,DateOut)
SELECT 'AEAA00294','Alexander','16 Oct 2008', '09:00:00', '18:00:00','16 Oct 2008','16 Oct 2008'
DECLARE @Table2 TABLE(
Date DATETIME,
[Time] VARCHAR(10),
ID VARCHAR(50)
)
INSERT INTO @Table2 (Date,[Time],ID) SELECT '13 Oct 2008', '10:34:17', 'AEAA00294'
INSERT INTO @Table2 (Date,[Time],ID) SELECT '13 Oct 2008' , '15:15:52' , 'AEAA00294'
INSERT INTO @Table2 (Date,[Time],ID) SELECT '13 Oct 2008' , '17:08:36' , 'AEAA00294'
INSERT INTO @Table2 (Date,[Time],ID) SELECT '13 Oct 2008' , '17:09:09' , 'AEAA00294'
INSERT INTO @Table2 (Date,[Time],ID) SELECT '13 Oct 2008' , '17:10:15' , 'AEAA00294'
INSERT INTO @Table2 (Date,[Time],ID) SELECT '14 Oct 2008' , '16:36:48' , 'AEAA00294'
INSERT INTO @Table2 (Date,[Time],ID) SELECT '14 Oct 2008' , '03:08:38' , 'AEAA00294'
INSERT INTO @Table2 (Date,[Time],ID) SELECT '15 Oct 2008' , '14:47:08' , 'AEAA00294'
INSERT INTO @Table2 (Date,[Time],ID) SELECT '15 Oct 2008' , '15:11:33' , 'AEAA00294'
INSERT INTO @Table2 (Date,[Time],ID) SELECT '16 Oct 2008' , '09:52:11' , 'AEAA00294'
SELECT t1.ID,
t1.PNAME,
t1.DateIn,
t1.DateOut,
MIN(CAST(t2In.[Time] AS DATETIME)),
MAX(CAST(t2Out.[Time] AS DATETIME))
FROM @Table1 t1 LEFT JOIN
@Table2 t2IN ON t1.ID = t2IN.ID
AND t1.DateIn = t2IN.Date LEFT JOIN
@Table2 t2Out ON t1.ID = t2Out.ID
AND t1.DateOut = t2Out.Date
GROUP BY t1.ID,
t1.PNAME,
t1.DateIn,
t1.DateOut
Upvotes: 1
Reputation: 6640
I think this will do it:
update table1 set timein=t2.timein, timeout=t2.timeout from table1 t1 join
(select ID, date, min(time) as timein, max(time) as timeout from table2 group by is, date) as t2 on t1.id=t2.id
Upvotes: 0