| 1 | CREATE OR REPLACE FUNCTION public.fn_get_customer_usage_between(
|
|---|
| 2 | p_customer_id bigint,
|
|---|
| 3 | p_start_date date,
|
|---|
| 4 | p_end_date date
|
|---|
| 5 | )
|
|---|
| 6 | RETURNS TABLE (
|
|---|
| 7 | subscription_id bigint,
|
|---|
| 8 | subscription_number text,
|
|---|
| 9 | call_minutes numeric,
|
|---|
| 10 | sms_count bigint,
|
|---|
| 11 | data_gb numeric,
|
|---|
| 12 | usage_charges numeric
|
|---|
| 13 | )
|
|---|
| 14 | LANGUAGE sql
|
|---|
| 15 | STABLE
|
|---|
| 16 | AS $$
|
|---|
| 17 | SELECT s.subscription_id,
|
|---|
| 18 | s.subscription_number,
|
|---|
| 19 | ROUND(SUM(u.total_call_seconds)::numeric / 60.0, 2),
|
|---|
| 20 | SUM(u.total_sms_count)::bigint,
|
|---|
| 21 | ROUND(SUM(u.total_data_mb) / 1024.0, 3),
|
|---|
| 22 | SUM(u.total_charge_amount)
|
|---|
| 23 | FROM public.accounts a
|
|---|
| 24 | JOIN public.subscriptions s ON s.account_id = a.account_id
|
|---|
| 25 | JOIN public.usage_aggregates_daily u ON u.subscription_id = s.subscription_id
|
|---|
| 26 | WHERE a.customer_id = p_customer_id
|
|---|
| 27 | AND u.usage_date BETWEEN p_start_date AND p_end_date
|
|---|
| 28 | GROUP BY s.subscription_id, s.subscription_number
|
|---|
| 29 | ORDER BY s.subscription_id;
|
|---|
| 30 | $$;
|
|---|
| 31 |
|
|---|