简体   繁体   English

如何在一个sqlite命令中将多个行项目作为不同的列

[英]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 / mary / together在时间戳记乘汽车/飞机返回xxx,我希望有一个单独的sqlite查询来获得如下结果:

          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. 这表示(约翰,玛丽,汽车,飞机计划) (往/返)到xxx的最新时间戳。

I try the sql command , but failed... 我尝试sql命令,但是失败了...

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: 根据Andriy M评论 ,这是答案:

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. 我们将John / Mary / car / airplan的所有“行程” left outer join所有可能的方向进行left outer join联接。 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. 即使在该方向上没有“行程”,也需要稍后填写0
  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. 我们将其“减少”为每个方向( group by direction max(Timestamp)的最大时间戳( max(Timestamp) ),因为我们对同一方向上的较早行程不感兴趣。
  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 将结果与John / Mary / car / airplan的整体最大时间戳交叉连接,以允许将较低的时间戳替换为0使用

     (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. 我们为约翰,玛丽,汽车和飞机计划的每一个人做到这一点,并最终natural join一切融合在一起。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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