-
Notifications
You must be signed in to change notification settings - Fork 0
/
queries_supplemental.sql
72 lines (58 loc) · 2.39 KB
/
queries_supplemental.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
-- Create a table for each view in queries.sql
--DROP TABLE cardholder_trans;
CREATE TABLE cardholder_trans AS
SELECT * FROM cardholder_transactions;
--DROP TABLE cardholder_tran;
CREATE TABLE cardholder_tran AS
SELECT * FROM cardholder_transaction;
--DROP TABLE smaller_than_2;
CREATE TABLE smaller_than_2 AS
SELECT * FROM less_than_2;
--DROP TABLE mini_transactions;
CREATE TABLE mini_transactions AS
SELECT * FROM small_transactions;
--DROP TABLE top_100_07to09;
CREATE TABLE top_100_07to09 AS
SELECT * FROM top_100_7to9;
--DROP TABLE transactions_07to09;
CREATE TABLE transactions_07to09 AS
SELECT * FROM transaction_time;
--DROP TABLE vulnerable_merchant;
CREATE TABLE vulnerable_merchant AS
SELECT * FROM vulnerable_merchants;
-- Since 12am to 5am is one of the most popular timeframe for fraudulent transactions,
-- Tables are created to fetch transaction data during the that time window.
-- One of the reasons is that people are not monitoring card activities when asleep.
--DROP TABLE suspicious_12to5am;
CREATE TABLE suspicious_12to5am AS
SELECT *
FROM cardholder_transactions
-- gets the hour of the day from the datetime
WHERE date::time between time '00:00:00' and '05:00:00';
--DROP TABLE suspicious_moonlight;
CREATE TABLE suspicious_moonlight AS
SELECT u.id, u.name, COUNT(u.id) AS "moonlight_transactions"
FROM suspicious_12to5am as u
GROUP BY u.id, u.name
ORDER BY "moonlight_transactions" DESC;
-- Create tables for activities of less than $2.00 in amount occuring from 12am to 5am
DROP TABLE suspicious_mini_12to5am;
CREATE TABLE suspicious_mini_12to5am AS
SELECT *
FROM suspicious_12to5am
WHERE amount <= 2.00;
--DROP TABLE suspicious_mini_transactions;
CREATE TABLE suspicious_mini_transactions AS
SELECT p.id, p.name, COUNT(p.id) AS "mini_transactions"
FROM suspicious_mini_12to5am as p
GROUP BY p.id, p.name
ORDER BY "mini_transactions" DESC;
--DROP TABLE vulnerable_merchants_moonlight;
CREATE TABLE vulnerable_merchants_moonlight AS
SELECT p.merchant, p.category, COUNT(p.merchant) AS "vulnerable_merchant_moonlight"
FROM suspicious_mini_12to5am as p
GROUP BY p.merchant, p.category
ORDER BY "vulnerable_merchant_moonlight" DESC;
-- Use export table feature to export tables created into csv file,
-- specifying path and checking header and delimitor ','. Alternatively, an example as the following:
-- COPY suspicious_moonlight TO '../Data/suspicious_moonlight.csv' WITH (FORMAT CSV, HEADER);