aboutsummaryrefslogtreecommitdiff
path: root/res/sql/views/orcl_sales.sql
diff options
context:
space:
mode:
Diffstat (limited to 'res/sql/views/orcl_sales.sql')
-rw-r--r--res/sql/views/orcl_sales.sql49
1 files changed, 49 insertions, 0 deletions
diff --git a/res/sql/views/orcl_sales.sql b/res/sql/views/orcl_sales.sql
new file mode 100644
index 0000000..851d17e
--- /dev/null
+++ b/res/sql/views/orcl_sales.sql
@@ -0,0 +1,49 @@
1CREATE OR REPLACE VIEW sales AS
2
3 SELECT
4 items.uuid AS item_uuid,
5 items.user_uuid AS seller_uuid,
6 items.start_date AS start_date,
7 items.end_date AS end_date,
8 best_bids.uuid AS best_bid_uuid,
9 best_bids.user_uuid AS best_bidder_uuid,
10 best_bids.offer AS best_offer,
11 COALESCE(charges.fee, 0) + COALESCE(charges.rate, 0) * best_bids.offer AS charges
12
13 FROM items
14
15 LEFT OUTER JOIN (
16 SELECT
17 DISTINCT
18 item_uuid,
19 first_value(offer)
20 OVER (
21 PARTITION BY item_uuid
22 ORDER BY offer DESC
23 ) offer,
24 first_value(uuid)
25 OVER (
26 PARTITION BY item_uuid
27 ORDER BY offer DESC
28 ) uuid,
29 first_value(user_uuid)
30 OVER (
31 PARTITION BY item_uuid
32 ORDER BY offer DESC
33 ) user_uuid
34
35 FROM bids
36 ) best_bids
37 ON best_bids.item_uuid = items.uuid
38
39 LEFT OUTER JOIN (
40 SELECT
41 start_date,
42 end_date,
43 SUM(fee) AS fee,
44 SUM(rate) AS rate
45
46 FROM charges
47 GROUP BY charges.start_date, charges.end_date
48 ) charges
49 ON items.end_date BETWEEN charges.start_date AND charges.end_date;