Skip to content

Instantly share code, notes, and snippets.

View hansthen's full-sized avatar

Hans Then hansthen

  • The Netherlands
View GitHub Profile
create materialized view mview
as
select a.userid, a.balance, d.date
from (select d as date from generate_series(timestamp '2023-05-01', '2023-06-2', interval '1 day') d) d
join lateral (select userid, balance, date from balance where date <= d.date order by date desc limit 1) a
on true union select userid, balance, date from balance order by date;
create materialized view mview2
as
select a.userid, a.balance, a.date as start, b.date as fin
@hansthen
hansthen / vimrc
Created May 9, 2023 21:37
My vimrc
set nocompatible " be iMproved, required
filetype off " required
set rtp+=~/.vim/bundle/Vundle.vim
call vundle#begin()
" let Vundle manage Vundle, required
Plugin 'VundleVim/Vundle.vim'
Plugin 'tpope/vim-fugitive'
Plugin 'scrooloose/nerdtree'
# code is from streamlit forums https://discuss.streamlit.io/t/ag-grid-component-with-input-support/8108/385
df=pd.DataFrame({ "Name": ['Erica', 'Rogers', 'Malcolm', 'Barrett'], "Age": [43, 35, 57, 29]})
add_row_func = """function(e)
{ let api = e.api;
let rowIndex = e.rowIndex + 1;
let vnme = e.data.Name;
let vage = e.data.Age;
@hansthen
hansthen / resample.psql
Last active January 7, 2023 19:39
Postgres query to resample data to a specific interval (based on something from SO, but adapted use only one table)
SELECT a.user_id, b.balance, d.as_of_date
FROM (
SELECT d AS as_of_date
FROM generate_series(timestamp '2015-12-29', '2016-01-10', interval '1 hour') d
) d
JOIN LATERAL (
select user_id, as_of_date
from balances
where as_of_date <= d.as_of_date
order by as_of_date
@hansthen
hansthen / bin_counter.py
Created March 14, 2022 22:35
Determine bins from a duration
from datetime import timedelta, datetime
from collections import Counter
def bin_days(d1, d2):
counter = Counter()
d_next = d1.replace(hour = 0, minute=0, second=0) + timedelta(days=1)
weekday = d1.weekday()
counter[weekday] += (d_next - d1).total_seconds() / (24 * 60 * 60)
delta = d2 - d_next