根据条件将多行转换为单行
Convert multiple rows into single row based on condition
以下是我的 table 的输入。
Name Starttime Endtime
ABC 2019-06-12 01:52:46 2019-06-12 02:52:46
DEF 2019-06-12 02:52:46 2019-06-12 03:52:46
GHI 2019-06-12 03:52:46 2019-06-12 04:52:46
JKL 2019-06-12 04:52:46 2019-06-12 05:52:46
ABC 2019-06-11 01:22:46 2019-06-11 02:22:46
DEF 2019-06-11 02:22:46 2019-06-11 03:22:46
GHI 2019-06-11 03:22:46 2019-06-11 04:22:46
JKL 2019-06-11 04:22:46 2019-06-11 05:22:46
期望的输出
DATE STARTTIME ENDTIME
2019-06-12 2019-06-12 01:52:46 2019-06-12 03:52:46
我输出的开始时间是名字 'ABC' 的开始时间,结束时间是名字 'DEF' 的结束时间。需要获取最新的数据。我只需要名称 ABC 和 DEF。
如果我没理解错的话,你只需要一些条件聚合:
max(case when name = 'ABC' then StartTime else null end) as StartTime,
max(Case when name = 'DEF' the EndTime else null end) as EndTime
以下是我的 table 的输入。
Name Starttime Endtime
ABC 2019-06-12 01:52:46 2019-06-12 02:52:46
DEF 2019-06-12 02:52:46 2019-06-12 03:52:46
GHI 2019-06-12 03:52:46 2019-06-12 04:52:46
JKL 2019-06-12 04:52:46 2019-06-12 05:52:46
ABC 2019-06-11 01:22:46 2019-06-11 02:22:46
DEF 2019-06-11 02:22:46 2019-06-11 03:22:46
GHI 2019-06-11 03:22:46 2019-06-11 04:22:46
JKL 2019-06-11 04:22:46 2019-06-11 05:22:46
期望的输出
DATE STARTTIME ENDTIME
2019-06-12 2019-06-12 01:52:46 2019-06-12 03:52:46
我输出的开始时间是名字 'ABC' 的开始时间,结束时间是名字 'DEF' 的结束时间。需要获取最新的数据。我只需要名称 ABC 和 DEF。
如果我没理解错的话,你只需要一些条件聚合:
max(case when name = 'ABC' then StartTime else null end) as StartTime,
max(Case when name = 'DEF' the EndTime else null end) as EndTime