Solved

Inner Join the same table to compare record against the previous record - again

Posted on 2004-09-02
8
526 Views
Last Modified: 2008-02-01
Well, I though I had this resolved in the following answer...

http://www.experts-exchange.com/Databases/Mysql/Q_21108778.html

But, when you do the Group By, it doesn't always work.  Continuing on the example info from before...

I am working with readings from a meter (like an electric meter), where the readings are on a totalizer, so to get the amount used between readings, you subtract the earlier reading from the later reading.

So, I am trying to do a sql statement that joins in the IMMEDIATE previous reading so you can subtract it.

The table (meter_data) with the essential fields and example data...

reading_id   read_date     read_time     reading
-----------    -----------      -----------      ---------
    1            2004-08-25    4:00:00         100
    2            2004-08-26    5:00:00         150
    3            2004-08-26    11:00:00       170
    4            2004-08-22    14:00:00        60

SELECT m1.read_date as end_date , m1.read_time as end_time , m2.read_date as start_date , m2.read_time as start_time , m1.reading - m2.reading as amount_used

FROM meter_data m1 inner join meter_data m2 on (m1.read_date > m2.read_date OR (m1.read_date = m2.read_date AND m1.read_time > m2.read_time))

GROUP BY  m2.read_date, m2.read_time

If you run this without the GROUP BY statement, you will get 6 records (3 previous records for reading_id of 3, 2 previous records for reading_id 2, 1 previous record for reading_id 1, and none for reading_id 4).

When you apply a GROUP BY, it doesn't have enough information to correctly group by, i.e. it doesn't know which of the 3 returned records for reading_id 3 is the right one (the immediate previously one).

I tried meddling around with timediff to find the smallest timediff, but kept going in circles.

I am trying to keep this in one sql statement, though if I have to, I will do two seperate SQL statments, ordered by read_date,read_time, and offset by 1 with a limit, and run through my math that way.

Any suggestions you have would be much appreciated.

Thanks.


0
Comment
Question by:keeper3
  • 4
  • 3
8 Comments
 
LVL 15

Expert Comment

by:JakobA
ID: 11969243
Your GROUP BY criteria are not consistent with the fields you ask for.

The fields (m1.read_date, m1.read_time) are likely to be different, eventhough (m2.read_date, m2.read_time) is the same, and when that happens those rows cannot be grouped.

So I would suggest another approach:

SELECT m1.read_date as end_date ,
            m1.read_time as end_time ,
            m2.read_date as start_date ,
            m2.read_time as start_time ,
            m1.reading - m2.reading as amount_used
FROM   meter_data m1 inner join meter_data m2
WHERE m1.reading_id = m2.reading_id -1              -- complex joiming conditions should be in the where clause

this will give you  each interval between readings. Note that the GROUP BY is unnessesarry (and this will run a LOT faster)

But it will not work if there are holes in the sequence of reading_id values, or if the -id ordering is not the same as the time ordering.  With an auto increment field and no DELETES that should be ok, but check it please.

regards JakobA
0
 

Author Comment

by:keeper3
ID: 11974245
Unfortunately, the reading_id values cannot be depended to be sequential because they are manually entered.  Someone might store up a couple readings on paper, and then enter them in an order that isn't necessarily chronological.

As for the GROUP BY not being consistent, agreed.  Each reading_id should end up with a single record, so you can group by the read_date/read_time, or the reading_id, but regardless of which, the statement is missing something to ensure correct grouping.
0
 
LVL 15

Expert Comment

by:JakobA
ID: 11976584
OK. there is another way to 'GROUP' them, but it may be quite slow, so test with a more than 4 entries table:

concatenate the 3 fields  read_date, read_time and reading from each row in each table. That gives us a compond field we can do a MAX on and then group with your original approach.

SELECT CONCAT( m1.read_date, m1.read_time, m1.reading) as m1conc,
            CONCAT( m2.read_date, m2.read_time, m2.reading) as m2conc,
FROM   meter_data m1 inner join meter_data m2
WHERE m1conc > m2conc
GROUP BY  m2conc

note that we cannot fetch the reading field as an independent value (it too differs within the groups), so you will have to extract it with programming from the resultset and then subtract to get the amount-used value.

regards JakobA

There is a possibility error here, I assume you are storing date and time in mysql's builtin DATE and TIME formats. If you are not the concatenation can go wrong as the time lose it initial zero with one digit hour values.

regards JakobA
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 15

Expert Comment

by:JakobA
ID: 11976627
Oops.  I forgot grouping the m1 values:

SELECT MAX( CONCAT( m1.read_date, m1.read_time, m1.reading) ) AS m1conc,
            CONCAT( m2.read_date, m2.read_time, m2.reading) AS m2conc,
FROM   meter_data m1 JOIN meter_data m2
GROUP BY  m2conc

0
 
LVL 15

Assisted Solution

by:JakobA
JakobA earned 250 total points
ID: 11976638
Darn. dumb again

SELECT MAX( CONCAT( m1.read_date, m1.read_time, m1.reading AS m1temp) ) AS m1conc,
            CONCAT( m2.read_date, m2.read_time, m2.reading) AS m2conc,
FROM   meter_data m1 JOIN meter_data m2
where   m1temp < m2conc
GROUP BY  m2conc
0
 

Author Comment

by:keeper3
ID: 11996238
Well, a couple things.  

1)  MySQL isn't liking, for me, putting in the alias m1temp inside a CONCAT
2)  Nor does it like having alias' in the WHERE clause.  Especially where the WHERE includes a MAX function, gives a invalid group function.

I have gotten a statement to work where I am working with a specific man_reading_id by doing the same type of join, but doing the following in my WHERE statement...

WHERE  m1.reading_id = 1 and m2.reading_id =  (select reading_id from meter_data m1 where (read_date = (select read_date from meter_data where reading_id = 1) and read_time < (select read_time from meter_data where reading_id = 1)) or (read_date < (select read_date from meter_data where reading_id = 1)) order by read_date desc,read_time desc limit 1)

This way I can be assured that my m2.reading_id is the immediate previous.  I don't see how to do this without specifying a specific reading_id.
0
 
LVL 2

Accepted Solution

by:
_kiew earned 250 total points
ID: 12030848
This should work:

SELECT m1.reading_id, m1.read_date, m1.read_time, MIN(m1.reading - m2.reading) AS amount_used
FROM meter_data m1, meter_data m2
ON (m1.read_date > m2.read_date OR (m1.read_date = m2.read_date AND m1.read_time > m2.read_time))
GROUP BY  m1.reading_id
0
 

Author Comment

by:keeper3
ID: 12404706
Well, I didn't end up getting it to work this way, but both of your answers did lead me in a different direction.

Thanks.
0

Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Foreword In the years since this article was written, numerous hacking attacks have targeted password-protected web sites.  The storage of client passwords has become a subject of much discussion, some of it useful and some of it misguided.  Of cou…
I use MySQL for many of my development projects in a Windows environment. To manage my databases (and perform queries) for years I used a tool called MySQL administrator.  This tool has since been replaced by MySQL Workbench. So I decided to m…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

778 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question