实施MIN或获得最低记录

时间:2011-10-20 08:53:10

标签: mysql join

我想就如何解决此问题寻求帮助。我想添加一个MIN或某种查询,只显示下面SQL查询的TotalTime基数上的最小数字。

查询正在运行,但它显示重复数据,我只想获得最低的TotalTime。

提前致谢。

SELECT DISTINCT t1.name, 
  t1.Description, 
  t1.Issue, 
  t1.Dateres AS ATime, 
  t2.Dateres AS BAck, 
  TIMEDIFF(t2.Dateres,t1.Dateres) AS TotalTime, 
  t2.Acknowledge, t2.Resolution 
FROM t1
LEFT JOIN t2 ON t1.name = t2.name 
  AND t1.IPAddress = t2.IPAddress 
  AND t1.Description = t2.Description 
  AND t1.Issue = t2.Issue 
  AND t1.Severity = t2.Severity 
  AND t1.Timestamp = t2.Timestamp 
WHERE t1.Dateres IS NOT NULL
  AND t2.Dateres IS NOT NULL 
  AND t2.Acknowledge = 'user@home.net' 
  AND t2.Dateres >= '2011-10-18 00:00:00' 
  AND t2.Dateres <= '2011-10-23 23:59:59' 
GROUP BY ATime
ORDER by BAck ASC;

2 个答案:

答案 0 :(得分:1)

希望这会起作用:::

SELECT DISTINCT t1.name, 
t1.Description, 
t1.Issue, 
t1.Dateres AS ATime, 
t2.Dateres AS BAck, 
Min(TIMEDIFF(t2.Dateres,t1.Dateres)) AS TotalTime, 
t2.Acknowledge, t2.Resolution 
FROM t1 LEFT JOIN t2 ON t1.name = t2.name 
AND t1.IPAddress = t2.IPAddress 
AND t1.Description = t2.Description 
AND t1.Issue = t2.Issue 
AND t1.Severity = t2.Severity 
AND t1.Timestamp = t2.Timestamp 
WHERE t1.Dateres is NOT NULL AND t2.Dateres is NOT NULL 
AND t2.Acknowledge = 'user@home.net' 
AND t2.Dateres >= '2011-10-18 00:00:00' 
AND t2.Dateres <= '2011-10-23 23:59:59' 
GROUP BY ATime ORDER by BAck ASC;

答案 1 :(得分:1)

SELECT 
t1.name, 
t1.Description, 
t1.Issue, 
t1.Dateres AS ATime, 
t2.Dateres AS BAck, 
Min(TIMEDIFF(t2.Dateres,t1.Dateres)) AS TotalTime, 
t2.Acknowledge, t2.Resolution 
FROM t1 LEFT JOIN t2 ON t1.name = t2.name 
AND t1.IPAddress = t2.IPAddress 
AND t1.Description = t2.Description 
AND t1.Issue = t2.Issue 
AND t1.Severity = t2.Severity 
AND t1.Timestamp = t2.Timestamp 
WHERE t1.Dateres is NOT NULL AND t2.Dateres is NOT NULL 
AND t2.Acknowledge = 'user@home.net' 
AND t2.Dateres >= '2011-10-18 00:00:00' 
AND t2.Dateres <= '2011-10-23 23:59:59' 
GROUP BY t1.name ORDER by BAck ASC;