~singpolyma/jmp-schemas

ref: 583a6f7074d52cd90aef975fc28b33d831f4c279 jmp-schemas/deploy/cdr_with_charge.sql -rw-r--r-- 500 bytes
583a6f70Stephen Paul Weber Table for call_rates and view to augment CDRs with that data 2 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
-- Deploy jmp:cdr_with_charge to pg
-- requires: cdr
-- requires: call_rates
-- requires: customer_plans

BEGIN;

CREATE VIEW cdr_with_charge AS
	SELECT DISTINCT ON (cdr_id)
		cdr.*,
		rate,
		rate * ceiling(billsec / 60.0) AS charge
	FROM
		cdr
		INNER JOIN customer_plans USING (customer_id)
		LEFT JOIN call_rates ON
			cdr.direction = call_rates.direction AND
			customer_plans.plan_name = call_rates.plan_name AND
			cdr.tel LIKE call_rates.prefix || '%'
	ORDER BY cdr_id, prefix DESC;

COMMIT;