這兩天的工作涉及了一些數(shù)據(jù)整理,所以來一波好久不見的sql記錄。
多次更新取最新時間的數(shù)據(jù)
在一組數(shù)據(jù)中,uuid相同的記錄有多條,每一條的update_time不同,想取每一個uuid最后一條更新記錄。
select t1.uuid, t1.update_time
from record t1
inner join (select t2.uuid, max(t2.update_time) as update_time
from record t2
group by t2.uuid) as t3
on t1.uuid = t3.uuid and t1.update_time = t3.update_time
取特定星期幾的數(shù)據(jù)
用到的就是下面的語句,感謝internet。輸出的星期一對應(yīng)1,星期日對應(yīng)0。
select extract(DOW FROM cast('2022-03-04 16:25' as TIMESTAMP)) weekday;
輸出結(jié)果

nice day
對輸出的特定字段增加條件
對于一些數(shù)據(jù),如果當(dāng)滿足條件1輸出一個結(jié)果,滿足條件2輸出另外的結(jié)果,可以利用case語句。
select uuid,
case
when count > 100 then 100
else count end,
update_time
from record
# count大于100,均輸出100;否則輸出本來記錄的count
與特定時間間隔固定長度的數(shù)據(jù)
想要輸出update_time是在publish_time三天后的數(shù)據(jù)。
select *
from record
where date_trunc('day', update_time) =
date_trunc('day', t1.publish_time) + interval '3 day'
#加號后面是時間間隔,可進(jìn)行靈活調(diào)整。
新的一個月開始啦~ 最近的變化也很多,感覺不踏實...但是就是還在努力上進(jìn)找方向呀~
自我記錄,有錯誤歡迎指正~