-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathvendor stats.sql
117 lines (107 loc) · 3 KB
/
vendor stats.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
WITH order_dates AS (
SELECT
DISTINCT o.id,
p.invoice_date_gmt::DATE AS ship_date,
p.paid_date_gmt::DATE AS paid_date,
TO_DATE(SUBSTRING(sent.content, '\d{2}\-\d{2}\-\d{4}'),'MM-DD-YYYY') AS sent_date
FROM
sierra_view.order_record o
JOIN
sierra_view.order_record_paid p
ON
o.id = p.order_record_id
JOIN
sierra_view.subfield sent
ON
o.id = sent.record_id AND sent.field_type_code = 'b' AND sent.tag = 'b' AND TO_DATE(SUBSTRING(sent.content, '\d{2}\-\d{2}\-\d{4}'),'MM-DD-YYYY') IS NOT NULL
JOIN
sierra_view.record_metadata rm
ON
o.id = rm.id AND rm.creation_date_gmt::DATE >= {{created_date}}
WHERE o.accounting_unit_code_num = {{accounting_unit}}
)
SELECT
*,
'' AS "VENDOR STATISTICS",
'' AS "https://sic.minlib.net/reports/58"
FROM(
SELECT
o.vendor_record_code AS vendor_code,
n.field_content AS vendor_name,
SUM(cmf.copies) AS copies_ordered,
COALESCE(SUM(p.copies),0) AS copies_received,
COALESCE(SUM(cmf.copies) FILTER(WHERE o.order_status_code = 'z'),'0') - COALESCE(SUM(p.copies) FILTER(WHERE o.order_status_code = 'z'),0) AS copies_cancelled,
COALESCE(SUM(p.paid_amount),'0')::MONEY AS total_paid,
COALESCE(SUM(p.paid_amount)/SUM(p.copies),'0')::MONEY AS price_per_copy,
COALESCE(ROUND(AVG(ship_date - sent_date))::VARCHAR,'') AS avg_days_to_ship,
COALESCE(ROUND(AVG(paid_date - sent_date))::VARCHAR,'') AS avg_days_to_invoice
FROM
sierra_view.order_record o
JOIN
sierra_view.vendor_record v
ON
o.vendor_record_code = v.code AND o.accounting_unit_code_num = v.accounting_unit_code_num
JOIN
sierra_view.varfield n
ON
v.id = n.record_id AND n.varfield_type_code = 't'
JOIN
sierra_view.order_record_cmf cmf
ON
o.id = cmf.order_record_id AND cmf.display_order = '0'
JOIN
sierra_view.record_metadata rm
ON
o.id = rm.id AND rm.creation_date_gmt::DATE >= {{created_date}}
LEFT JOIN
sierra_view.order_record_paid p
ON
o.id = p.order_record_id
LEFT JOIN order_dates od
ON
o.id = od.id
WHERE o.accounting_unit_code_num = {{accounting_unit}}
GROUP BY 1,2
UNION
SELECT
'TOTAL' AS vendor_code,
'' AS vendor_name,
SUM(cmf.copies) AS copies_ordered,
SUM(p.copies) AS copies_received,
SUM(cmf.copies) FILTER(WHERE o.order_status_code = 'z') - COALESCE(SUM(p.copies) FILTER(WHERE o.order_status_code = 'z'),0) AS copies_cancelled,
SUM(p.paid_amount)::MONEY AS total_paid,
(SUM(p.paid_amount)/SUM(p.copies))::MONEY AS price_per_copy,
'' AS avg_days_to_ship,
'' AS avg_days_to_invoice
FROM
sierra_view.order_record o
JOIN
sierra_view.vendor_record v
ON
o.vendor_record_code = v.code AND o.accounting_unit_code_num = v.accounting_unit_code_num
JOIN
sierra_view.varfield n
ON
v.id = n.record_id AND n.varfield_type_code = 't'
JOIN
sierra_view.order_record_cmf cmf
ON
o.id = cmf.order_record_id AND cmf.display_order = '0'
JOIN
sierra_view.record_metadata rm
ON
o.id = rm.id AND rm.creation_date_gmt::DATE >= {{created_date}}
LEFT JOIN
sierra_view.order_record_paid p
ON
o.id = p.order_record_id
LEFT JOIN order_dates od
ON
o.id = od.id
WHERE o.accounting_unit_code_num = {{accounting_unit}}
GROUP BY 1,2)a
ORDER BY CASE
WHEN vendor_code = 'TOTAL' THEN 2
ELSE 1
END,
vendor_code