sql跳过条件的最大行

fiei3ece  于 2021-05-29  发布在  Hadoop
关注(0)|答案(3)|浏览(402)

我需要返回最新的可用行,但有条件。因为hive不支持pl-t/sql,所以我需要处理函数。
当前代码只选择最新记录,不接受 ACTIVE_F 考虑到。

WITH CTE AS 

(select 
ID, 
myuser_insert_time as insert_time,
max(myuser_insert_time) OVER (PARTITION BY ID ORDER BY ID) as rn
from tbl1)

SELECT * FROM CTE 
WHERE rn =  insert_time

我的数据:

MYUSER_INSERT_TIME        ACTIVE_F
2019-06-14 15:00:32.000   6
2019-03-06 15:54:22.000   0
2019-01-25 08:43:45.000   1
2018-12-13 09:49:50.000   0
2018-11-24 10:11:06.000   0
2018-11-06 12:17:34.000   1
2018-07-04 16:59:15.000   0
2018-05-29 12:22:15.000   1
2018-05-24 20:19:00.000   2
2018-05-24 20:19:00.000   2

预期行为:
查找最新记录(完成)
选中活动的\u f(当6-移到下一行并返回该行时,否则继续下一行)
期望结果:

MYUSER_INSERT_TIME        ACTIVE_F
2019-03-06 15:54:22.000   0
e5nszbig

e5nszbig1#

按活动\u f so 6有条件地排序行

WITH CTE AS 

(select 
ID, 
myuser_insert_time as insert_time,
row_number() OVER (PARTITION BY ID ORDER BY case ACTIVE_F when 6 then 1 else 0 end,  eendmyuser_insert_time desc) as rn
from tbl1)

SELECT * FROM CTE 
WHERE rn = 1
pcww981p

pcww981p2#

你好像只是想过滤掉 active_f = 6 ,如果我理解正确的话。在计算最大插入时间之前,应该这样做;也就是说,在cte中:

with cte as (
      select ID, myuser_insert_time as insert_time,
             max(myuser_insert_time) over (partition by ID) as max_myuser_insert_time
      from tbl1
      where active_f <> 6
    )
select * 
from CTE 
where myuser_insert_time = insert_time;

你的 max() 也有一个 order by 列,所以你是采取累计 max() . 这是没有必要的。这个 order by 是不必要的(即使代码有效)。
编辑:
如果您需要“6”,如果它是唯一一行,那么使用 row_number() 最后一行:

with cte as (
      select ID, myuser_insert_time as insert_time,
             row_number() over (partition by id
                                order by (case when active_f = 6 then 2 else 1 end),
                                         myuser_insert_time desc
                               ) as rn
      from tbl1
    )
select * 
from CTE 
where rn = 1;
nkkqxpd9

nkkqxpd93#

添加 case when active_f =6 then 1 else 0 endorder by 解析函数的子句。活动的记录\u f=6人优先。同时使用行号和顺序,按myuser插入时间描述:

WITH CTE AS 

(select 
ID, 
myuser_insert_time as insert_time,
row_number() OVER (PARTITION BY ID ORDER BY myuser_insert_time desc, case when active_f =6 then 1 else 0 end) as rn
from tbl1)

SELECT * FROM CTE 

WHERE rn =  1

相关问题