我有这样的情况
~id ~from ~to ~label ~weight
100 A B knows 2
100 A B knows 3
100 A B knows 4
但是我只想要最大日期的权重。 如何修改下面的CASE语句,以使ID仅存在1个条目。
查询:
(
select distinct
CASE WHEN *some-condition* as "~id"
,CASE *some-condition* as "~from"
,CASE *some-condition* as "~to"
,CASE *some-condition* as "~label"
,CASE ??? as "weight"
from
(select
dense_rank() over(partition by t.job_id order by start_time desc) rnk,
t.Date,
t.job_id,
t.start_time,
t.end_time,
t.dep_id,
t.table_name
.....
t.region_id,
from Table1 t
,Tabel2 J
where t.JOB_ID=J.JOB_ID
)
where rnk=1
order by JOB_ID,table_name
)
where "~id" is NOT NULL and "~label" is NOT NULL and "~from" is NOT NULL and "~to" is NOT NULL;
;
表t
job_id Date table_name ....... dep_id weight
100 2020-10-20 abc 1 2
100 2020-10-20 abc 2 3
100 2020-10-20 abc 3 4
100 2020-10-20 abc 4 10
100 2020-10-19 abc 3 2
结果中的输出权重应对应于最大dep_id。
~id ~from ~to ~label ~weight
100 A B knows 10
答案 0 :(得分:0)
很难找到解决方案,因为您没有说明~id, ~from, ~to, ~label
的计算方式。您应该可以使用window functions,即FIRST_VALUE()
来实现所需的输出:
...
,CASE *some-condition* as "~label"
,FIRST_VALUE(weight)OVER(ORDER BY dep_id desc) "weight"
...
您可能需要添加PARTITION BY
子句,这取决于您是希望整体获得第一个值,还是取决于其他一些条件。