Skip to content

Instantly share code, notes, and snippets.

@dankleiman
dankleiman / daily_metrics.sql
Created September 27, 2019 19:28
Data set up script for dankleiman.com/2019/09/27/sql-quick-tip-showing-changes-in-your-data/
CREATE TABLE daily_metrics AS (
SELECT date, metric, count FROM (SELECT '2019-09-01', 'A', 115) as r(date, metric, count)
UNION
SELECT date, metric, count FROM (SELECT '2019-09-02', 'A', 98) as r2(date, metric, count)
UNION
SELECT date, metric, count FROM (SELECT '2019-09-03', 'A', 268) as r2(date, metric, count)
UNION
SELECT date, metric, count FROM (SELECT '2019-09-04', 'A', 451) as r4(date, metric, count)
UNION
SELECT date, metric, count FROM (SELECT '2019-09-05', 'A', 239) as r5(date, metric, count)
CREATE TABLE students (id serial primary key, first_name text, last_name text);
CREATE TABLE quizzes (id serial primary key, quiz_date date, topic text);
CREATE TABLE quiz_results (quiz_id int references quizzes(id), student_id int references students(id), score float);
INSERT INTO students (first_name, last_name) VALUES ('Bernadette','Stanton'),('Lexus','Stokes'),('Bartholome','Sauer'),('Mikel','Ferry'),('Reggie','Bins'),('Lavern','Blick'),('Angus','Murray'),('Effie','Volkman'),('Orie','Hartmann'),('Aracely','Friesen'),('Brenda','Kirlin'),('Giovanna','Buckridge'),('Samantha','Block'),('Ruben','Stamm'),('Francesca','Stamm'),('Louvenia','Collins'),('Annabell','Batz'),('Webster','O''Conner'),('Bertram','Fritsch'),('Michele','Lesch'),('Keagan','Cronin'),('Broderick','Brakus'),('Horacio','Rodriguez'),('Willie','Schumm'),('Vance','Collier'),('Michaela','Gibson'),('Devin','Schumm'),('Narciso','Kautzer'),('Burley','Considine'),('Juliet','Hand'),('Katrina','Deckow'),('Deven','Leannon'),('Aurore','Littel'),('Annalise
-- First set up the relevant DB/tables
CREATE DATABASE words;
CREATE TABLE word_list (word text);
CREATE TABLE words (word text, len int, letters text);
-- Create a seed script for `word_list` and run the sql file into your db
-- cat /usr/share/dict/words | awk '{ print "INSERT INTO word_list VALUES('\''" $1 "'\'');" }' > seed.sql
-- psql words -f seed.sql
-- Now we need to build the `words` table from pg functions off the `word_list` table
@dankleiman
dankleiman / buckets.rb
Created March 10, 2017 20:08
Code for Real-time Deduping at scala
buckets = keys_per_minute / keys_per_bucket
buckets_per_min = 2,000,000 / 100 = 20,000
buckets_per_sec = 20,000 / 60 = 333

Doing things manually is for chumps.

Tjsh is meant to wrap up some common tasks that Tapjoy developers do all the time and make them easier and maybe even a little magical.

tjsh tiab push --restart

will push all dirty files in your git index up to your tiab, put them in the right place on the box and restart the service.

How does it know all this stuff? Magic.

@dankleiman
dankleiman / sql_query_exercises.md
Last active August 29, 2015 14:01
SQL_Query_Exercises

MOVIES

  1. What are the top 50 worst rated movies? The results should include the movie title and rating and be sorted by the worst rating first.

SELECT title, rating FROM movies WHERE rating IS NOT NULL ORDER BY movies.rating ASC LIMIT 50;

  1. What movies do not have a rating? The results should include just the movie titles in sorted order.

SELECT title FROM movies WHERE rating IS NULL ORDER BY title ASC;

@dankleiman
dankleiman / cash_register_1.rb
Last active August 29, 2015 14:01
Cash Register 1
#input amount due
puts "What is the amount due?"
due = gets.chomp.to_f
#input amount tendered
puts "What is the amount tendered?"
tendered = gets.chomp.to_f
#if tendered >= due, print receipt
change = (tendered-due).abs
change = '%.2f' % change
if tendered >= due