问题描述
我需要满足以下条件的时间戳:order_day ='4th'才能在1列中显示
这样的数据称为“ t1”:
timestamp id order_day
day 9 id1 5th
day 8 id1 4th
day 7 id1 3th
day 6 id1 2th
day 5 id1 1th
所需结果:
timestamp id order_day info
day 9 id1 5th day 8
day 8 id1 4th day 8
day 7 id1 3rd day 8
day 6 id1 2nd day 8
day 5 id1 1st day 8
当前我使用:
select *,min(timestamp) filter (where order_day = '4th') over (partition by id) as info
from t1
但是我不想使用min()
,因为这不是我的目的,因为每个id
的{{1}}只有1个'4th'
,所以请使用{{1 }}显示相同的结果。有什么办法吗?
我用[{1}} here
提出了一个基本相同的问题解决方法
您可以使用相关子查询作为替代选项-
select A.*,(select timestamp from t1 B where B.order_day = '4th' AND B.id = A.id) as info
from t1 A
另一种选择是-
select A.imestamp,A.id,A.order_day,B.timestamp as info
from t1 A
inner join (
select id,timestamp
from t1
where order_day = '4th'
) B
on A.id = B.id
请记住,这些只是其他选择,您需要选择最适合您的情况的一个。