diff options
Diffstat (limited to 'res/sql/views/orcl_accounts.sql')
-rw-r--r-- | res/sql/views/orcl_accounts.sql | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/res/sql/views/orcl_accounts.sql b/res/sql/views/orcl_accounts.sql new file mode 100644 index 0000000..400d38f --- /dev/null +++ b/res/sql/views/orcl_accounts.sql | |||
@@ -0,0 +1,31 @@ | |||
1 | CREATE OR REPLACE VIEW accounts AS | ||
2 | |||
3 | SELECT | ||
4 | users.uuid AS user_uuid, | ||
5 | COALESCE(transactions.balance, 0) AS balance, | ||
6 | COALESCE(sales.open_bids, 0) AS open_bids, | ||
7 | COALESCE(transactions.balance, 0) - COALESCE(sales.open_bids, 0) AS equity | ||
8 | |||
9 | FROM users | ||
10 | |||
11 | LEFT OUTER JOIN ( | ||
12 | SELECT | ||
13 | user_uuid, | ||
14 | SUM(transactions.amount) AS balance | ||
15 | |||
16 | FROM transactions | ||
17 | WHERE transaction_date < current_datetime() | ||
18 | GROUP BY user_uuid | ||
19 | ) transactions | ||
20 | ON transactions.user_uuid = users.uuid | ||
21 | |||
22 | LEFT OUTER JOIN ( | ||
23 | SELECT | ||
24 | best_bidder_uuid, | ||
25 | SUM(sales.best_offer + sales.charges) AS open_bids | ||
26 | |||
27 | FROM sales | ||
28 | WHERE end_date > current_datetime() | ||
29 | GROUP BY best_bidder_uuid | ||
30 | ) sales | ||
31 | ON sales.best_bidder_uuid = users.uuid; | ||