| Author |
Topic |
|
jeusdi
Starting Member
27 Posts |
Posted - 2008-05-28 : 13:09:55
|
Hello forum. I address you in order to help me for create a little sentence.the problem is as:I have a table "STAMPING" that save data as:TIMESTAMP------------USER_ID-----TERMINAL_ID----CHIP_ID19/05/1982 15:30:31 1 1 1 19/05/1982 15:31:29 1 1 1 19/05/1982 15:31:55 1 1 1 19/05/1982 16:25:46 1 1 1 19/05/1982 16:26:23 1 1 1 19/05/1982 16:26:34 1 1 1 19/05/1982 22:23:56 1 1 1 19/05/1982 22:24:23 1 1 1 and I would need to filter them as-->TIMESTAMP------------USER_ID-----TERMINAL_ID----CHIP_ID19/05/1982 15:30:31 1 1 1 19/05/1982 15:31:29 1 1 1 19/05/1982 15:31:55 1 1 1 19/05/1982 16:25:46 1 1 1 19/05/1982 16:26:23 1 1 1 19/05/1982 16:26:34 1 1 1 19/05/1982 22:23:56 1 1 1 19/05/1982 22:24:23 1 1 120/05/1982 06:10:29 1 1 1 20/05/1982 06:11:51 1 1 1 TIMESTAMP------------USER_ID-----TERMINAL_ID----CHIP_ID19/05/1982 15:30:31 1 1 1 <<<---- less (difference) --> 00:56:0319/05/1982 16:26:34 1 1 1 <<<----19/05/1982 22:24:23 1 1 1 <<<---- less (difference) --> 07:45:00(+/-)20/05/1982 06:11:51 1 1 1 <<<---- So, I need to delete the "timestamps" the an user has done, which the difference between one stamping and the following one is less than 2 minutes for the inputs for entrances and I need to remove it when the "stampings" which the difference between an one stamping and the previous one is less than 2 minutes for exits.Can you help me please?I will appreciate a lot your help. |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-05-28 : 13:28:32
|
| How can you distinguish entrances and exits here? do you have any other field for it? |
 |
|
|
jeusdi
Starting Member
27 Posts |
Posted - 2008-05-29 : 02:37:08
|
| No, only with these fields should be possible distingish between entraces and exits. We might stablish that the first timestamp group (difference between them is less than 2 minutes) is an entrace, the second one exit, the thirth one entrace... |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-05-29 : 03:48:55
|
quote: Originally posted by jeusdi No, only with these fields should be possible distingish between entraces and exits. We might stablish that the first timestamp group (difference between them is less than 2 minutes) is an entrace, the second one exit, the thirth one entrace...
Ok. Even then how did you decide that 19/05/1982 16:25:46 should be deleted? What are rules for that? its diff is more than 2 minutes with previous record 19/05/1982 15:31:55 |
 |
|
|
jeusdi
Starting Member
27 Posts |
Posted - 2008-05-29 : 04:00:32
|
I think that a good beginning would be to get a table that a row with the previous timestamp and the following timestamp, eficiently:TIMESTAMP------------USER_ID-----TERMINAL_ID----CHIP_ID----PREVIOUS-----------------NEXT19/05/1982 15:30:31 1 1 1 NULL 19/05/1982 15:31:2919/05/1982 15:31:29 1 1 1 19/05/1982 15:30:31 19/05/1982 15:31:5519/05/1982 15:31:55 1 1 1 19/05/1982 15:31:29 19/05/1982 16:25:4619/05/1982 16:25:46 1 1 1 19/05/1982 15:31:55 19/05/1982 16:26:2319/05/1982 16:26:23 1 1 1 19/05/1982 16:25:46 19/05/1982 16:26:3419/05/1982 16:26:34 1 1 1 19/05/1982 16:26:23 19/05/1982 22:23:5619/05/1982 22:23:56 1 1 1 19/05/1982 16:26:34 19/05/1982 22:24:2319/05/1982 22:24:23 1 1 1 19/05/1982 22:23:56 NULL |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-05-29 : 04:10:57
|
For that you can use a CTE with ROW_NUMBER() function. E 12°55'05.25"N 56°04'39.16" |
 |
|
|
jeusdi
Starting Member
27 Posts |
Posted - 2008-05-29 : 05:42:19
|
quote: Originally posted by visakh16Ok. Even then how did you decide that 19/05/1982 16:25:46 should be deleted? What are rules for that? its diff is more than 2 minutes with previous record 19/05/1982 15:31:55
The timestamp are distinguished in entrances/exits timestamps groups:So in the sample data are 4 groups:In entrances group 1 are three timestamps (the diference between them are less than 2 minutes):19/05/1982 15:30:31 1 1 1 <<<< in entrance group I have to get the first one19/05/1982 15:31:29 1 1 1 19/05/1982 15:31:55 1 1 1 In exits group 1 are three ones:19/05/1982 16:25:46 1 1 1 19/05/1982 16:26:23 1 1 1 19/05/1982 16:26:34 1 1 1 <<<< in exit group I have to get the last one Entrances group 2:19/05/1982 22:23:56 1 1 1 <<<<19/05/1982 22:24:23 1 1 1 Exit group 2:20/05/1982 06:10:29 1 1 1 20/05/1982 06:11:51 1 1 1 <<<<TIMESTAMP------------USER_ID-----TERMINAL_ID----CHIP_ID19/05/1982 15:30:31 1 1 1 <<<---- less (difference) --> 00:56:0319/05/1982 16:26:34 1 1 1 <<<----19/05/1982 22:23:56 1 1 1 <<<---- It was wrong in the first post!!! less (difference) --> 07:45:00(+/-)20/05/1982 06:11:51 1 1 1 <<<---- |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-05-29 : 06:25:49
|
Are you using SQL Server 2000 or SQL Server 2005? E 12°55'05.25"N 56°04'39.16" |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-05-29 : 06:27:50
|
These records are two groups?1982-05-19 15:30:31.0001982-05-19 15:32:30.000 -- Less than two minutes from previous record1982-05-19 15:34:30.000 -- Exactly two minutes from previous record1982-05-19 15:36:31.000 -- More than two minutes from previous record, new group. E 12°55'05.25"N 56°04'39.16" |
 |
|
|
jeusdi
Starting Member
27 Posts |
Posted - 2008-05-29 : 07:04:15
|
quote: Originally posted by Peso Are you using SQL Server 2000 or SQL Server 2005? E 12°55'05.25"N 56°04'39.16"
Sql Server 2000 |
 |
|
|
jeusdi
Starting Member
27 Posts |
Posted - 2008-05-29 : 07:07:42
|
quote: Originally posted by Peso These records are two groups?1982-05-19 15:30:31.0001982-05-19 15:32:30.000 -- Less than two minutes from previous record1982-05-19 15:34:30.000 -- Exactly two minutes from previous record1982-05-19 15:36:31.000 -- More than two minutes from previous record, new group. E 12°55'05.25"N 56°04'39.16"
Yes, the first group would be-->1982-05-19 15:30:31.0001982-05-19 15:32:30.000 -- Less than two minutes from previous recordand the second one-->1982-05-19 15:34:30.000 -- Exactly two minutes from previous record1982-05-19 15:36:31.000 -- More than two minutes from previous record, new group.[/code] |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-05-29 : 07:20:33
|
So they are THREE groups?1982-05-19 15:30:31.000 -- Group 11982-05-19 15:32:30.000 -- Group 11982-05-19 15:34:30.000 -- Group 21982-05-19 15:36:31.000 -- Group 3 E 12°55'05.25"N 56°04'39.16" |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-05-29 : 07:32:05
|
[code]-- Prepare sample dataCREATE TABLE #Sample ( ts DATETIME, uid INT, tid INT, cid INT, grp INT )SET DATEFORMAT DMYINSERT #Sample ( ts, uid, tid, cid )SELECT '19/05/1982 15:30:31', 1, 1, 1 UNION ALLSELECT '19/05/1982 15:31:29', 1, 1, 1 UNION ALLSELECT '19/05/1982 15:31:55', 1, 1, 1 UNION ALLSELECT '19/05/1982 16:25:46', 1, 1, 1 UNION ALLSELECT '19/05/1982 16:26:23', 1, 1, 1 UNION ALLSELECT '19/05/1982 16:26:34', 1, 1, 1 UNION ALLSELECT '19/05/1982 22:23:56', 1, 1, 1 UNION ALLSELECT '19/05/1982 22:24:23', 1, 1, 1 UNION ALLSELECT '20/05/1982 06:10:29', 1, 1, 1 UNION ALLSELECT '20/05/1982 06:11:51', 1, 1, 1-- Initialize stagingCREATE CLUSTERED INDEX IX_Yak ON #Sample (uid, ts)UPDATE #SampleSET Grp = NULL-- Update sequencingDECLARE @Grp INT, @uid INT, @ts DATETIMESELECT TOP 1 @uid = uid, @Grp = 0, @ts = tsFROM #SampleORDER BY uid, tsUPDATE #SampleSET @Grp = Grp = CASE WHEN uid > @uid THEN 1 WHEN ts < DATEADD(MINUTE, 2, @ts) THEN @Grp ELSE @Grp + 1 END, @ts = ts, @uid = uid-- Show the expected resultsetSELECT CASE WHEN Grp % 2 = 0 THEN MIN(ts) ELSE MAX(ts) END AS TIMESTAMPFROM #SampleGROUP BY GrpORDER BY Grp-- Clean upDROP TABLE #Sample[/code] E 12°55'05.25"N 56°04'39.16" |
 |
|
|
|