我正在尝试找到产品的EPC,其产品的第一次和最后一次读取之间的时间最长。表中可以有多个条目。
例如,在下面显示的示例数据中,一个带有EPC 03.0000A89的产品......的日期为1992-11-05,条目的日期为2015-05-12。这显然是日期之间的最长持续时间,但我不知道如何编写查询来解决这个问题。
这些是DDL:
CREATE TABLE IF NOT EXISTS `Track_Record` (
`longitude` varchar(15) NOT NULL ,
`lattitude` varchar(15) NOT NULL ,
`datetime` DATETIME NOT NULL,
`EPC` varchar(200) NOT NULL ,
`ip` varchar(50) NOT NULL ,
PRIMARY KEY (ip, EPC, datetime)
);
INSERT INTO track_record (ip, longitude, lattitude, datetime, EPC)
VALUES
('000.111.222', '27.4667 S', '153.0333 E', '1992-11-05 18:56:46', '03.0000A89.00016F.000169DCD'), -- 0
('000.111.222', '27.4667 S', '153.0333 E', '2015-05-12 13:21:16', '03.0000A89.00016F.000169DCD'), -- 0
('555.666.777', '22.2783 N', '114.1747 E', '2012-07-19 12:22:16', '04.0000A89.00016F.000169DCD'), -- 0
('000.111.222', '27.4667 S', '153.0333 E', '2011-03-01 11:43:26', '03.0000A89.00016F.000169DCD'), -- 0
('555.666.777', '27.4667 S', '153.0333 E', '2014-09-02 18:53:14', '06.0000A89.00016F.000169DCD'),
('222.333.444', '59.3500 N', '18.0667 E', '2002-10-05 18:23:18', '04.0000A89.00016F.000169DCD'), -- 0
('333.444.555', '15.7833 S', '47.8667 W', '2015-02-22 19:53:16', '01.0000A89.00016F.000169DCD'),
('555.666.777', '22.2783 N', '114.1747 E', '2014-02-23 18:53:14', '08.0000A89.00016F.000169DCD'),
('222.333.444', '59.3500 N', '18.0667 E', '2009-08-12 18:23:18', '03.0000A89.00016F.000169DCD'), -- 0
('333.444.555', '15.7833 S', '47.8667 W', '2015-10-26 19:53:16', '07.0000A89.00016F.000169DCD'),
('000.111.222', '27.4667 S', '153.0333 E', '2002-11-21 11:43:26', '03.0000A89.00016F.000169DCD'), -- 0
('555.666.777', '22.2783 N', '114.1747 E', '2014-09-12 18:53:14', '06.0000A89.00016F.000169DCD'),
('222.333.444', '59.3500 N', '18.0667 E', '2010-09-13 18:23:18', '11.0000A89.00016F.000169DCD'), -- 0
('333.444.555', '15.7833 S', '47.8667 W', '2005-01-27 19:53:16', '01.0000A89.00016F.000169DCD'),
('555.666.777', '22.2783 N', '114.1747 E', '2006-09-22 18:53:14', '06.0000A89.00016F.000169DCD'),
('222.333.444', '59.3500 N', '18.0667 E', '2008-10-11 18:23:18', '04.0000A89.00016F.000169DCD'), -- 0
('888.444.555', '15.7833 S', '47.8667 W', '2013-03-19 19:53:16', '10.0000A89.00016F.000169DCD'),
('444.555.666', '51.5072 N', '0.1275 W', '2013-01-11 22:21:15', '04.0000A89.00016F.000169DCD'); -- 0
如果有人知道我如何获得此信息,我将非常感谢您提供的任何帮助。
感谢Corey
答案 0 :(得分:2)
那么EPC是您的产品代码?然后,您需要每个EPC的最小和最大时间,然后选择具有最大差异的那个。这就是全部。
select epc
from
(
select epc, max(datetime) - min(datetime) as diff
from track_record
group by epc
) products
order by diff desc limit 1;
答案 1 :(得分:1)
想要知道这似乎是一件奇怪的事情,但无论如何......
<table class="fixed">
<tr>
<th>FIXED</th>
</tr>
<tr>
<td>val1</td>
</tr>
<tr>
<td>val2</td>
</tr>
<tr>
<td>val3</td>
</tr>
<tr>
<td>val4</td>
</tr>
</table>
<div class="scrollable">
<table>
<tr>
<th>col1</th>
<th>col1</th>
<th>col1</th>
<th>col1</th>
<th>col1</th>
<th>col1</th>
</tr>
<tr>
<td>val1</td>
<td>val1</td>
<td>val1</td>
<td>val1</td>
<td>val1</td>
<td>val1</td>
</tr>
<tr>
<td>val2</td>
<td>val2</td>
<td>val2</td>
<td>val2</td>
<td>val2</td>
<td>val2</td>
</tr>
<tr>
<td>val3</td>
<td>val3</td>
<td>val3</td>
<td>val3</td>
<td>val3</td>
<td>val3</td>
</tr>
<tr>
<td>val4</td>
<td>val4</td>
<td>val4</td>
<td>val4</td>
<td>val4</td>
<td>val4</td>
</tr>
</table>
</div>
答案 2 :(得分:0)
使用BETWEEN
select * from tbl_name where DateTime BETWEEN '1992-11-05 00:00:0000' AND '2015-05-12 00:00:0000' ;
使用DATEDIFF获取两个日期之间的差异
SELECT DATEDIFF(day,'2014-06-05','2014-08-05') AS DiffDate
一旦获得所有产品之间的差异,请选择具有最大值的产品
答案 3 :(得分:0)
您可以尝试差异&#39; end_time - start_time&#39;。 选择* from where ip = ip end_time - start_time = max(select from end_time - start_time from ...)(s.thg like)
答案 4 :(得分:0)
尝试下面的查询,我并不认为这是最好的性能方法,因为mysql不是我的主要工作区域。但这很有效。
SELECT MAX(DATEDIFF(a.`datetime`,b.`datetime`) * 24*60*60) AS diff, a.EPC FROM track_record a LEFT JOIN track_record b ON a.EPC = b.EPC