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 6319457
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T15:48:36+00:00 2026-05-24T15:48:36+00:00

I have the following data in one table: Time, Type, Kilometers 12:00, 1, 0.1

  • 0

I have the following data in one table:

Time, Type, Kilometers
12:00, 1, 0.1
12:30, 2, 0.2
14:00, 1, 0.4
15:00, 2, 1.0
16:00, 1, 1.2
16:30, 2, 1.5
16:45, 1, 2.0

This data is sorted chronologically using the DateTime field. I would like to show these record ‘pairs’ as 1 row, like so:

StartTime, Type1Km, Type2Km
12:00, 0.1, 0.2
14:00, 0.4, 1.0
16:00, 1.2, 1.5
16:45, 2.0, NULL

There are a couple of caveats: If there is no Type1 to start, then show NULL in the resulting tables’ Type1Km field. Similarly, if there is no Type2 end, show NULL in records’ Type2Km field.

How could i do this?

  • 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-24T15:48:36+00:00Added an answer on May 24, 2026 at 3:48 pm

    Unfortunately, MySQL lacks a FULL OUTER JOIN, so you’ll have to UNION two sets together.

    This will get you the cases where Type1Km exists, whether or not Type2Km does.

    SELECT
        t1.`Time` as StartTime,
        t1.`Kilometers` as Type1Km,
        t2.`Kilometers` as Type2Km
    FROM `times` t1
    LEFT JOIN `times` t2 ON t2.`Type` = 2
                        AND t2.`Time` = (SELECT `Time` FROM `times`
                                         WHERE `Time` > t1.`Time`
                                         ORDER BY `Time` LIMIT 1)
    WHERE t1.`Type` = 1
    

    Now we need the cases where Type1Km does not exist.

    SELECT
        t2.`Time` as StartTime,
        NULL as Type1Km,
        t2.`Kilometers` as Type2Km
    FROM `times` t2
    LEFT JOIN `times` t1 ON t1.`Time` = (SELECT `Time` FROM `times`
                                         WHERE `Time` < t2.`Time`
                                         ORDER BY `Time` DESC LIMIT 1)
    WHERE t2.`Type` = 2
      AND (t1.`Type` = 2 OR t1.`Type` IS NULL)
    

    UNION those together, and you have the desired result:

    (
    SELECT
        t1.`Time` as StartTime,
        t1.`Kilometers` as Type1Km,
        t2.`Kilometers` as Type2Km
    FROM `times` t1
    LEFT JOIN `times` t2 ON t2.`Type` = 2
                        AND t2.`Time` = (SELECT `Time` FROM `times`
                                         WHERE `Time` > t1.`Time`
                                         ORDER BY `Time` LIMIT 1)
    WHERE t1.`Type` = 1
    
    ) UNION ALL (
    
    SELECT
        t2.`Time` as StartTime,
        NULL as Type1Km,
        t2.`Kilometers` as Type2Km
    FROM `times` t2
    LEFT JOIN `times` t1 ON t1.`Time` = (SELECT `Time` FROM `times`
                                         WHERE `Time` < t2.`Time`
                                         ORDER BY `Time` DESC LIMIT 1)
    WHERE t2.`Type` = 2
      AND (t1.`Type` = 2 OR t1.`Type` IS NULL)
    )
    
    ORDER BY `StartTime`
    

    Update

    In my previous query, I forgot to account for having a “type 2” record at the very beginning. Updated to account for that. Here’s the results I get:

    Data in times table:

    +----------+------+------------+
    | Time     | Type | Kilometers |
    +----------+------+------------+
    | 11:00:00 |    2 |        0.1 |
    | 12:00:00 |    1 |        0.1 |
    | 12:30:00 |    2 |        0.2 |
    | 14:00:00 |    1 |        0.4 |
    | 14:30:00 |    1 |        0.8 |
    | 15:00:00 |    2 |        1.0 |
    | 15:30:00 |    2 |        0.2 |
    | 16:00:00 |    1 |        1.2 |
    | 16:30:00 |    2 |        1.5 |
    | 16:45:00 |    1 |        2.0 |
    +----------+------+------------+
    

    Results of query:

    +-----------+---------+---------+
    | StartTime | Type1Km | Type2Km |
    +-----------+---------+---------+
    | 11:00:00  |    NULL |     0.1 |
    | 12:00:00  |     0.1 |     0.2 |
    | 14:00:00  |     0.4 |    NULL |
    | 14:30:00  |     0.8 |     1.0 |
    | 15:30:00  |    NULL |     0.2 |
    | 16:00:00  |     1.2 |     1.5 |
    | 16:45:00  |     2.0 |    NULL |
    +-----------+---------+---------+
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table which has field with data type as DATETIME. I persist
I have a table called Basic , start_time is one field with type :VARCHAR(5),
I currently have the following in one solution: Core Project (data access, biz logic,
I have following data to save in database using php my data is :
I have following data in my table. alt text http://img26.imageshack.us/img26/3746/productfield.png I want to extract
I have following data in excel table r1 r2 r3 r4 r5 v1 v2
From my experience I have learn that using an surrogate INT data type column
I have the following db table (People): +--------+--------+--------+--------+ | Name |Building| Nr | Time
I have a table of time-series data of which I need to find all
I have following data frames > head(elo) date elo 1 1921-12-18 1597 2 1922-05-14

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.