我有一个如下的数据集 http://sqlfiddle.com/#!18/149382/2
结果应该如下
TripGroup Grouper1 Grouper2 Date Hour Code Description
1 6EA579BEA901 58639247D5CB 2018-02-03 00:01:00.0000000 3962X1 GARAG EXTRA
1 6EA579BEA901 58639247D5CB 2018-02-03 00:03:00.0000000 3962X2 RET EXTRA/GARAG
2 6EA579BEA901 58639247D5CB 2018-02-03 05:45:00.0000000 3962X1 GARAG EXTRA
2 6EA579BEA901 58639247D5CB 2018-02-03 07:30:00.0000000 3962X2 RET EXTRA/GARAG
3 6EA579BEA901 58639247D5CB 2018-02-03 05:55:00.0000000 3962X1 GARAG EXTRA
3 6EA579BEA901 58639247D5CB 2018-02-03 07:30:00.0000000 3962X2 RET EXTRA/GARAG
4 6EA579BEA901 58639247D5CB 2018-02-03 14:45:00.0000000 3962X1 GARAG EXTRA
4 6EA579BEA901 58639247D5CB 2018-02-03 15:40:00.0000000 3962X2 RET EXTRA/GARAG
我的问题是在RET
之前2 GARAG启动所有组都应该有一行以GARAG开头,一行以RET
开头答案 0 :(得分:1)
我猜这是你想要的。您还可以将where子句中的Description更改为Code。但如果没有,请告诉我并寻找答案。希望这可以帮助。感谢。
SELECT row_number() over (partition by Grouper1, Grouper2 order by Date, Hour) as TripGroup,
Grouper1,
Grouper2,
Date,
Hour,
Code,
Description
FROM FOO
where Description = 'GARAG EXTRA'
UNION ALL
SELECT row_number() over (partition by Grouper1, Grouper2 order by Date, Hour) as TripGroup,
Grouper1,
Grouper2,
Date,
Hour,
Code,
Description
FROM FOO
where Description = 'RET EXTRA/GARAG'
order by TripGroup, Description;
Result:
TripGroup Grouper1 Grouper2 Date Hour Code Description
1 6EA579BEA901 58639247D5CB 2018-02-03 00:01:00.0000000 3962X1 GARAG EXTRA
1 6EA579BEA901 58639247D5CB 2018-02-03 00:03:00.0000000 3962X2 RET EXTRA/GARAG
2 6EA579BEA901 58639247D5CB 2018-02-03 05:45:00.0000000 3962X1 GARAG EXTRA
2 6EA579BEA901 58639247D5CB 2018-02-03 07:30:00.0000000 3962X2 RET EXTRA/GARAG
3 6EA579BEA901 58639247D5CB 2018-02-03 05:55:00.0000000 3962X1 GARAG EXTRA
3 6EA579BEA901 58639247D5CB 2018-02-03 07:30:00.0000000 3962X2 RET EXTRA/GARAG
4 6EA579BEA901 58639247D5CB 2018-02-03 14:45:00.0000000 3962X1 GARAG EXTRA
4 6EA579BEA901 58639247D5CB 2018-02-03 15:40:00.0000000 3962X2 RET EXTRA/GARAG
答案 1 :(得分:0)
如果你想要一行以GARAG开头,一行以RET开头,那么这个怎么样?
schema