-
Notifications
You must be signed in to change notification settings - Fork 7
/
sql-scratchpad.sql
65 lines (61 loc) · 2.24 KB
/
sql-scratchpad.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
-- select * from orders_v4_nfts as orders
-- left outer join order_status_v4_nfts order_status on orders.nonce = order_status.nonce
-- where orders.nonce is not null
-- AND orders.nonce = '0x0ec20fd860da445aabbeccb880c38d44'
SELECT erc20_token,
chain_id,
nonce,
app_id,
app_metadata,
date_created,
date_last_updated,
direction,
*
from (
select
-- Only one status update per nonce (there can be many status updates to a single order)
-- We're only interested in the latest status update (as we assume that's it's final resting state, e.g. filled or cancelled
DISTINCT ON (nonce) *
from (
orders_v4_nfts as orders
left outer join (
select block_number,
transaction_hash,
order_status,
nonce as order_nonce_from_update,
date_posted_to_db
from order_status_v4_nfts
order by nonce,
block_number desc,
date_posted_to_db desc
) as order_status on orders.nonce = order_status.order_nonce_from_update
)
) as orders_with_latest_status
where orders_with_latest_status.nonce is not null
order by orders_with_latest_status.date_posted_to_db desc;
-- AND orders.nonce = '0x0ec20fd860da445aabbeccb880c38d44'
-- 0x7845622560b14d79a7dc07c85bbdf086
drop
view if exists orders_with_latest_status;
create view orders_with_latest_status as
select
-- Only one status update per nonce, per maker (there can be many status updates to a single order)
-- We're only interested in the latest status update (as we assume that's it's final resting state, e.g. filled or cancelled
DISTINCT ON (nonce, maker) *
from
(
orders_v4_nfts as orders
left outer join (
select
block_number,
transaction_hash,
order_status,
nonce as order_nonce_from_update,
date_posted_to_db
from
order_status_v4_nfts
order by
block_number desc,
date_posted_to_db desc
) as order_status on orders.nonce = order_status.order_nonce_from_update
);