2

I want to find the time difference between two function names in my database. the database looks like this:

enter image description here

what I want to do is to find the time difference between two consecutive function names who have the same name. for example the output will be for "getPrice" at row number "2" and row number "3" and then time difference for "getPrice"at row "3" and row "5" and so on for all other times and all other function names. Please help me and thanks a lot!

I tried

SELECT a.lid, a.date, (b.date-a.date) as timeDifference 
FROM myTable a 
INNER JOIN myTable b ON b.lid = (a.lid+1) 
ORDER BY a.lid ASC;

The problem is, it gives time difference for any consecutive function names even if they are not identical!

@tombom

there is a table I use for testing and have different variable names than the example I provided earlier. the table looks like this:

enter image description here

and after applying your code (and of course change the variable names to match with this table) the output looks like this:

enter image description here

as you can see the "getTax" is subtracted from "getPrice" although they are different. how can I solve this problem?? Thanks a lot.

the schema I'm trying to build is:

 CREATE  TABLE `test` (
  `id` INT NOT NULL AUTO_INCREMENT ,
  `nserviceName` VARCHAR(45) NULL ,
  `functionName` VARCHAR(45) NULL ,
  `time` TIMESTAMP NULL ,
  `tps` INT NULL ,
  `clientID` INT NULL ,
  PRIMARY KEY (`id`) );

and the insert is :

INSERT INTO `test` (`id`, `nserviceName`, `functionName`, `time`, `tps`, `clientID`) VALUES ('1', 'X1', 'getPrice', '2013-05-23 00:36:08', '22', '0');
INSERT INTO `test` (`id`, `nserviceName`, `functionName`, `time`, `tps`, `clientID`) VALUES ('2', 'X2', 'getTax', '2013-05-23 00:38:00', '33', '0');
INSERT INTO `test` (`id`, `nserviceName`, `functionName`, `time`, `tps`, `clientID`) VALUES ('3', 'X1', 'getPrice', '2013-05-23 00:35:00', '12', '0');
INSERT INTO `test` (`id`, `nserviceName`, `functionName`, `time`, `tps`, `clientID`) VALUES ('4', 'X1', 'getPrice', '2013-05-23 00:35:00', '11', '0');
INSERT INTO `test` (`id`, `nserviceName`, `functionName`, `time`, `tps`, `clientID`) VALUES ('5', 'X2', 'getTax', '2013-05-23 00:35:00', '88', '0');
INSERT INTO `test` (`id`, `nserviceName`, `functionName`, `time`, `tps`, `clientID`) VALUES ('6', 'X1', 'getPrice', '2013-05-23 00:35:00', '33', '0');

thanks.

@tombom the operation I want to perform on the table is like the following image: enter image description here

where I start from the first record X1 getPrice which have no record before it. so no operation is required. then check number two getTax have no getPrice before it which are not identical so again no operation will be performed. then number 3 getPrice have getTax before it so it ignores it and check above getTax to find getPrice here it will do the time difference between getPrice(#3) and getPrice(#1). next getPrice at row 4 will check the rows above it, and it find the one directly above it is getPrice so time difference between getPrice*(#4) and getPrice(#3) will be found. then getTax at row 5 will check the rows above it until it finds a similar functionName (getTax) which is at row #2. then the time difference between getTax at row 5 and getTax at row 2 will be found.

thanks a lot..

4

1 回答 1

2

请试试这个:

SELECT lid, `date`, serviceName, functionName, responseTime, sid, timeDifference FROM (
SELECT
IF(@prevFname = functionName, SEC_TO_TIME(TIMESTAMPDIFF(SECOND, `date`, @prevDate)), 'functionName differs') AS timeDifference,
@prevFname := functionName AS a,
@prevDate := `date` AS b,
yt.*
FROM
yourTable yt
, (SELECT @prevFname:=NULL, @prevDate:=NULL) vars
ORDER BY functionName, `date`
) subquery_alias

我喜欢在这种情况下使用用户定义的变量,因为我在性能方面取得了惊人的体验,因为不需要自联接。

另请注意,我使用了该timestampdiff功能并sec_to_time润色了输出。Timestampdiff是减去不同日期(+次)的正确方法。唯一的缺点是,sec_to_time它只允许从 '00:00:00' 到 '23:59:59' 的范围。如果这会导致问题,请再次删除该功能。在此站点上阅读有关这两个功能的更多信息。

更新(没有必要那么复杂):

SELECT lid, `date`, serviceName, functionName, responseTime, sid, timeDifference FROM (
SELECT
SEC_TO_TIME(TIMESTAMPDIFF(SECOND, @prevDate, `date`)) AS timeDifference,
@prevDate := `date` AS b,
yt.*
FROM
yourTable yt
, (SELECT @prevDate:=NULL) vars
ORDER BY lid
) subquery_alias

更新 2:

这一个将时间差重置为00:00:00functionName 与前一个不同的时间。

SELECT * /*choose here only the columns you need*/ FROM (
SELECT
IF(@prevFunction = functionName, SEC_TO_TIME(TIMESTAMPDIFF(SECOND, @prevDate, `time`)), '00:00:00') AS timeDifference,
@prevFunction := functionName AS a,
@prevDate := `time` AS b,
yt.*
FROM
test yt
, (SELECT @prevDate:=NULL, @prevFunction:=NULL) vars
ORDER BY id
) subquery_alias

更新 3:

好吧,这是一个多么艰难的出生。只是一个小调整。

SELECT * /*choose here only the columns you need*/ FROM (
SELECT
IF(@prevFunction = functionName, SEC_TO_TIME(TIMESTAMPDIFF(SECOND, @prevDate, `time`)), '00:00:00') AS timeDifference,
@prevFunction := functionName AS a,
@prevDate := `time` AS b,
yt.*
FROM
test yt
, (SELECT @prevDate:=NULL, @prevFunction:=NULL) vars
ORDER BY functionName, id#, `time`
) subquery_alias
ORDER BY id

我在子查询中再次按函数名称和 id 排序(或时间,如果您愿意),进行所有计算,然后在外部查询中再次按 id 排序。而已。

于 2013-06-17T17:36:14.797 回答