#+property: header-args:sqlite :dir ../res/ :db db.sqlite :colnames yes #+begin_src elisp :results none (org-babel-do-load-languages 'org-babel-load-languages (quote ((emacs-lisp . t) (sqlite . t)))) #+end_src #+begin_src sqlite :results none CREATE TABLE IF NOT EXISTS cards( id INTEGER, client_id INTEGER, card_brand TEXT, card_type TEXT, card_number TEXT, expires TEXT, cvv TEXT, has_chip INTEGER, num_cards_issued INTEGER, credit_limit INTEGER, acct_open_date TEXT, year_pin_last_changed INTEGER, card_on_dark_web INTEGER ); CREATE INDEX IF NOT EXISTS idx_cards_credit ON cards(credit_limit); CREATE INDEX IF NOT EXISTS idx_cards_id_client ON cards(id, client_id); CREATE TABLE IF NOT EXISTS users( id INTEGER, current_age INTEGER, retirement_age INTEGER, birth_year INTEGER, birth_month INTEGER, gender TEXT, address TEXT, latitude REAL, longitude REAL, per_capita_income INTEGER, yearly_income INTEGER, total_debt INTEGER, credit_score INTEGER, num_credit_cards INTEGER ); CREATE INDEX IF NOT EXISTS idx_users_id ON users(id); CREATE TABLE IF NOT EXISTS transactions( id INTEGER, date TEXT, client_id INTEGER, card_id INTEGER, amount INTEGER, use_chip TEXT, merchant_id INTEGER, merchant_city TEXT, merchant_state TEXT, zip REAL, mcc INTEGER, errors TEXT ); CREATE INDEX IF NOT EXISTS idx_transactions_card ON transactions(card_id); CREATE INDEX IF NOT EXISTS idx_transactions_client_city_month ON transactions(client_id, merchant_city, date); #+end_src #+begin_src sqlite :results none .mode csv .import --skip 1 cards_data.csv cards UPDATE cards SET credit_limit = CAST(SUBSTR(credit_limit, 2) AS INTEGER); .import --skip 1 users_data.csv users UPDATE users SET per_capita_income = CAST(SUBSTR(per_capita_income, 2) AS INTEGER); .import --skip 1 transactions_data.csv transactions UPDATE transactions SET amount = CAST(SUBSTR(amount, 2, LENGTH(amount) - 4) || SUBSTR(amount, -2) AS INTEGER) #+end_src #+begin_src sqlite SELECT * FROM cards LIMIT 10; #+end_src #+RESULTS: | 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 | |------+-----------+------------+-----------------+------------------+---------+-----+----------+------------------+--------------+----------------+-----------------------+------------------| | 4524 | 825 | Visa | Debit | 4344676511950444 | 12/2022 | 623 | YES | 2 | 24295 | 09/2002 | 2008 | No | | 2731 | 825 | Visa | Debit | 4956965974959986 | 12/2020 | 393 | YES | 2 | 21968 | 04/2014 | 2014 | No | | 3701 | 825 | Visa | Debit | 4582313478255491 | 02/2024 | 719 | YES | 2 | 46414 | 07/2003 | 2004 | No | | 42 | 825 | Visa | Credit | 4879494103069057 | 08/2024 | 693 | NO | 1 | 12400 | 01/2003 | 2012 | No | | 4659 | 825 | Mastercard | Debit (Prepaid) | 5722874738736011 | 03/2009 | 75 | YES | 1 | 28 | 09/2008 | 2009 | No | | 4537 | 1746 | Visa | Credit | 4404898874682993 | 09/2003 | 736 | YES | 1 | 27500 | 09/2003 | 2012 | No | | 1278 | 1746 | Visa | Debit | 4001482973848631 | 07/2022 | 972 | YES | 2 | 28508 | 02/2011 | 2011 | No | | 3687 | 1746 | Mastercard | Debit | 5627220683410948 | 06/2022 | 48 | YES | 2 | 9022 | 07/2003 | 2015 | No | | 3465 | 1746 | Mastercard | Debit (Prepaid) | 5711382187309326 | 11/2020 | 722 | YES | 2 | 54 | 06/2010 | 2015 | No | | 3754 | 1746 | Mastercard | Debit (Prepaid) | 5766121508358701 | 02/2023 | 908 | YES | 1 | 99 | 07/2006 | 2012 | No | #+begin_src sqlite SELECT * FROM users LIMIT 10; #+end_src #+RESULTS: | 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 | |------+-------------+----------------+------------+-------------+--------+--------------------------+----------+-----------+-------------------+---------------+------------+--------------+------------------| | 825 | 53 | 66 | 1966 | 11 | Female | 462 Rose Lane | 34.15 | -117.76 | 29278 | $59696 | $127613 | 787 | 5 | | 1746 | 53 | 68 | 1966 | 12 | Female | 3606 Federal Boulevard | 40.76 | -73.74 | 37891 | $77254 | $191349 | 701 | 5 | | 1718 | 81 | 67 | 1938 | 11 | Female | 766 Third Drive | 34.02 | -117.89 | 22681 | $33483 | $196 | 698 | 5 | | 708 | 63 | 63 | 1957 | 1 | Female | 3 Madison Street | 40.71 | -73.99 | 163145 | $249925 | $202328 | 722 | 4 | | 1164 | 43 | 70 | 1976 | 9 | Male | 9620 Valley Stream Drive | 37.76 | -122.44 | 53797 | $109687 | $183855 | 675 | 1 | | 68 | 42 | 70 | 1977 | 10 | Male | 58 Birch Lane | 41.55 | -90.6 | 20599 | $41997 | $0 | 704 | 3 | | 1075 | 36 | 67 | 1983 | 12 | Female | 5695 Fifth Street | 38.22 | -85.74 | 25258 | $51500 | $102286 | 672 | 3 | | 1711 | 26 | 67 | 1993 | 12 | Male | 1941 Ninth Street | 45.51 | -122.64 | 26790 | $54623 | $114711 | 728 | 1 | | 1116 | 81 | 66 | 1938 | 7 | Female | 11 Spruce Avenue | 40.32 | -75.32 | 26273 | $42509 | $2895 | 755 | 5 | | 1752 | 34 | 60 | 1986 | 1 | Female | 887 Grant Street | 29.97 | -92.12 | 18730 | $38190 | $81262 | 810 | 1 | #+begin_src sqlite SELECT * FROM transactions LIMIT 10; #+end_src #+RESULTS: | id | date | client_id | card_id | amount | use_chip | merchant_id | merchant_city | merchant_state | zip | mcc | errors | |---------+---------------------+-----------+---------+---------+--------------------+-------------+---------------+----------------+---------+------+--------| | 7475327 | 2010-01-01 00:01:00 | 1556 | 2972 | -7700.0 | Swipe Transaction | 59935 | Beulah | ND | 58523.0 | 5499 | | | 7475328 | 2010-01-01 00:02:00 | 561 | 4575 | 1457.0 | Swipe Transaction | 67570 | Bettendorf | IA | 52722.0 | 5311 | | | 7475329 | 2010-01-01 00:02:00 | 1129 | 102 | 8000.0 | Swipe Transaction | 27092 | Vista | CA | 92084.0 | 4829 | | | 7475331 | 2010-01-01 00:05:00 | 430 | 2860 | 20000.0 | Swipe Transaction | 27092 | Crown Point | IN | 46307.0 | 4829 | | | 7475332 | 2010-01-01 00:06:00 | 848 | 3915 | 4641.0 | Swipe Transaction | 13051 | Harwood | MD | 20776.0 | 5813 | | | 7475333 | 2010-01-01 00:07:00 | 1807 | 165 | 481.0 | Swipe Transaction | 20519 | Bronx | NY | 10464.0 | 5942 | | | 7475334 | 2010-01-01 00:09:00 | 1556 | 2972 | 7700.0 | Swipe Transaction | 59935 | Beulah | ND | 58523.0 | 5499 | | | 7475335 | 2010-01-01 00:14:00 | 1684 | 2140 | 2646.0 | Online Transaction | 39021 | ONLINE | | | 4784 | | | 7475336 | 2010-01-01 00:21:00 | 335 | 5131 | 26158.0 | Online Transaction | 50292 | ONLINE | | | 7801 | | | 7475337 | 2010-01-01 00:21:00 | 351 | 1112 | 1074.0 | Swipe Transaction | 3864 | Flushing | NY | 11355.0 | 5813 | | #+begin_src sqlite PRAGMA journal_mode = OFF; PRAGMA synchronous = OFF; PRAGMA temp_store = MEMORY; WITH cards_of_interest AS ( SELECT id as card_id FROM cards WHERE credit_limit >= 12000 ), transactions_of_interest AS ( SELECT t.merchant_city, t.client_id, strftime('%Y-%m', t.date) as month, t.amount FROM transactions t JOIN cards_of_interest c ON t.card_id = c.card_id ), monthly AS ( SELECT t.merchant_city, t.client_id, t.month, SUM(t.amount) AS amount FROM transactions_of_interest t GROUP BY t.merchant_city, t.client_id, t.month ), normalized AS ( SELECT m.merchant_city, m.client_id, m.month, m.amount / (u.per_capita_income / 12.0) AS amount FROM monthly m JOIN users u ON m.client_id = u.id ) SELECT * FROM normalized ORDER BY normalized.amount DESC LIMIT 100; #+end_src #+RESULTS: | journal_mode | | | | |----------------+-----------+---------+------------------| | delete | | | | | merchant_city | client_id | month | amount | | Houston | 1340 | 2010-10 | 679.564723694171 | | Houston | 1340 | 2010-06 | 676.406207418622 | | Houston | 1340 | 2010-07 | 666.335200605602 | | Houston | 1340 | 2011-06 | 660.198485995458 | | Yorba Linda | 96 | 2011-06 | 652.132895142241 | | Houston | 1340 | 2010-09 | 649.042392127176 | | Houston | 1340 | 2010-04 | 640.598788796366 | | Houston | 1340 | 2011-11 | 631.538834216503 | | Houston | 1340 | 2011-02 | 609.198940196821 | | Yorba Linda | 96 | 2015-10 | 604.364588682346 | | Houston | 1340 | 2011-09 | 597.758516275549 | | Yorba Linda | 96 | 2011-10 | 595.716134332957 | | Yorba Linda | 96 | 2011-09 | 593.845332237856 | | Houston | 1340 | 2010-11 | 589.237244511734 | | Houston | 1340 | 2010-01 | 582.088115064345 | | Orlando | 464 | 2010-12 | 579.945220388173 | | ONLINE | 1198 | 2015-07 | 578.782136231116 | | Yorba Linda | 96 | 2013-08 | 567.000616206224 | | Yorba Linda | 96 | 2011-08 | 565.96261682243 | | Houston | 1340 | 2011-08 | 565.072369417108 | | Yorba Linda | 96 | 2012-10 | 562.521413166273 | | Yorba Linda | 96 | 2014-07 | 561.647324637979 | | Orlando | 464 | 2017-01 | 561.633956103755 | | Orlando | 464 | 2016-03 | 558.938146199891 | | Orlando | 464 | 2013-07 | 558.750952294576 | | Yorba Linda | 96 | 2013-09 | 557.48300297833 | | Orlando | 464 | 2010-06 | 554.161073825503 | | Orlando | 464 | 2011-01 | 554.015962270996 | | Houston | 1340 | 2017-03 | 550.990310370931 | | Houston | 1340 | 2014-09 | 549.663133989402 | | Yorba Linda | 96 | 2010-04 | 549.126322275855 | | Orlando | 464 | 2016-08 | 547.458008343914 | | Houston | 1340 | 2010-08 | 544.690991672975 | | Riverside | 954 | 2012-07 | 541.950910360385 | | Yorba Linda | 96 | 2016-10 | 541.854164527062 | | Orlando | 464 | 2012-12 | 540.91492835117 | | Yorba Linda | 96 | 2010-03 | 540.797370853446 | | Merritt Island | 1696 | 2012-10 | 539.918447928927 | | Orlando | 464 | 2012-10 | 539.864683475422 | | Orlando | 464 | 2018-07 | 539.839288953383 | | Yorba Linda | 96 | 2012-02 | 539.153948854883 | | Yorba Linda | 96 | 2010-10 | 537.778884666735 | | Riverside | 954 | 2018-10 | 537.490305601805 | | Yorba Linda | 96 | 2012-11 | 537.215364075177 | | Orlando | 464 | 2014-01 | 537.159078541629 | | Orlando | 464 | 2015-09 | 537.108652276438 | | Orlando | 464 | 2011-05 | 536.478868129875 | | Yorba Linda | 96 | 2016-08 | 536.309849029475 | | Yorba Linda | 96 | 2015-04 | 535.968162678443 | | Orlando | 464 | 2011-03 | 535.779793216035 | | Orlando | 464 | 2017-12 | 535.62270995828 | | Yorba Linda | 96 | 2014-12 | 535.217623497997 | | Yorba Linda | 96 | 2013-01 | 534.968676183629 | | Gregory | 488 | 2014-07 | 534.919913694631 | | Orlando | 464 | 2018-05 | 534.581897333575 | | Houston | 1340 | 2010-05 | 533.152006056018 | | Orlando | 464 | 2016-10 | 533.118447306367 | | Orlando | 464 | 2014-02 | 533.095592236532 | | Orlando | 464 | 2012-11 | 532.176673317613 | | Yorba Linda | 96 | 2013-05 | 531.213207353394 | | Orlando | 464 | 2011-04 | 530.844186468348 | | Yorba Linda | 96 | 2014-09 | 530.161959535791 | | Orlando | 464 | 2015-08 | 529.43805550517 | | Orlando | 464 | 2011-10 | 529.305278432795 | | Yorba Linda | 96 | 2011-01 | 527.80928417377 | | Orlando | 464 | 2016-07 | 527.265372755306 | | Orlando | 464 | 2013-06 | 527.023762017051 | | Yorba Linda | 96 | 2012-05 | 525.633459997946 | | Orlando | 464 | 2014-06 | 525.433339379648 | | Yorba Linda | 96 | 2015-03 | 525.325356886105 | | Orlando | 464 | 2014-12 | 525.134046798476 | | Orlando | 464 | 2013-03 | 525.057137674587 | | Orlando | 464 | 2018-09 | 524.896789406857 | | Orlando | 464 | 2013-01 | 524.276800290223 | | Gregory | 488 | 2016-07 | 523.484198502348 | | Houston | 1340 | 2013-10 | 522.040878122634 | | Yorba Linda | 96 | 2013-07 | 521.966416760809 | | Riverside | 954 | 2014-08 | 521.898490788979 | | Orlando | 464 | 2015-10 | 521.500090694722 | | Orlando | 464 | 2012-03 | 520.863776528206 | | Gregory | 488 | 2018-11 | 520.620129458053 | | Yorba Linda | 96 | 2011-03 | 520.157235288076 | | Yorba Linda | 96 | 2011-07 | 519.814008421485 | | Yorba Linda | 96 | 2010-05 | 519.526856321249 | | Houston | 1340 | 2011-10 | 519.263890991673 | | Orlando | 464 | 2012-01 | 519.181208053691 | | Yorba Linda | 96 | 2015-11 | 518.93468214029 | | Orlando | 464 | 2019-01 | 518.83910756394 | | Orlando | 464 | 2010-07 | 518.701614366044 | | Orlando | 464 | 2016-01 | 517.583529838563 | | Yorba Linda | 96 | 2014-01 | 517.495532504878 | | Bronx | 1595 | 2013-11 | 517.17946508987 | | Yorba Linda | 96 | 2018-03 | 516.025572558283 | | Orlando | 464 | 2011-08 | 516.022129512062 | | Orlando | 464 | 2015-01 | 515.696716851079 | | Bronx | 1595 | 2013-08 | 514.461526412656 | | Orlando | 464 | 2019-10 | 513.753673136223 | | Orlando | 464 | 2018-04 | 513.35824415019 | | Yorba Linda | 96 | 2014-03 | 511.697648146246 | | Yorba Linda | 96 | 2017-07 | 511.537126424977 |