0

Curerntly, we are taking and monitoring machine readings which are stored into MySql table into following format

data format in MySQL Table

I am trying to represent it in a single row in following format..

required format

I am using following approach to generate desired output..which i think is not a optimized approach

  1. get data using SQL query

Query:

select machine , min_reading, max_reading,current_reading,reading_round
from machine_reading
where machine = "XY" group by reading_round having max(timestamp)
  1. once i get the data , i process it on fronted using two foreah loops

code:

$machine = [];
foreach ($machine_readings as $reading) {
    if ($reading["reading_round"] == "round_1") {   
        $machine['machine'] =  $reading['machine']
        $machine['max-reading'] =  $reading['max-reading']
        $machine['min-reading'] =  $reading['min-reading']
        $machine['round_1'] =  $reading['current-reading']
        foreach ($machine_readings as $reading1) {
            if ($reading1['machine'] == $reading['machine']) {
                if ($reading1["reading_round"] == "round_2") {
                    $machine['round_2'] =  $reading1['current-reading']
                }elseif ($reading1["reading_round"] == "round_3") {
                    $machine['round_3'] =  $reading1['current-reading']
                }elseif ($reading1["reading_round"] == "round_4") {
                    $machine['round_4'] =  $reading1['current-reading']
                }elseif ($reading1["reading_round"] == "round_5") {
                    $machine['round_5'] =  $reading1['current-reading']
                }
            }
        }
    }
}

at the end i will get $machine object which will have data in single row...but this slows down when i am monitoring and generating data for multiple machine and we have new round every 30 minutes( so 48 rounds of reading in single day)

is it possible to optimize the above process ? i am open for changes on server-side data and or client side processing

Has QUIT--Anony-Mousse
  • 76,138
  • 12
  • 138
  • 194
MrPandav
  • 1,831
  • 1
  • 20
  • 24
  • aah !! **MySQL - Pivot Table**..advance SQL is my Solution....couldn't find that term..bang on !!....next task is to build case when using Eloquent query builder...Thanks – MrPandav Dec 29 '15 at 13:04
  • This Questions is Duplicate of Stack-overflow Question Answered in this post [MySQL - Pivot Table](http://stackoverflow.com/questions/7674786/mysql-pivot-table) External Link where this concept is explained: [MySQL - Pivot Table Concept Basics](http://www.artfulsoftware.com/infotree/qrytip.php?id=78) – MrPandav Dec 29 '15 at 13:13

0 Answers0