0

这是我的 Mysql 查询

SELECT COUNT(*) CallCnt,ANBCnt, t2.Duration,(ANB) Number,
  ((DATE_SUB('2012-12-10 05:22:30',
              INTERVAL (SELECT CONCAT(''',t2.Duration:0,''')) MINUTE_SECOND))) Mintime,
  ('2012-12-10 05:22:30') Maxtime,
  (1) TYPE,
  (t1.CallTypeId) 
FROM cdr_temp1_check t1
  INNER JOIN cdr_temp1_check_details t2 
  ON t1.CallTypeId=t2.CallTypeId
WHERE TimeStamp1 >= DATE_SUB('2012-12-10 05:22:30',
                              INTERVAL (SELECT CONCAT(''',t2.Duration:0,''')) MINUTE_SECOND) 
  AND TimeStamp1<='2012-12-10 05:22:30'
GROUP BY ANB 
  HAVING COUNT(*) > ANBCnt

返回的结果是

    CallCnt  ANBCnt  Duration      Number  Mintime              Maxtime                TYPE  CallTypeId
-------  ------  --------  ----------  -------------------  -------------------  ------  ----------
    267      40        25      2042011234  2012-12-10 05:20:30  2012-12-10 05:22:30       1           5
    134      20        15      9895697970  2012-12-10 05:20:30  2012-12-10 05:22:30       1           3

预期结果

    CallCnt  ANBCnt  Duration      Number  Mintime              Maxtime                TYPE  CallTypeId
-------  ------  --------  ----------  -------------------  -------------------  ------  ----------
    267      40        25      2042011234  2012-12-10 04:57:30  2012-12-10 05:22:30       1           5
    134      20        15      9895697970  2012-12-10 04:57:30  2012-12-10 05:22:30       1           3

显示问题的列是 Mintime 应该是Maxtime minus Duration

4

1 回答 1

0

我建议你尝试不同的方法。

鉴于 maxtime 只是您要输入查询的字符串。您可以使用 DATE_SUB 直接减去所需的间隔

((DATE_SUB('2012-12-10 05:22:30',
INTERVAL t2.Duration MINUTE))) Mintime

所以你的整个查询将是:

SELECT COUNT(*) CallCnt,ANBCnt,
t2.Duration,(ANB) Number,
((DATE_SUB('2012-12-10 05:22:30',
INTERVAL t2.Duration MINUTE))) Mintime,
('2012-12-10 05:22:30') Maxtime,(1) TYPE,
(t1.CallTypeId) FROM cdr_temp1_check t1
INNER JOIN cdr_temp1_check_details t2 ON t1.CallTypeId=t2.CallTypeId
WHERE TimeStamp1>=DATE_SUB('2012-12-10 05:22:30',
INTERVAL t2.Duration MINUTE) 
AND TimeStamp1<='2012-12-10 05:22:30'
GROUP BY ANB HAVING COUNT(*)>ANBCnt
于 2012-12-14T13:35:55.253 回答