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 |
asif372
Posting Yak Master
100 Posts |
Posted - 2013-04-20 : 05:02:17
|
my data is Like thisID------CheckTime------------------Type9000----2013-01-09 09:00:00.000-----I9000----2013-01-09 09:01:00.000-----O9000----2013-01-09 09:10:00.000-----O9000----2013-01-09 10:00:00.000-----I9000----2013-01-09 10:01:00.000-----I9000----2013-01-09 12:00:00.000-----O9000----2013-01-09 13:00:00.000-----I9000----2013-01-09 13:01:00.000-----I9000----2013-01-09 13:01:00.000-----I9000----2013-01-09 15:00:00.000-----O9000----2013-01-09 15:01:00.000-----Oi have Query like thisSELECT T .ID,DATEADD(dd, 0, DATEDIFF(dd, 0, T .CheckTime)) Date,T .CheckTime AS TimeIn,(SELECT TOP 1 CASE WHEN T1.Type = 'O' THEN CHECKTIME ELSE NULL ENDFROM AttendLog T1WHERE T1.ID = T .ID AND T1.CHECKTIME > T .CHECKTIMEORDER BY CHECKTIME) AS TimeOutFROM AttendLog TWHERE Type = 'I'but getiing false data like thisID------Date----------TimeIN----------TimeOut9000----2013-01-09----09:00:00.000----09:01:00.0009000----2013-01-09----10:00:00.000----NULL9000----2013-01-09----10:01:00.000----12:00:00.0009000----2013-01-09----13:00:00.000----NULL9000----2013-01-09----13:01:00.000----15:00:00.0009000----2013-01-09----13:01:00.000----15:00:00.000My Required Data is like thisID------Date----------TimeIN----------TimeOut-----9000----2013-01-09----09:00:00.000----09:10:00.0009000----2013-01-09----10:01:00.000----12:00:00.0009000----2013-01-09----13:01:00.000----15:01:00.000Kindly Suggest Bettter Procedure thanks in Advance.... |
|
MuMu88
Aged Yak Warrior
549 Posts |
Posted - 2013-04-20 : 10:19:10
|
How about this:Select D.ID, D.Date, D.TimeIn, D.TimeOut from (SELECT T.ID,DATEADD(dd, 0, DATEDIFF(dd, 0, T.CheckTime)) Date,T.CheckTime AS TimeIn,(SELECT TOP 1 CASE WHEN T1.Type = 'O' THEN CHECKTIME ELSE NULL ENDFROM AttendLog T1WHERE T1.ID = T.ID AND T1.CHECKTIME > T .CHECKTIMEORDER BY CHECKTIME) AS TimeOutFROM AttendLog TWHERE Type = 'I') as D Where D.TimeOut Is NOT NULL |
|
|
immad
Posting Yak Master
230 Posts |
Posted - 2013-04-22 : 04:49:38
|
this particular query takes much time to execute is there any other way???immad uddin ahmed |
|
|
ahmeds08
Aged Yak Warrior
737 Posts |
Posted - 2013-04-22 : 05:14:18
|
are your tables properly indexed?mohammad.javeed.ahmed@gmail.com |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-04-22 : 05:18:04
|
[code]SELECT p.ID,p.[Date],p.TimeIN,DATEADD(dd,-DATEDIFF(dd,0,q.MaxDate),q.MaxDate) AS TimeOUTFROM(SELECT t.ID,DATEADD(dd,DATEDIFF(dd,0,t.CheckTime),0) AS [Date],DATEADD(dd,-DATEDIFF(dd,0,t.CheckTime),t.CheckTime) AS [TimeIN],MAX(CASE WHEN t1.Type='I' THEN CheckTime END) AS [NextTypeDate]FROM (SELECT *,ROW_NUMBER() OVER (PARTITION BY ID ORDER BY CheckTime ) AS Seq,ROW_NUMBER() OVER (PARTITION BY ID,Type ORDER BY CheckTime ) AS SSeq FROM Table) tINNER JOIN (SELECT *,ROW_NUMBER() OVER (PARTITION BY ID ORDER BY CheckTime ) AS Seq,ROW_NUMBER() OVER (PARTITION BY ID,Type ORDER BY CheckTime ) AS SSeq FROM Table)t1ON t1.ID = t.IDAND ((t1.Seq = t.Seq + 1 AND t1.Type='O')OR t1.SSeq = t.SSeq + 1)WHERE t.Type = 'I'GROUP BY t.ID,t.CheckTime)pOUTER APPLY (SELECT MAX(CheckTime) AS MaxDateFROM Table WHERE ID = p.IDAND Type = 'O'AND CheckTime < p.NextTypeDate)q[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
|
immad
Posting Yak Master
230 Posts |
Posted - 2013-04-23 : 00:47:40
|
YES Our table is properly indeximmad uddin ahmed |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-04-23 : 12:44:04
|
quote: Originally posted by immad YES Our table is properly indeximmad uddin ahmed
how did you determine that?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
|
|
asif372
Posting Yak Master
100 Posts |
Posted - 2013-05-21 : 08:28:20
|
thanks all of you for your Interest.can my requirement be fullfill while using Pivot Table if yes than what will be the query because all suggested Queries are taking very much timeThanks in Advance Regards |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-05-22 : 00:33:24
|
Pivot? you dont need pivot hereCan you see execution plan and let us know the costly steps?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
|
|
asif372
Posting Yak Master
100 Posts |
Posted - 2013-05-23 : 01:40:31
|
Thanks Visakh dear last pattern of Query is taking much time to execute including Groupby kindly help its urgentselect a.CID,a.BID,a.Eid,CONVERT (Datetime,Convert (varchar(10),a.itime,120)) AS Date,a.itime TimeIN,a.IAttendID,case when max(b.checktime)>a.itime and convert (VARCHAR(10),a.ITime,120) = convert (VARCHAR(10),MAX(b.checktime),120)then max(b.checktime) else null end as TimeOut,a.OAttendID,a.Remarks,a.Comments,a.InBetweenfrom(selecta.CID,a.BID,a.Eid,min(a.itime) as itime,IAttendID,a.otime,OAttendID,min(b.checktime) as itime2,a.Remarks,a.Comments,a.InBetweenfrom (select a.CID,a.BID,a.Eid,a.checktime as itime,a.AttendID IAttendID,min(b.checktime) as otime,b.AttendID OAttendID,a.Remarks,a.Comments,a.InBetweenfrom TABLE as aleft outer join TABLE as bon b.Eid=a.Eidand b.checktime>a.checktimeand b.Status='O'and a.bid = b.bidAND convert (VARCHAR(10),a.CHECKTIME,120) = convert (VARCHAR(10),b.CHECKTIME,120)where a.Status='I'group by a.CID,a.BID,a.Eid,a.AttendID,b.AttendID,a.checktime,a.Remarks,a.Comments,a.InBetween) as aleft outer join TABLE as bon b.Eid=a.Eidand b.checktime>a.otimeand b.Status='I'and a.bid = b.bidAND convert (VARCHAR(10),b.CHECKTIME,120) = convert (VARCHAR(10),a.ITIME,120)group by a.CID,a.BID,a.Eid,IAttendID,OAttendID,otime,a.Remarks,a.Comments,a.InBetween) as aleft outer join TABLE as bon a.Eid=b.Eidand b.Status='O'AND convert (VARCHAR(10),a.ITime,120) = convert (VARCHAR(10),b.CheckTime,120)AND isnull(a.itime2,'21991231') > b.checktimeand a.bid = b.bidand a.IAttendID = a.OAttendIDWhere b.checktime is not NULLgroup bya.CID,a.BID,a.Eid,a.itime,a.IAttendID,a.OAttendID,a.Remarks,a.Comments,a.InBetween |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-05-23 : 01:42:58
|
this doesnt help as its not even close to my earlier suggestion.You have to explain us what you're trying to achieve here by giving some sample data and expected output from it.------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
|
|
|
|
|
|
|