Author |
Topic |
bny82
Starting Member
3 Posts |
Posted - 2012-05-14 : 00:42:40
|
Guys,help me out,for this query...I have this tableNoTrans-----ID------------Datetimelog-------------------Type1-----------001-----------01/01/2012-08:00:00-----------IN2-----------001-----------01/01/2012-09:00:00-----------IN3-----------001-----------01/01/2012-17:00:00-----------OUT4-----------001-----------02/01/2012-08:00:00-----------IN5-----------001-----------02/01/2012-17:00:00-----------OUT6-----------001-----------02/01/2012-19:00:00-----------OUT7-----------001-----------02/01/2012-23:00:00-----------IN8-----------001-----------03/01/2012-04:00:00-----------OUT9-----------001-----------03/01/2012-20.00:00-----------IN10----------001-----------04/01/2012-15:00:00-----------IN11----------001-----------05/01/2012-20:00:00-----------OUTi-want-to-become-like-this-on-result.ID------------DATE-IN-----------------------DATE-OUT---------------------------------------VARIANT001-----------01/01/2012-08:00:00-----------01/01/2012-17:00:00---------------------------------8001-----------02/01/2012-08:00:00-----------02/01/2012-19:00:00-(The-Longest-Periode)-----------10001-----------02/01/2012-23:00:00-----------03/01/2012-04.00.00---------------------------------4 001-----------03/01/2012-20:00:00-----------NULL------------------------------------------------0001-----------04/01/2012-15:00:00-----------NULL------------------------------------------------0001-----------NULL--------------------------05/01/2012-20:00:00---------------------------------0Any kind of help would be appreciate...Thanks & Regards,bny17 |
|
bny82
Starting Member
3 Posts |
Posted - 2012-05-14 : 01:22:10
|
Please...some one help me out...anyone....Thanks & Regards,bny17 |
|
|
namman
Constraint Violating Yak Guru
285 Posts |
Posted - 2012-05-14 : 22:09:58
|
Explain buz rule how to get the expected output, please. |
|
|
vinu.vijayan
Posting Yak Master
227 Posts |
Posted - 2012-05-15 : 02:56:26
|
I wasn't able to work out a business logic according to your requirement. If you can tell us, according to what Business Logic your data is transformed into your Expected Result Set, then perhaps we could try it.But till then if it is a one-time requirement(may be for generating a one-time report) then perhaps you can use the following workaround:--Creating TableCreate Table Ex(NoTrans int, ID varchar(5), Datetimelog DateTime, Type varchar(5) )--Inserting Sample Data Insert Into ExSelect 1, 001, '01/01/2012 08:00:00', 'IN'Union ALLSelect 2, 001, '01/01/2012 09:00:00', 'IN'Union ALLSelect 3, 001, '01/01/2012 17:00:00', 'OUT'Union ALLSelect 4, 001, '02/01/2012 08:00:00', 'IN'Union ALLSelect 5, 001, '02/01/2012 17:00:00', 'OUT'Union ALLSelect 6, 001, '02/01/2012 19:00:00', 'OUT'Union ALLSelect 7, 001, '02/01/2012 23:00:00', 'IN'Union ALLSelect 8, 001, '03/01/2012 04:00:00', 'OUT'Union ALLSelect 9, 001, '03/01/2012 20:00:00', 'IN'Union ALLSelect 10, 001, '04/01/2012 15:00:00', 'IN'Union ALLSelect 11, 001, '05/01/2012 20:00:00', 'OUT'--Query For Your Requirement;With CTEAs(Select c.ID, c.DateIN ,d.DateOUT, ROW_NUMBER() Over (Order By (Select NULL)) As rownum From (Select a.* From (Select ID, (Case When TYPE = 'IN' Then Datetimelog Else '' End) As DateIN From Ex) As a ) As cFull JOIN (Select b.* From (Select ID, (Case When TYPE = 'OUT' Then Datetimelog Else '' End) As DateOUT From Ex) As b ) As dON c.ID = d.ID)Select ID, (Case When DateIN = '19000101 00:00:00.000' Then NULL Else DateIN End) As DateIN, (Case When DateOUT = '19000101 00:00:00.000' Then NULL Else DateOUT End) As DateOUTFrom CTEWhere rownum IN (3,39,74,89,100,33)Order By DateIN OR maybe some of the Pros here might come up with something. N 28° 33' 11.93148"E 77° 14' 33.66384" |
|
|
vinu.vijayan
Posting Yak Master
227 Posts |
Posted - 2012-05-15 : 05:17:40
|
Sorry, I forgot about Variant in the last attempt. Here is the edited Version:--Creating TableCreate Table Ex(NoTrans int, ID varchar(5), Datetimelog DateTime, Type varchar(5) )--Inserting Sample Data Insert Into ExSelect 1, 001, '01/01/2012 08:00:00', 'IN'Union ALLSelect 2, 001, '01/01/2012 09:00:00', 'IN'Union ALLSelect 3, 001, '01/01/2012 17:00:00', 'OUT'Union ALLSelect 4, 001, '02/01/2012 08:00:00', 'IN'Union ALLSelect 5, 001, '02/01/2012 17:00:00', 'OUT'Union ALLSelect 6, 001, '02/01/2012 19:00:00', 'OUT'Union ALLSelect 7, 001, '02/01/2012 23:00:00', 'IN'Union ALLSelect 8, 001, '03/01/2012 04:00:00', 'OUT'Union ALLSelect 9, 001, '03/01/2012 20:00:00', 'IN'Union ALLSelect 10, 001, '04/01/2012 15:00:00', 'IN'Union ALLSelect 11, 001, '05/01/2012 20:00:00', 'OUT'--Query For Your Requirement;With CTEAs(Select c.ID, c.DateIN ,d.DateOUT, ROW_NUMBER() Over (Order By (Select NULL)) As rownum From (Select a.* From (Select ID, (Case When TYPE = 'IN' Then Datetimelog Else '' End) As DateIN From Ex) As a ) As cFull JOIN (Select b.* From (Select ID, (Case When TYPE = 'OUT' Then Datetimelog Else '' End) As DateOUT From Ex) As b ) As dON c.ID = d.ID)Select ID, (Case When DateIN = '19000101 00:00:00.000' Then NULL Else DateIN End) As DateIN, (Case When DateOUT = '19000101 00:00:00.000' Then NULL Else DateOUT End) As DateOUT, (Case When DateDiff(MM, Cast(DateIN AS Date), Cast(DateOUT As Date)) = 0 AND DateIN <> '19000101 00:00:00.000' Then DateDiff(SS, Convert(Time, DateIN), Convert(Time, DateOUT))/3600 When DateDiff(MM, Cast(DateIN AS Date), Cast(DateOUT As Date)) > 0 AND DateIN <> '19000101 00:00:00.000' Then Convert(int, DateAdd(SS, DateDiff(SS,Convert(Time, DateIN), CONVERT(Time, '23:59:59')), DateDiff(SS, CONVERT(Time, '00:00:00'),Convert(Time, DateOUT)) ))/3600 Else DatePart(HH, CONVERT(Time, '00:00:00')) End) As VariantFrom CTEWhere rownum IN (3,39,74,89,100,33)Order By DateIN N 28° 33' 11.93148"E 77° 14' 33.66384" |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-05-16 : 00:16:31
|
[code]set dateformat dmyCREATE TABLE #Temp(NoTrans int,ID varchar(3),Datetimelog datetime,[Type] varchar(3))GOINSERT #TempVALUES(1,'001','01/01/2012 08:00:00','IN'),(2,'001','01/01/2012 09:00:00','IN'),(3,'001','01/01/2012 17:00:00','OUT'),(4,'001','02/01/2012 08:00:00','IN'),(5,'001','02/01/2012 17:00:00','OUT'),(6,'001','02/01/2012 19:00:00','OUT'),(7,'001','02/01/2012 23:00:00','IN'),(8,'001','03/01/2012 04:00:00','OUT'),(9,'001','03/01/2012 20:00:00','IN'),(10,'001','04/01/2012 15:00:00','IN'),(11,'001','05/01/2012 20:00:00','OUT');With CTEAS(SELECT t.*FROM #Temp tOUTER APPLY (SELECT TOP 1 [Type] FROM #Temp WHERE Datetimelog< t.Datetimelog AND ID = t.ID AND DAY(DATEADD(hh,-8,Datetimelog)) = DAY(DATEADD(hh,-8,t.Datetimelog)) ORDER BY Datetimelog DESC )t1 WHERE (t1.[Type] IS NULL OR t1.[Type] <> 'IN') AND t.[Type] = 'IN' UNION ALL SELECT t.*FROM #Temp tOUTER APPLY (SELECT TOP 1 [Type] FROM #Temp WHERE Datetimelog> t.Datetimelog AND ID = t.ID AND DAY(DATEADD(hh,-8,Datetimelog)) = DAY(DATEADD(hh,-8,t.Datetimelog)) ORDER BY Datetimelog ASC )t1 WHERE (t1.[Type] IS NULL OR t1.[Type] <> 'OUT') AND t.[Type] = 'OUT' ) SELECT c1.ID,c1.Datetimelog,c2.Datetimelog,COALESCE(DATEDIFF(HH,c1.Datetimelog,c2.Datetimelog)-1,0) AS Variant FROM CTE c1 OUTER APPLY (SELECT TOP 1 Datetimelog FROM CTE WHERE [TYPE]='OUT' AND [ID] = c1.ID AND Datetimelog > c1.Datetimelog AND DAY(DATEADD(hh,-8,Datetimelog)) = DAY(DATEADD(hh,-8,c1.Datetimelog)) ORDER BY Datetimelog)c2 WHERE c1.[Type] = 'IN' UNION ALL SELECT c1.ID,null,c1.Datetimelog,0 AS Variant FROM CTE c1 OUTER APPLY (SELECT TOP 1 Datetimelog FROM CTE WHERE [TYPE]='IN' AND [ID] = c1.ID AND Datetimelog < c1.Datetimelog AND DAY(DATEADD(hh,-8,Datetimelog)) = DAY(DATEADD(hh,-8,c1.Datetimelog)) ORDER BY Datetimelog)c2 WHERE c1.[Type] = 'OUT' AND c2.Datetimelog IS NULL DROP TABLE #temp output--------------------------------- ID Datetimelog Datetimelog Variant001 2012-01-01 08:00:00.000 2012-01-01 17:00:00.000 8001 2012-01-02 08:00:00.000 2012-01-02 19:00:00.000 10001 2012-01-02 23:00:00.000 2012-01-03 04:00:00.000 4001 2012-01-03 20:00:00.000 NULL 0001 2012-01-04 15:00:00.000 NULL 0001 NULL 2012-01-05 20:00:00.000 0[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
|
|
bny82
Starting Member
3 Posts |
Posted - 2012-05-23 : 23:12:45
|
thanks in many visakh16, you help me on this solution, actually i have the solution also, now i will choose regard the execution time, anyway thanks for vinu.vijayan but unfortunately your solution looks have a hardcode, which is i cant select one by one...anyway thanks for all your solutions guys....if you want to know what my solution...this as ur reff. also.THIS IS MY STRUCTURE TABLECREATE TABLE [dbo].[checkinout]( [id] [int] NULL, [userid] [bigint] NULL, [checktime] [datetime] NULL, [checktype] [int] NULL, [verifycode] [int] NULL, [sn] [bigint] NULL) ON [PRIMARY]AND THIS IS MY STORE PROCEDURE.GO/****** Object: StoredProcedure [dbo].[SP_SYNCLOGIX] Script Date: 05/24/2012 09:58:28 ******/SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOALTER PROC [dbo].[SP_SYNCLOGIX](@STARTDATE AS DATETIME, @ENDDATE AS DATETIME)ASBEGIN--SET @STARTDATE = '05/01/2012'--SET @ENDDATE = '05/30/2012'IF EXISTS(SELECT *FROM tempdb.dbo.sysobjectsWHERE ID = OBJECT_ID(N'TempDB..#CHECKIN'))BEGINDROP TABLE #CHECKINENDselect * into #CHECKINfrom checkinoutWHERE CHECKTYPE = '0'IF EXISTS(SELECT *FROM tempdb.dbo.sysobjectsWHERE ID = OBJECT_ID(N'TempDB..#CHECKOUT'))BEGINDROP TABLE #CHECKOUTENDselect * into #CHECKOUTfrom checkinoutWHERE CHECKTYPE = '1'SELECT DISTINCT * FROM (SELECT A.ID,A.USERID,A.CHECKTIME AS CHECKIN , B.CHECKTIME AS CHECKOUT,A.VERIFYCODE,A.SN,DATEDIFF(HOUR, A.CHECKTIME, B.CHECKTIME)AS VARIANCE FROM #CHECKIN ALEFT OUTER JOIN #CHECKOUT BON A.UserID = B.UserID AND ((DAY(A.CHECKTIME) = DAY(B.CHECKTIME) AND MONTH(A.CHECKTIME) = MONTH(B.CHECKTIME)AND YEAR(A.CHECKTIME) = YEAR(B.CHECKTIME))OR(DAY(A.CHECKTIME) = DAY(B.CHECKTIME) - 1 AND MONTH(A.CHECKTIME) = MONTH(B.CHECKTIME)AND YEAR(A.CHECKTIME) = YEAR(B.CHECKTIME)))AND A.ID < B.ID)AWHERE CHECKIN < CHECKOUT AND ((DATEDIFF(HOUR, CHECKIN, CHECKOUT))<12)AND CHECKIN BETWEEN @STARTDATE AND @ENDDATEENDThanks & Regards,bny17 |
|
|
|
|
|