简体   繁体   中英

How to get multiple row items as different column in one sqlite command

I have a table looks like this:

Timestamp  direction  John  Mary  car   airplan

 4960        1        1   null   1     null
 5960        2        1     1   null    1  
 6960        1      null  null   1      1
 7960        1        1   null  null    1
 8960        2        1   null  null   null

the record means : john/mary/together go/back to xxx by car/airplan at timestamp and I wish to have one single sqlite query to get the result like this:

          John_t          Mary_t       car_t      airplan_t
          0                0           6960        7960
          8960            5960            0          0

which means the latest timestmap of (john, mary, car, airplan ) (go/back) to xxx.

I try the sql command , but failed...

select
(case when John=1 then max(Timestamp) else 0 end) as John_t, 
(case when Mary=1 then max(Timestamp) else 0 end) as Mary_t,
(case when car=1 then max(Timestamp) else 0 end) as car_t,
(case when airplan=1 then max(Time) else 0 end) as airplan_t
 from table where 4960 < Time < 8960 and group by direction

can anyone help?

Based on the comment by Andriy M , here is the answer:

select John_t, Mary_t, car_t, airplan_t from
    -- John --
    (select direction, (case when Timestamp=max_Timestamp then Timestamp else 0 end) as John_t from
        (
        (select dir.direction as direction, max(Timestamp) as Timestamp from
            (select distinct direction from trips) as dir
            left outer join
            (select * from trips where John = 1) as t
            on dir.direction = t.direction
            group by direction
        )
        cross join
        (select max(Timestamp) as max_Timestamp from trips where John = 1)
        )
    )
    natural join
    -- Mary --
    (select direction, (case when Timestamp=max_Timestamp then Timestamp else 0 end) as Mary_t from
        (
        (select dir.direction as direction, max(Timestamp) as Timestamp from
            (select distinct direction from trips) as dir
            left outer join
            (select * from trips where Mary = 1) as t
            on dir.direction = t.direction
            group by direction
        )
        cross join
        (select max(Timestamp) as max_Timestamp from trips where Mary = 1)
        )
    )
    natural join
    -- car --
    (select direction, (case when Timestamp=max_Timestamp then Timestamp else 0 end) as car_t from
        (
        (select dir.direction as direction, max(Timestamp) as Timestamp from
            (select distinct direction from trips) as dir
            left outer join
            (select * from trips where car = 1) as t
            on dir.direction = t.direction
            group by direction
        )
        cross join
        (select max(Timestamp) as max_Timestamp from trips where car = 1)
        )
    )
    natural join
    -- airplan --
    (select direction, (case when Timestamp=max_Timestamp then Timestamp else 0 end) as airplan_t from
        (
        (select dir.direction as direction, max(Timestamp) as Timestamp from
            (select distinct direction from trips) as dir
            left outer join
            (select * from trips where airplan = 1) as t
            on dir.direction = t.direction
            group by direction
        )
        cross join
        (select max(Timestamp) as max_Timestamp from trips where airplan = 1)
        )
    )
;

A brief explaination (from inner selects to outer):

  1. We left outer join all possible directions with all "trips" of John/Mary/car/airplan. This ensures that there is a row for each direction for each John, Mary, car and airplan. This is needed to fill in 0 later even if there was no "trip" in that direction.
  2. We "reduce" this to the maximum timestamp ( max(Timestamp) ) for each direction ( group by direction ) since we are not interested in earlier trips in the same direction.
  3. The result is cross joined with the overall maximum timestamp of John/Mary/car/airplan to allow replacing the lower timestamps with 0 using

     (case when Timestamp=max_Timestamp then Timestamp else 0) 
  4. We do this for each John, Mary, car and airplan and finally natural join everything together.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM