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1lIjoia2V5YmFzZS5pbyBnbyBjbGllbnQiLCJ2ZXJzaW9uIjoiMS4wLjMz
select 1 in (1,2);
select 3 in (1,2);
select 3 not in (1,2);
select null in (1,2,null);
select null not in (1,2,null);
-- This SQL snippet attributes an interest to traffic source and campaign.
-- Any interest event is attributed to FIRST traffic_source event for that
-- user.
with
-- first order traffic_sources per user, and number them with row_number
ordered_traffic_sources as (
-- This SQL snippet attributes an interest to traffic source and campaign.
-- Any interest event is attributed to previous traffic_source event.
--
-- The approach used here, uses 'lag() ignore nulls'. This is supported
-- on Redshift, but not in PostgreSQL. This approach yields a shorter,
-- more readable, and likely faster query, but it is less portable.
with
-- Merge traffic_sources and interest events in one event pool.
-- This SQL snippet attributes an interest to traffic source. Any interest
-- event is attributed to previous traffic_source event.
with
-- Merge traffic_sources and interest events in one event pool.
-- * pad with 'null' every field that is not common between these two events
-- * add a new field 'is_first_event'; set it to 1 for traffic_source events
-- and to 0 for interest events
-- * add a new field 'type' with event type: interest or traffic_source.
@quarterdome
quarterdome / sql_challenge.md
Last active August 29, 2015 14:25
Events SQL Challenge

Analyzing Events - SQL Challenge

Lets assume you have the following tables tracking web app activity in a fully featured SQL database (e.g. PostgreSQL).

identifies 
  user_id
  sent_at
  email
  first_name
@quarterdome
quarterdome / redshift_session.sql
Last active August 29, 2015 14:25
Defining a session from Redshift events table
with
-- First add a column to tracks table with the timestamp of
-- previous event (using lag window function). Note, that
-- lag() will return null if it is first event.
linked_tracks as (
select
*,
lag(sent_at, 1) over
@quarterdome
quarterdome / apartmentlist_code_test.txt
Last active September 12, 2016 22:08
Apartment List Coding Problem: Snippets
Welcome to Apartment List code test!
For an apartment search we need to generate description snippets that match the search query. For example, lets assume we have the apartment with the following description:
"Our luxury loft-style apartments were constructed as condominiums, so your new residence will have: Solid floors and walls (this will be the quietest apartment you've EVER lived in); Premium stainless steel designer appliances; Distinctive accent walls and hardwood flooring; A kitchen that most chefs would drool over with easy to clean gas stove and countertops; Walk in closets with built in storage; Full size washer and dryer in each apartment home. In addition, all residents will enjoy use of our top-notch amenities, including reserved parking, cutting-edge fitness center, wireless internet cafe/business center, and rooftop lounge to soak up the sun!"
Lets consider a user searching for "designer kitchen". In this case, a good snippet would be something like this:
"Premium stainless steel designer
@quarterdome
quarterdome / gist:4669408
Last active December 11, 2015 22:28
A postgres conundrum
#
# lets say we have a LARGE table
test=# select count(*) from blah;
count
----------
20000000
(1 row)
#