aboutsummaryrefslogtreecommitdiff
path: root/doc/notes.org
diff options
context:
space:
mode:
Diffstat (limited to 'doc/notes.org')
-rw-r--r--doc/notes.org290
1 files changed, 290 insertions, 0 deletions
diff --git a/doc/notes.org b/doc/notes.org
new file mode 100644
index 0000000..d3a4dce
--- /dev/null
+++ b/doc/notes.org
@@ -0,0 +1,290 @@
1#+property: header-args:sqlite :dir ../res/ :db db.sqlite :colnames yes
2
3#+begin_src elisp :results none
4(org-babel-do-load-languages
5 'org-babel-load-languages (quote ((emacs-lisp . t)
6 (sqlite . t))))
7#+end_src
8
9#+begin_src sqlite :results none
10CREATE TABLE IF NOT EXISTS cards(
11 id INTEGER,
12 client_id INTEGER,
13 card_brand TEXT,
14 card_type TEXT,
15 card_number TEXT,
16 expires TEXT,
17 cvv TEXT,
18 has_chip INTEGER,
19 num_cards_issued INTEGER,
20 credit_limit INTEGER,
21 acct_open_date TEXT,
22 year_pin_last_changed INTEGER,
23 card_on_dark_web INTEGER
24);
25
26CREATE INDEX IF NOT EXISTS idx_cards_credit ON cards(credit_limit);
27CREATE INDEX IF NOT EXISTS idx_cards_id_client ON cards(id, client_id);
28
29CREATE TABLE IF NOT EXISTS users(
30 id INTEGER,
31 current_age INTEGER,
32 retirement_age INTEGER,
33 birth_year INTEGER,
34 birth_month INTEGER,
35 gender TEXT,
36 address TEXT,
37 latitude REAL,
38 longitude REAL,
39 per_capita_income INTEGER,
40 yearly_income INTEGER,
41 total_debt INTEGER,
42 credit_score INTEGER,
43 num_credit_cards INTEGER
44);
45
46CREATE INDEX IF NOT EXISTS idx_users_id ON users(id);
47
48CREATE TABLE IF NOT EXISTS transactions(
49 id INTEGER,
50 date TEXT,
51 client_id INTEGER,
52 card_id INTEGER,
53 amount INTEGER,
54 use_chip TEXT,
55 merchant_id INTEGER,
56 merchant_city TEXT,
57 merchant_state TEXT,
58 zip REAL,
59 mcc INTEGER,
60 errors TEXT
61);
62
63CREATE INDEX IF NOT EXISTS idx_transactions_card ON transactions(card_id);
64CREATE INDEX IF NOT EXISTS idx_transactions_client_city_month
65 ON transactions(client_id, merchant_city, date);
66#+end_src
67
68#+begin_src sqlite :results none
69.mode csv
70
71.import --skip 1 cards_data.csv cards
72UPDATE cards
73SET credit_limit = CAST(SUBSTR(credit_limit, 2) AS INTEGER);
74
75.import --skip 1 users_data.csv users
76UPDATE users
77SET per_capita_income = CAST(SUBSTR(per_capita_income, 2) AS INTEGER);
78
79.import --skip 1 transactions_data.csv transactions
80UPDATE transactions
81SET amount = CAST(SUBSTR(amount, 2, LENGTH(amount) - 4) || SUBSTR(amount, -2) AS INTEGER)
82#+end_src
83
84#+begin_src sqlite
85SELECT * FROM cards LIMIT 10;
86#+end_src
87
88#+RESULTS:
89| id | client_id | card_brand | card_type | card_number | expires | cvv | has_chip | num_cards_issued | credit_limit | acct_open_date | year_pin_last_changed | card_on_dark_web |
90|------+-----------+------------+-----------------+------------------+---------+-----+----------+------------------+--------------+----------------+-----------------------+------------------|
91| 4524 | 825 | Visa | Debit | 4344676511950444 | 12/2022 | 623 | YES | 2 | 24295 | 09/2002 | 2008 | No |
92| 2731 | 825 | Visa | Debit | 4956965974959986 | 12/2020 | 393 | YES | 2 | 21968 | 04/2014 | 2014 | No |
93| 3701 | 825 | Visa | Debit | 4582313478255491 | 02/2024 | 719 | YES | 2 | 46414 | 07/2003 | 2004 | No |
94| 42 | 825 | Visa | Credit | 4879494103069057 | 08/2024 | 693 | NO | 1 | 12400 | 01/2003 | 2012 | No |
95| 4659 | 825 | Mastercard | Debit (Prepaid) | 5722874738736011 | 03/2009 | 75 | YES | 1 | 28 | 09/2008 | 2009 | No |
96| 4537 | 1746 | Visa | Credit | 4404898874682993 | 09/2003 | 736 | YES | 1 | 27500 | 09/2003 | 2012 | No |
97| 1278 | 1746 | Visa | Debit | 4001482973848631 | 07/2022 | 972 | YES | 2 | 28508 | 02/2011 | 2011 | No |
98| 3687 | 1746 | Mastercard | Debit | 5627220683410948 | 06/2022 | 48 | YES | 2 | 9022 | 07/2003 | 2015 | No |
99| 3465 | 1746 | Mastercard | Debit (Prepaid) | 5711382187309326 | 11/2020 | 722 | YES | 2 | 54 | 06/2010 | 2015 | No |
100| 3754 | 1746 | Mastercard | Debit (Prepaid) | 5766121508358701 | 02/2023 | 908 | YES | 1 | 99 | 07/2006 | 2012 | No |
101
102#+begin_src sqlite
103SELECT * FROM users LIMIT 10;
104#+end_src
105
106#+RESULTS:
107| id | current_age | retirement_age | birth_year | birth_month | gender | address | latitude | longitude | per_capita_income | yearly_income | total_debt | credit_score | num_credit_cards |
108|------+-------------+----------------+------------+-------------+--------+--------------------------+----------+-----------+-------------------+---------------+------------+--------------+------------------|
109| 825 | 53 | 66 | 1966 | 11 | Female | 462 Rose Lane | 34.15 | -117.76 | 29278 | $59696 | $127613 | 787 | 5 |
110| 1746 | 53 | 68 | 1966 | 12 | Female | 3606 Federal Boulevard | 40.76 | -73.74 | 37891 | $77254 | $191349 | 701 | 5 |
111| 1718 | 81 | 67 | 1938 | 11 | Female | 766 Third Drive | 34.02 | -117.89 | 22681 | $33483 | $196 | 698 | 5 |
112| 708 | 63 | 63 | 1957 | 1 | Female | 3 Madison Street | 40.71 | -73.99 | 163145 | $249925 | $202328 | 722 | 4 |
113| 1164 | 43 | 70 | 1976 | 9 | Male | 9620 Valley Stream Drive | 37.76 | -122.44 | 53797 | $109687 | $183855 | 675 | 1 |
114| 68 | 42 | 70 | 1977 | 10 | Male | 58 Birch Lane | 41.55 | -90.6 | 20599 | $41997 | $0 | 704 | 3 |
115| 1075 | 36 | 67 | 1983 | 12 | Female | 5695 Fifth Street | 38.22 | -85.74 | 25258 | $51500 | $102286 | 672 | 3 |
116| 1711 | 26 | 67 | 1993 | 12 | Male | 1941 Ninth Street | 45.51 | -122.64 | 26790 | $54623 | $114711 | 728 | 1 |
117| 1116 | 81 | 66 | 1938 | 7 | Female | 11 Spruce Avenue | 40.32 | -75.32 | 26273 | $42509 | $2895 | 755 | 5 |
118| 1752 | 34 | 60 | 1986 | 1 | Female | 887 Grant Street | 29.97 | -92.12 | 18730 | $38190 | $81262 | 810 | 1 |
119
120#+begin_src sqlite
121SELECT * FROM transactions LIMIT 10;
122#+end_src
123
124#+RESULTS:
125| id | date | client_id | card_id | amount | use_chip | merchant_id | merchant_city | merchant_state | zip | mcc | errors |
126|---------+---------------------+-----------+---------+---------+--------------------+-------------+---------------+----------------+---------+------+--------|
127| 7475327 | 2010-01-01 00:01:00 | 1556 | 2972 | -7700.0 | Swipe Transaction | 59935 | Beulah | ND | 58523.0 | 5499 | |
128| 7475328 | 2010-01-01 00:02:00 | 561 | 4575 | 1457.0 | Swipe Transaction | 67570 | Bettendorf | IA | 52722.0 | 5311 | |
129| 7475329 | 2010-01-01 00:02:00 | 1129 | 102 | 8000.0 | Swipe Transaction | 27092 | Vista | CA | 92084.0 | 4829 | |
130| 7475331 | 2010-01-01 00:05:00 | 430 | 2860 | 20000.0 | Swipe Transaction | 27092 | Crown Point | IN | 46307.0 | 4829 | |
131| 7475332 | 2010-01-01 00:06:00 | 848 | 3915 | 4641.0 | Swipe Transaction | 13051 | Harwood | MD | 20776.0 | 5813 | |
132| 7475333 | 2010-01-01 00:07:00 | 1807 | 165 | 481.0 | Swipe Transaction | 20519 | Bronx | NY | 10464.0 | 5942 | |
133| 7475334 | 2010-01-01 00:09:00 | 1556 | 2972 | 7700.0 | Swipe Transaction | 59935 | Beulah | ND | 58523.0 | 5499 | |
134| 7475335 | 2010-01-01 00:14:00 | 1684 | 2140 | 2646.0 | Online Transaction | 39021 | ONLINE | | | 4784 | |
135| 7475336 | 2010-01-01 00:21:00 | 335 | 5131 | 26158.0 | Online Transaction | 50292 | ONLINE | | | 7801 | |
136| 7475337 | 2010-01-01 00:21:00 | 351 | 1112 | 1074.0 | Swipe Transaction | 3864 | Flushing | NY | 11355.0 | 5813 | |
137
138#+begin_src sqlite
139PRAGMA journal_mode = OFF;
140PRAGMA synchronous = OFF;
141PRAGMA temp_store = MEMORY;
142
143WITH
144cards_of_interest AS (
145 SELECT id as card_id
146 FROM cards
147 WHERE credit_limit >= 12000
148),
149
150transactions_of_interest AS (
151 SELECT
152 t.merchant_city,
153 t.client_id,
154 strftime('%Y-%m', t.date) as month,
155 t.amount
156 FROM transactions t
157 JOIN cards_of_interest c ON t.card_id = c.card_id
158),
159
160monthly AS (
161 SELECT
162 t.merchant_city,
163 t.client_id,
164 t.month,
165 SUM(t.amount) AS amount
166 FROM transactions_of_interest t
167 GROUP BY t.merchant_city, t.client_id, t.month
168),
169
170normalized AS (
171 SELECT
172 m.merchant_city,
173 m.client_id,
174 m.month,
175 m.amount / (u.per_capita_income / 12.0) AS amount
176 FROM monthly m
177 JOIN users u ON m.client_id = u.id
178)
179
180SELECT *
181FROM normalized
182ORDER BY normalized.amount DESC
183LIMIT 100;
184#+end_src
185
186#+RESULTS:
187| journal_mode | | | |
188|----------------+-----------+---------+------------------|
189| delete | | | |
190| merchant_city | client_id | month | amount |
191| Houston | 1340 | 2010-10 | 679.564723694171 |
192| Houston | 1340 | 2010-06 | 676.406207418622 |
193| Houston | 1340 | 2010-07 | 666.335200605602 |
194| Houston | 1340 | 2011-06 | 660.198485995458 |
195| Yorba Linda | 96 | 2011-06 | 652.132895142241 |
196| Houston | 1340 | 2010-09 | 649.042392127176 |
197| Houston | 1340 | 2010-04 | 640.598788796366 |
198| Houston | 1340 | 2011-11 | 631.538834216503 |
199| Houston | 1340 | 2011-02 | 609.198940196821 |
200| Yorba Linda | 96 | 2015-10 | 604.364588682346 |
201| Houston | 1340 | 2011-09 | 597.758516275549 |
202| Yorba Linda | 96 | 2011-10 | 595.716134332957 |
203| Yorba Linda | 96 | 2011-09 | 593.845332237856 |
204| Houston | 1340 | 2010-11 | 589.237244511734 |
205| Houston | 1340 | 2010-01 | 582.088115064345 |
206| Orlando | 464 | 2010-12 | 579.945220388173 |
207| ONLINE | 1198 | 2015-07 | 578.782136231116 |
208| Yorba Linda | 96 | 2013-08 | 567.000616206224 |
209| Yorba Linda | 96 | 2011-08 | 565.96261682243 |
210| Houston | 1340 | 2011-08 | 565.072369417108 |
211| Yorba Linda | 96 | 2012-10 | 562.521413166273 |
212| Yorba Linda | 96 | 2014-07 | 561.647324637979 |
213| Orlando | 464 | 2017-01 | 561.633956103755 |
214| Orlando | 464 | 2016-03 | 558.938146199891 |
215| Orlando | 464 | 2013-07 | 558.750952294576 |
216| Yorba Linda | 96 | 2013-09 | 557.48300297833 |
217| Orlando | 464 | 2010-06 | 554.161073825503 |
218| Orlando | 464 | 2011-01 | 554.015962270996 |
219| Houston | 1340 | 2017-03 | 550.990310370931 |
220| Houston | 1340 | 2014-09 | 549.663133989402 |
221| Yorba Linda | 96 | 2010-04 | 549.126322275855 |
222| Orlando | 464 | 2016-08 | 547.458008343914 |
223| Houston | 1340 | 2010-08 | 544.690991672975 |
224| Riverside | 954 | 2012-07 | 541.950910360385 |
225| Yorba Linda | 96 | 2016-10 | 541.854164527062 |
226| Orlando | 464 | 2012-12 | 540.91492835117 |
227| Yorba Linda | 96 | 2010-03 | 540.797370853446 |
228| Merritt Island | 1696 | 2012-10 | 539.918447928927 |
229| Orlando | 464 | 2012-10 | 539.864683475422 |
230| Orlando | 464 | 2018-07 | 539.839288953383 |
231| Yorba Linda | 96 | 2012-02 | 539.153948854883 |
232| Yorba Linda | 96 | 2010-10 | 537.778884666735 |
233| Riverside | 954 | 2018-10 | 537.490305601805 |
234| Yorba Linda | 96 | 2012-11 | 537.215364075177 |
235| Orlando | 464 | 2014-01 | 537.159078541629 |
236| Orlando | 464 | 2015-09 | 537.108652276438 |
237| Orlando | 464 | 2011-05 | 536.478868129875 |
238| Yorba Linda | 96 | 2016-08 | 536.309849029475 |
239| Yorba Linda | 96 | 2015-04 | 535.968162678443 |
240| Orlando | 464 | 2011-03 | 535.779793216035 |
241| Orlando | 464 | 2017-12 | 535.62270995828 |
242| Yorba Linda | 96 | 2014-12 | 535.217623497997 |
243| Yorba Linda | 96 | 2013-01 | 534.968676183629 |
244| Gregory | 488 | 2014-07 | 534.919913694631 |
245| Orlando | 464 | 2018-05 | 534.581897333575 |
246| Houston | 1340 | 2010-05 | 533.152006056018 |
247| Orlando | 464 | 2016-10 | 533.118447306367 |
248| Orlando | 464 | 2014-02 | 533.095592236532 |
249| Orlando | 464 | 2012-11 | 532.176673317613 |
250| Yorba Linda | 96 | 2013-05 | 531.213207353394 |
251| Orlando | 464 | 2011-04 | 530.844186468348 |
252| Yorba Linda | 96 | 2014-09 | 530.161959535791 |
253| Orlando | 464 | 2015-08 | 529.43805550517 |
254| Orlando | 464 | 2011-10 | 529.305278432795 |
255| Yorba Linda | 96 | 2011-01 | 527.80928417377 |
256| Orlando | 464 | 2016-07 | 527.265372755306 |
257| Orlando | 464 | 2013-06 | 527.023762017051 |
258| Yorba Linda | 96 | 2012-05 | 525.633459997946 |
259| Orlando | 464 | 2014-06 | 525.433339379648 |
260| Yorba Linda | 96 | 2015-03 | 525.325356886105 |
261| Orlando | 464 | 2014-12 | 525.134046798476 |
262| Orlando | 464 | 2013-03 | 525.057137674587 |
263| Orlando | 464 | 2018-09 | 524.896789406857 |
264| Orlando | 464 | 2013-01 | 524.276800290223 |
265| Gregory | 488 | 2016-07 | 523.484198502348 |
266| Houston | 1340 | 2013-10 | 522.040878122634 |
267| Yorba Linda | 96 | 2013-07 | 521.966416760809 |
268| Riverside | 954 | 2014-08 | 521.898490788979 |
269| Orlando | 464 | 2015-10 | 521.500090694722 |
270| Orlando | 464 | 2012-03 | 520.863776528206 |
271| Gregory | 488 | 2018-11 | 520.620129458053 |
272| Yorba Linda | 96 | 2011-03 | 520.157235288076 |
273| Yorba Linda | 96 | 2011-07 | 519.814008421485 |
274| Yorba Linda | 96 | 2010-05 | 519.526856321249 |
275| Houston | 1340 | 2011-10 | 519.263890991673 |
276| Orlando | 464 | 2012-01 | 519.181208053691 |
277| Yorba Linda | 96 | 2015-11 | 518.93468214029 |
278| Orlando | 464 | 2019-01 | 518.83910756394 |
279| Orlando | 464 | 2010-07 | 518.701614366044 |
280| Orlando | 464 | 2016-01 | 517.583529838563 |
281| Yorba Linda | 96 | 2014-01 | 517.495532504878 |
282| Bronx | 1595 | 2013-11 | 517.17946508987 |
283| Yorba Linda | 96 | 2018-03 | 516.025572558283 |
284| Orlando | 464 | 2011-08 | 516.022129512062 |
285| Orlando | 464 | 2015-01 | 515.696716851079 |
286| Bronx | 1595 | 2013-08 | 514.461526412656 |
287| Orlando | 464 | 2019-10 | 513.753673136223 |
288| Orlando | 464 | 2018-04 | 513.35824415019 |
289| Yorba Linda | 96 | 2014-03 | 511.697648146246 |
290| Yorba Linda | 96 | 2017-07 | 511.537126424977 |