gpt4 book ai didi

mysql - hive 中的等级/组

转载 作者:可可西里 更新时间:2023-11-01 15:21:16 27 4
gpt4 key购买 nike

我将屏幕截图中的前三列按记录时间排列。我想给组​​相同的范围,直到出现区域“CDEF”。你能建议一下,我怎样才能在配置单元中实现这个结果。

enter image description here

CREATE TABLE TEST (RECORD_DATE STRING, AREA STRING, REASON STRING);

INSERT INTO TEST (RECORD_DATE, AREA, REASON)
VALUES ('10/11/2017','A','a'),
('10/12/2017','A','a'),
('10/13/2017','B','b'),
('10/14/2017','CDEF','A:B'),
('10/15/2017','E','e') ,
('10/16/2017','F','f'),
('10/17/2017','CDEF','E:F');

谢谢。

最佳答案

我无法一次性获得结果,所以我使用了中间表。希望这些步骤是解释性的。检查一下。

select * from sam;

+------------------+-----------+-------------+--+
| sam.record_date | sam.area | sam.reason |
+------------------+-----------+-------------+--+
| 2017-10-11 | A | a |
| 2017-10-12 | A | a |
| 2017-10-13 | B | b |
| 2017-10-14 | CDEF | A:B |
| 2017-10-15 | E | e |
| 2017-10-16 | F | f |
| 2017-10-17 | CDEF | E:F |
+------------------+-----------+-------------+--+


create table sam2 as select s1.*, s2.record_date as dt2 from sam s1 , ( select record_date,area,reason from sam where area='CDEF') s2 where s2.record_date >=s1.record_date;

+-------------------+------------+--------------+-------------+--+
| sam2.record_date | sam2.area | sam2.reason | sam2.dt2 |
+-------------------+------------+--------------+-------------+--+
| 2017-10-11 | A | a | 2017-10-14 |
| 2017-10-12 | A | a | 2017-10-14 |
| 2017-10-13 | B | b | 2017-10-14 |
| 2017-10-14 | CDEF | A:B | 2017-10-14 |
| 2017-10-11 | A | a | 2017-10-17 |
| 2017-10-12 | A | a | 2017-10-17 |
| 2017-10-13 | B | b | 2017-10-17 |
| 2017-10-14 | CDEF | A:B | 2017-10-17 |
| 2017-10-15 | E | e | 2017-10-17 |
| 2017-10-16 | F | f | 2017-10-17 |
| 2017-10-17 | CDEF | E:F | 2017-10-17 |
+-------------------+------------+--------------+-------------+--+

create table sam3 as select s.*, row_number() over(partition by record_date order by dt2) rn from sam2 s;

+----------------+---------+-----------+-------------+-----+--+
| s.record_date | s.area | s.reason | s.dt2 | rn |
+----------------+---------+-----------+-------------+-----+--+
| 2017-10-11 | A | a | 2017-10-14 | 1 |
| 2017-10-11 | A | a | 2017-10-17 | 2 |
| 2017-10-12 | A | a | 2017-10-14 | 1 |
| 2017-10-12 | A | a | 2017-10-17 | 2 |
| 2017-10-13 | B | b | 2017-10-14 | 1 |
| 2017-10-13 | B | b | 2017-10-17 | 2 |
| 2017-10-14 | CDEF | A:B | 2017-10-14 | 1 |
| 2017-10-14 | CDEF | A:B | 2017-10-17 | 2 |
| 2017-10-15 | E | e | 2017-10-17 | 1 |
| 2017-10-16 | F | f | 2017-10-17 | 1 |
| 2017-10-17 | CDEF | E:F | 2017-10-17 | 1 |
+----------------+---------+-----------+-------------+-----+--+

select s.*, dense_rank() over(order by dt2 ) rk from sam3 s where rn=1 order by record_date;

+----------------+---------+-----------+-------------+-------+-----+--+
| s.record_date | s.area | s.reason | s.dt2 | s.rn | rk |
+----------------+---------+-----------+-------------+-------+-----+--+
| 2017-10-11 | A | a | 2017-10-14 | 1 | 1 |
| 2017-10-12 | A | a | 2017-10-14 | 1 | 1 |
| 2017-10-13 | B | b | 2017-10-14 | 1 | 1 |
| 2017-10-14 | CDEF | A:B | 2017-10-14 | 1 | 1 |
| 2017-10-15 | E | e | 2017-10-17 | 1 | 2 |
| 2017-10-16 | F | f | 2017-10-17 | 1 | 2 |
| 2017-10-17 | CDEF | E:F | 2017-10-17 | 1 | 2 |
+----------------+---------+-----------+-------------+-------+-----+--+

关于mysql - hive 中的等级/组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52822516/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com