Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7194437
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T20:22:15+00:00 2026-05-28T20:22:15+00:00

i would like to add to an existent query 2 new columns. In the

  • 0

i would like to add to an existent query 2 new columns. In the first one, make a diff between the current row and the next one and in the next one to count the values between rows.

My current query is:

SELECT *, SUM(MINUTE(rest_time)) AS Times
  FROM routes as routes 
  LEFT JOIN sales
    ON (routes.departure_poi_code = sales.client_code AND
        routes.departure_date = sales.`date`)
 WHERE (routes.departure_poi_code LIKE 'C0%' OR routes.departure_poi_code LIKE 'MP%')
   AND routes.`car_no` =  'BH-07-EWR'
   AND routes.departure_date =  '2011-10-14'
 GROUP BY routes.departure_address, reports.routes.departure_poi_code,
          reports.routes.car_no, reports.routes.departure_date
 ORDER BY routes.car_no
 LIMIT 500000

and the result of my current query is something like:

Car_Numbr   Str_time    Cod_nr  KM
BH-07-EWR   08:59:00    C00425  7000
BH-07-EWR   10:29:00    C00149  8500
BH-07-EWR   14:27:00    C01075  9200
BH-07-EWR   15:07:00    C00305  9800
BH-07-EWR   16:08:00    C02572  9900

Of course it contains more columns but it is irrelevant to display all of them here.

The story is that a car make row2.km-row1.km from client 1 to client 2 an so on. Of course the cars km is the one that it has on board.
And I would like it to be:

Car_Number  Start_time  Code_nr KM  Total_km        Diff_km 
BH-07-EWR   08:59:00    C00425  7000    0   (km counter starting from 0)    0       (first row show have 0)
BH-07-EWR   10:29:00    C00149  8500    1500    (0+8500-7000)   1500    (8500-7000)
BH-07-EWR   14:27:00    C01075  9200    2200    (upper result+9200-8500)    700    (9200-8500)
BH-07-EWR   15:07:00    C00305  9800    2800    (upper result+9800-9200)    600 (9800-9200)
BH-07-EWR   16:08:00    C02572  9900    2900    (upper result+9900-9800)    100 (9900-9800)

How can I achieve this the easiest way?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T20:22:16+00:00Added an answer on May 28, 2026 at 8:22 pm

    Store the result into the temporary table (CREATE TEMPORARY TABLE…SELECT…), then use this query to calculate values –

    SELECT
      c.*,
      @diff:=IF(@prev_km IS NULL, 0, km - @prev_km) diff_km,
      @prev_diff_km:=IF(@prev_diff_km IS NULL, 0, @prev_diff_km + @diff) total,
      @prev_km:=km
    FROM
      car_temp c,
      (SELECT @prev_km := NULL, @prev_diff_km:=NULL) t
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to add each element in one vector with each element in
I would like add a new MessageBodyMember to my MessageContract which refers to a
I would like to make something like Add to favorites technology. In my app
I came across new play module and I would like add it in my
I have own project and i would like add for this class same as
I have a ListView and each item have a TextView. I would like add
I would like to add a GestureDetector to all views (view groups) of an
I would like to add a custom calculation method to a managed object (which
I would like to add roots to a VirtualTreeView http://www.delphi-gems.com/index.php/controls/virtual-treeview with a thread like
I would like to add a hash into an array using Ruby version 1.8.7:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.