-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrypto-analytics.sql
82 lines (68 loc) · 1.58 KB
/
crypto-analytics.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
72
73
74
75
76
77
78
79
80
81
82
-- Number of buy and sell transactions for Bitcoin
SELECT
txn_type,
COUNT(txn_type) AS transaction_count
FROM
raw.transactions
WHERE
ticker = 'BTC'
GROUP BY
txn_type;
/* For each year, calculate the following buy and sell metrics for Bitcoin:
a. Total transaction count
b. Total quantity
c. Average quantity */
SELECT
EXTRACT(YEAR FROM txn_date::DATE) AS txn_year,
txn_type,
COUNT(txn_id) AS txn_count,
SUM(quantity) AS total_quantity,
AVG(quantity) AS average_quantity
FROM
raw.transactions
WHERE
ticker = 'BTC'
GROUP BY
txn_year, txn_type
ORDER BY
txn_year, txn_type;
-- Monthly total quantity purchased and sold for Ethereum in 2020
SELECT
EXTRACT(MONTH FROM txn_date::DATE) AS calendar_month,
SUM(quantity) FILTER (WHERE txn_type = 'BUY') AS buy_quantity,
SUM(quantity) FILTER (WHERE txn_type = 'SELL') AS sell_quantity
FROM
raw.transactions
WHERE
ticker = 'ETH'
AND txn_date BETWEEN '2020-01-01' AND '2020-12-31'
GROUP BY
calendar_month
ORDER BY
calendar_month;
-- Who are the top 3 members with the most bitcoin quantity?
with buy as(
select
member_id,
sum(quantity) as bought_btc
from raw.transactions
where txn_type = 'BUY' and ticker = 'BTC'
group by member_id
)
, sell as (
select
member_id
,sum(quantity) as sold_btc
from raw.transactions
where txn_type = 'SELL' and ticker = 'BTC'
group by member_id
)
select
members.first_name
,buy.bought_btc - sell.sold_btc as total_quantity
from buy
full join sell using (member_id)
join raw.members
using (member_id)
order by total_quantity desc
limit 3;