| 1 | DROP VIEW IF EXISTS public.v_customer_daily_usage_summary;
|
|---|
| 2 |
|
|---|
| 3 | CREATE VIEW public.v_customer_daily_usage_summary AS
|
|---|
| 4 | SELECT
|
|---|
| 5 | c.customer_id,
|
|---|
| 6 | CASE WHEN c.customer_type = 'business' THEN c.company_name ELSE c.first_name || ' ' || c.last_name END AS customer_name,
|
|---|
| 7 | a.account_number,
|
|---|
| 8 | s.subscription_id,
|
|---|
| 9 | s.subscription_number,
|
|---|
| 10 | p.plan_name,
|
|---|
| 11 | uad.usage_date,
|
|---|
| 12 | ROUND(uad.total_call_seconds::numeric / 60.0, 2) AS total_call_minutes,
|
|---|
| 13 | uad.total_sms_count,
|
|---|
| 14 | ROUND(uad.total_data_mb / 1024.0, 4) AS total_data_gb,
|
|---|
| 15 | uad.total_charge_amount,
|
|---|
| 16 | SUM(uad.total_data_mb) OVER (
|
|---|
| 17 | PARTITION BY s.subscription_id, date_trunc('month', uad.usage_date::timestamp)
|
|---|
| 18 | ORDER BY uad.usage_date
|
|---|
| 19 | ) AS month_to_date_data_mb
|
|---|
| 20 | FROM public.customers c
|
|---|
| 21 | JOIN public.accounts a ON a.customer_id = c.customer_id
|
|---|
| 22 | JOIN public.subscriptions s ON s.account_id = a.account_id
|
|---|
| 23 | JOIN public.plans p ON p.plan_id = s.plan_id
|
|---|
| 24 | JOIN public.usage_aggregates_daily uad ON uad.subscription_id = s.subscription_id;
|
|---|
| 25 |
|
|---|