Untitled

mail@pastecode.io avatar
unknown
plain_text
24 days ago
433 B
1
Indexable
Never
SELECT 
  e.EmployeeID, 
  e.PostalCode, 
  AVG(num_customers) AS avg_customer_count
FROM 
  `project_id.dataset_name.employees` e
JOIN (
  SELECT 
    PostalCode, 
    COUNT(DISTINCT customer_id) AS num_customers
  FROM 
    `project_id.dataset_name.transactions`
  GROUP BY 
    PostalCode
) AS EmployeeCustomerCounts ON e.PostalCode = EmployeeCustomerCounts.PostalCode
GROUP BY 
  e.EmployeeID, 
  e.PostalCode;
Leave a Comment