From ad2ad61d4ecd6958c9665d46aa39d1eead531d63 Mon Sep 17 00:00:00 2001 From: Zach Margolis Date: Wed, 24 Jul 2024 09:55:01 -0700 Subject: [PATCH] Add query for cumulative unique active idv accounts (LG-14000) (#589) - Recurring request, documenting for easier discovery instead of searching Slack --- _articles/queries.md | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/_articles/queries.md b/_articles/queries.md index 0e474f7b..4132d93f 100644 --- a/_articles/queries.md +++ b/_articles/queries.md @@ -92,6 +92,26 @@ date = Date.new(2021, 1, 1) Profile.where(active: true).where('activated_at < ?', date).count ``` +## Cumulative Unique IDV Accounts at Partner + +Returns the number of active, unique users at a partner (across all apps/integrations/service providers). + +```ruby +values = { + agency_id: agency.id, # UPDATE THIS +} +sql = format(<<~SQL, values) + SELECT COUNT(DISTINCT profiles.id) + FROM profiles + JOIN identities ON profiles.user_id = identities.user_id + JOIN service_providers ON identities.service_provider = service_providers.issuer + WHERE + service_providers.agency_id = %{agency_id} + AND profiles.active = TRUE +SQL +ActiveRecord::Base.connection.execute(sql).to_a +``` + ## Active Partners **Note**: The queries below can be run locally in the [`18F/identity-idp-config`](https://github.com/18F/identity-idp-config) project by running `bin/data_console` from within the root directory, instead of from a production instance. You will need to remove the `Agreements::` module from the Model name (i.e. start with just `PartnerAccount`) for queries run in the config repo.