Retrieving rows that have 2 columns matching and 1 different

<p>Below is my table called 'datapoints'. I am trying to retrieve instances where there are different instances of 'sensorValue' for the same 'timeOfReading' and 'sensorNumber'.</p><p>For example:</p><pre><br /><code> sensorNumber sensorValue timeOfReading<br /><br /> 5 5 6<br /> 5 5 6<br /> 5 6 10 <----same time/sensor diff value!<br /> 5 7 10 <----same time/sensor diff value!<br /></code><br /></pre><p>Should output: sensorNumber:5, timeOfReading: 10 as a result.</p><p><a href="http://ift.tt/1JfjfiI" rel="nofollow"><img src="http://ift.tt/1JfjfiI" alt="datapoints table" /></a></p><p>I understand this is a duplicate question, in fact I have one of the links provided below for references - however none of the solutions are working as my query simply never ends.</p><p>Below is my SQL code:</p><pre><br /><code>SELECT table1.sensorNumber, table1.timeOfReading<br />FROM datapoints table1<br />WHERE (SELECT COUNT(*)<br /> FROM datapoints table2<br /> WHERE table1.sensorNumber = table2.sensorNumber<br /> AND table1.timeOfReading = table1.timeOfReading <br /> AND table1.sensorValue != table2.sensorValue) > 1<br /> AND table1.timeOfReading < 20;<br /></code><br /></pre><p>Notice I have placed a bound for timeOfReading as low as 20. I also tried setting a bound for both table1 and table 2 as well but the query just runs until timeout without displaying results no matter what I put...</p><p>The database contains about 700mb of data, so I do not think I can just run this on the entire DB in a reasonable amount of time, I am wondering if this is the culprit?</p><p>If so how could I properly limit my query to run a search efficiently? If not what am doing wrong that this is not working?</p><p><a href="http://ift.tt/1Vpgqxp">Select rows having 2 columns equal value</a></p><p>EDIT: Error Code: 2013. Lost connection to MySQL server during query 600.000 sec</p><p>When I try to run the query again I get this error unless I restart Error Code: 2006. MySQL server has gone away 0.000 sec</p><br /><h2>Click here view more answers <a href="%20http://stackoverflow.com/questions/34525665/retrieving-rows-that-have-2-columns-matching-and-1-different">http://ift.tt/1JfjeeR</a></h2>

Nhận xét

Bài đăng phổ biến từ blog này

Align div to bottom of the page in bootstrap

Align elements inside an tag vertically

Javascript is unable to stop page loading