Thursday 15 May 2014

mysql - What would be a good way to write this SQL query in a better way? -


  select the end of the MAX (timestamp), as the start of the MIN (timestamp), (MAX ( Odometer KM) - MIN (OdometerKM)) as the distance, (SELECT COUNT (*) from EventData, where speed is KPH = 0 and timestamp> =? And timestamp   

is quite confusing but I've created this query to find a summary of the set of rows. Minimum and maximum timestamps are simple, but the number of rows where speed is zero and zero is not, I've done an ugly hack. I'm sure there is a better way to do this?

Update : "?" The CodeIgniter is being replaced by an immature query builder. Sadly, it does not yet support nominal parameters.

You should include DeviceID, so it makes sense to try: As the end of

  SELECT DeviceID, MAX (timestamp), MIN as a start (timestamp), (MAX (Odometer KM) - MIN (Odometer KM)) distance, SUM (case speed) KPH when 0 to 1 sec 0 and stop), SUM (case speed KPH when 0 and 0 second 1 end) trip degree, (Madom (Odometer KM) - MIN (Odometer KM)) / device. Connect the fuel unit to the device on the console Device.deviceID from the Instant in the form of a fuel = EventData.deviceID WHERE device ID =? And Timestamp & gt; =? And Timestamp & lt; =? Group by device id    

No comments:

Post a Comment