蜂巢中的排名/组

时间:2018-10-15 18:11:27

标签: mysql sql hadoop hive hiveql

我有按记录时间排列的屏幕快照的前三列。 我想给组​​相同的范围,直到出现“ 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');

谢谢。

1 个答案:

答案 0 :(得分:1)

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

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   |
 +----------------+---------+-----------+-------------+-------+-----+--+