Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
belkin_99
Starting Member
27 Posts |
Posted - 2008-09-16 : 10:18:21
|
| I have two Tables:X with (Index char (25), Names varchar (120))300 Rows Included Names with Null Values.Y with (Index char (25),Location varchar (120),LogTime datetime)800 Rows Included Location with Null Values . where Each row from X.Index has Four rows from Y.LogTimeI created third Temp TableCreate Table #TempThird (Index char (25), Names varchar (120),Location varchar (120), IntervalTime1 datetime, IntervalTime2 datetime)Insert into #TempThird Select Index, Names , ???Q1 First Difference ???,???Q1 Second Difference???From XInner Join Y ON X.Index = Y.IndexMy Questions are:Q1: What I should put Or How to getThe First Difference which is : Y.LogTime (row3)- Y.LogTime (Row1)The Second Difference which is : Y.LogTime (row4)- Y.LogTime (Row1)Both on Y.Index = X.IndexQ2: I noticed when I run the insert I did not get Rows with the Null values Is this Right?Any Help will be appreciated. |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-09-16 : 10:28:53
|
May be this:-INSERT into #TempThirdSelect X.Index, X.Names , DATEDIFF(ss,MAX(CASE WHEN r.Seq=3 THEN r.LogTime ELSE NULL END),MAX(CASE WHEN r.Seq=1 THEN r.LogTime ELSE NULL END)) AS Diff1,DATEDIFF(ss,MAX(CASE WHEN r.Seq=4 THEN r.LogTime ELSE NULL END),MAX(CASE WHEN r.Seq=1 THEN r.LogTime ELSE NULL END)) AS Diff2From XInner Join (select (select count(*) from Y WHERE Index=t.Index AND LogTime<t.LogTime)+1 AS Seq,* FROM Y t)rON X.Index = r.IndexGROUP BY X.Index,X.Names |
 |
|
|
belkin_99
Starting Member
27 Posts |
Posted - 2008-09-18 : 21:25:13
|
| Sir,I really appreciate your help but, as I understood you create another table cable called r which have ( Index, Seq, LogTime)but how you fill this table with the information This step honestly I am confused about it.(select (select count(*) from Y WHERE Index=t.Index AND LogTime<t.LogTime)+1 AS Seq,* FROM Y t)rON X.Index = r.IndexWhat is the t?Please can you explain in details? |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-09-18 : 23:57:09
|
quote: Originally posted by belkin_99 Sir,I really appreciate your help but, as I understood you create another table cable called r which have ( Index, Seq, LogTime)but how you fill this table with the information This step honestly I am confused about it.(select (select count(*) from Y WHERE Index=t.Index AND LogTime<t.LogTime)+1 AS Seq,* FROM Y t)rON X.Index = r.IndexWhat is the t?Please can you explain in details?
t is alias given for table Y. as i'm using it twice inside r i want distinguish them both. thats the purpose of t.r is called a derived table. it will be populated on the fily on execution of the query. |
 |
|
|
belkin_99
Starting Member
27 Posts |
Posted - 2008-09-21 : 19:17:41
|
| Sir,I am sorry to say I am still stuck with it.Table1: XIn_Nr Type TimeLog------ ---- --------22 In 09/21/2008 12:00:20 PM22 out 09/21/2008 1:20:22 PM22 si 09/21/2008 12:10:13 PM44 In 09/22/2008 2:00:20 PM44 Out 09/22/2008 2:40:20 PMTable2: Y In_Nr Name------ ---- 22 TTT44 ZZZ55 VVVthe output table should be:Table output:In_Nr Name TimelogIN TimeLogSi TimeLogOUT Interval1 Interval2----- ---- ------ --------- --------- -------- --------22 TTT 12:00:20PM 12:10:13PM 1:20:22 PM out-in out-si44 zzz 2:00:20PM NULL 2:40:20PM out-in NULLI know it is seem easy but I am unable to implement thatThanks |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-09-22 : 03:47:09
|
quote: Originally posted by belkin_99 Sir,I am sorry to say I am still stuck with it.Table1: XIn_Nr Type TimeLog------ ---- --------22 In 09/21/2008 12:00:20 PM22 out 09/21/2008 1:20:22 PM22 si 09/21/2008 12:10:13 PM44 In 09/22/2008 2:00:20 PM44 Out 09/22/2008 2:40:20 PMTable2: Y In_Nr Name------ ---- 22 TTT44 ZZZ55 VVVthe output table should be:Table output:In_Nr Name TimelogIN TimeLogSi TimeLogOUT Interval1 Interval2----- ---- ------ --------- --------- -------- --------22 TTT 12:00:20PM 12:10:13PM 1:20:22 PM out-in out-si44 zzz 2:00:20PM NULL 2:40:20PM out-in NULLI know it is seem easy but I am unable to implement thatThanks
SELECT y.In_Nr,y.Name,MAX(CASE WHEN x.Type ='In' THEN x.TimeLog ELSE NULL END) AS TimelogIN,MAX(CASE WHEN x.Type ='Si' THEN x.TimeLog ELSE NULL END) AS TimelogSI,MAX(CASE WHEN x.Type ='Out' THEN x.TimeLog ELSE NULL END) AS TimelogOUT, CASE WHEN SUM(CASE WHEN x.Type ='In' OR x.Type ='Out'THEN 1 ELSE 0 END) >1 THEN 'out-in' ELSE NULL END AS Interval1, CASE WHEN SUM(CASE WHEN x.Type ='si' OR x.Type ='Out'THEN 1 ELSE 0 END) >1 THEN 'out-si' ELSE NULL END AS Interval2 FROM Y yINNER JOIN(SELECT (SELECT COUNT(*) FROM X WHERE In_Nr=t.In_Nr AND TimeLog <t.TimeLog) + 1 AS Seq,*FROM X t) tmpON tmp.In_Nr=y.In_Nr |
 |
|
|
belkin_99
Starting Member
27 Posts |
Posted - 2008-09-23 : 00:03:01
|
| Sir,I implemented your code , and I get the following1- Skip for the first field of the first row.2- duplicate all row data of first In_Nr (row), to the next In_Nr, which different in_NrThanks for your help |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-09-23 : 00:11:31
|
quote: Originally posted by belkin_99 Sir,I implemented your code , and I get the following1- Skip for the first field of the first row.2- duplicate all row data of first In_Nr (row), to the next In_Nr, which different in_NrThanks for your help
i missed a group bySELECT y.In_Nr,y.Name,MAX(CASE WHEN x.Type ='In' THEN x.TimeLog ELSE NULL END) AS TimelogIN,MAX(CASE WHEN x.Type ='Si' THEN x.TimeLog ELSE NULL END) AS TimelogSI,MAX(CASE WHEN x.Type ='Out' THEN x.TimeLog ELSE NULL END) AS TimelogOUT, CASE WHEN SUM(CASE WHEN x.Type ='In' OR x.Type ='Out'THEN 1 ELSE 0 END) >1 THEN 'out-in' ELSE NULL END AS Interval1, CASE WHEN SUM(CASE WHEN x.Type ='si' OR x.Type ='Out'THEN 1 ELSE 0 END) >1 THEN 'out-si' ELSE NULL END AS Interval2 FROM Y yINNER JOIN(SELECT (SELECT COUNT(*) FROM X WHERE In_Nr=t.In_Nr AND TimeLog <t.TimeLog) + 1 AS Seq,*FROM X t) tmpON tmp.In_Nr=y.In_NrGROUP BY y.In_Nr,y.Name |
 |
|
|
|
|
|
|
|