Skip to content

Instantly share code, notes, and snippets.

View mhkeller's full-sized avatar

Michael Keller mhkeller

View GitHub Profile
@mhkeller
mhkeller / concat.bash
Created October 22, 2018 15:09
Concatenate csv files
# Adapted from https://unix.stackexchange.com/questions/60577/concatenate-multiple-files-with-same-header#170692
# `head -1` gets first line of the file of the file and stashes them as the header row into `all.txt`
# `tail -n +2 -q *.csv >> all.txt` grabs every csv file from the second row down and stashes them into `all.txt`
# `all.txt` is a csv file but we use the txt extension to avoid it being captured in the `*.csv` glob.
# You could also output to a csv file by having your input files share a naming convention such as `file-001.csv` and glob on `file*.csv`
# But renaming `all.txt` to `all.csv` is sometimes easier than worrying about a naming convention and txt and csvs are the same thing
head -1 my-csv-01.csv > all.txt; tail -n +2 -q *.csv >> all.txt
CREATE OR REPLACE FUNCTION rando(anyarray) RETURNS any AS $BODY$
SELECT unnest($1::anyarray ORDER BY RANDOM()) LIMIT 1
$BODY$
LANGUAGE SQL;
@mhkeller
mhkeller / ntile-counts.sql
Created September 20, 2018 15:47
Calculat quantile amounts based on aggregate counts. Adapted from https://gist.github.com/hrwgc/5171860
SELECT
ntile,
avg(ct) AS avgAmount,
max(ct) AS maxAmount,
min(ct) AS minAmount,
median(ct) as median
FROM (SELECT ct, ntile(5) OVER (ORDER BY ct) AS ntile FROM (select count(col_name) as ct from table GROUP BY col_Name) t) x
GROUP BY ntile
ORDER BY ntile;
@mhkeller
mhkeller / README.md
Last active August 1, 2018 14:29
Count elements in a pgsql array_agg. Adapted to work with any typed array (numeric, text etc.) and more machine-readable format from https://wiki.postgresql.org/wiki/Count_elements_in_Array
-routes
--[category]
---_components
----List.html
---index.html
--index.html
--4xx.html
--5xx.html
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
/**
* This function takes a canvas, context, width and height. It scales both the
* canvas and the context in such a way that everything you draw will be as
* sharp as possible for the device.
*
* It doesn't return anything, it just modifies whatever canvas and context you
* pass in.
*
* Adapted from Paul Lewis's code here:
* http://www.html5rocks.com/en/tutorials/canvas/hidpi/

Keybase proof

I hereby claim:

  • I am mhkeller on github.
  • I am mhkeller (https://keybase.io/mhkeller) on keybase.
  • I have a public key ASB0OSFrnFF7g4pH9KGZL66qEGevF_sPvBkdHH9o9gxC6Qo

To claim this, I am signing this object:

@mhkeller
mhkeller / variable.sql
Created February 15, 2018 22:39
Setting a variable in pgsql
-- least onerous option from here https://stackoverflow.com/questions/1490942/how-to-declare-a-variable-in-a-postgresql-query#16552441
set session my.vars.id = '%my_string%';
SELECT
*
FROM
my_table
WHERE
field LIKE current_setting('my.vars.id')::text
OR other_field LIKE current_setting('my.vars.id')::text