Show the top 10 companies and their most common...
Created on May 6, 2025

Prompt

Show the top 10 companies and their most common routes of administration

Chart

Query Results

Query Results

SQL Query:

WITH TopApplicants AS (
    -- Step 1: Find the top 10 applicants by total product count
    SELECT
        applicant_full_name,
        COUNT(*) AS total_product_count
    FROM orange_book_products
    WHERE applicant_full_name IS NOT NULL
    GROUP BY 1
    ORDER BY total_product_count DESC
    LIMIT 10
),
ApplicantRouteCounts AS (
    -- Step 2: For the top applicants, count the occurrences of each route
    SELECT
        ta.applicant_full_name,
        p.route,
        COUNT(p.id) AS route_product_count
    FROM orange_book_products p
    JOIN TopApplicants ta ON p.applicant_full_name = ta.applicant_full_name
    WHERE p.route IS NOT NULL
    GROUP BY 1, 2
),
RankedRoutes AS (
    -- Step 3: Rank the routes for each applicant based on their count
    SELECT
        applicant_full_name,
        route,
        route_product_count,
        -- Assign a rank to each route within an applicant, 1 being the most common
        ROW_NUMBER() OVER (PARTITION BY applicant_full_name ORDER BY route_product_count DESC, route) as rn
    FROM ApplicantRouteCounts
)
-- Step 4: Select the most common route (rank 1) for each top applicant
-- and include their total product count for ordering
SELECT
    rr.applicant_full_name AS applicant_name,
    ta.total_product_count,
    rr.route AS most_common_route,
    rr.route_product_count AS count_of_most_common_route
FROM RankedRoutes rr
JOIN TopApplicants ta ON rr.applicant_full_name = ta.applicant_full_name
WHERE rr.rn = 1
ORDER BY ta.total_product_count DESC -- Order the final result by the applicant's total product count
LIMIT 20;

Results:

applicant_nametotal_product_countmost_common_routecount_of_most_common_route
WATSON LABORATORIES INC1221TABLET;ORAL749
AUROBINDO PHARMA LTD967TABLET;ORAL555
CHARTWELL RX SCIENCES LLC794TABLET;ORAL528
HOSPIRA INC790INJECTABLE;INJECTION666
HIKMA PHARMACEUTICALS USA INC760INJECTABLE;INJECTION326
SANDOZ INC742TABLET;ORAL297
TEVA PHARMACEUTICALS USA INC714TABLET;ORAL408
ZYDUS PHARMACEUTICALS USA INC692TABLET;ORAL357
SUN PHARMACEUTICAL INDUSTRIES LTD646TABLET;ORAL266
APOTEX INC569TABLET;ORAL279
Chart Configuration
{
  "grid": {
    "left": "3%",
    "right": "4%",
    "bottom": "3%",
    "containLabel": true
  },
  "title": {
    "text": "Total Product Count by Applicant"
  },
  "xAxis": {
    "data": [
      "WATSON LABORATORIES INC",
      "AUROBINDO PHARMA LTD",
      "CHARTWELL RX SCIENCES LLC",
      "HOSPIRA INC",
      "HIKMA PHARMACEUTICALS USA INC",
      "SANDOZ INC",
      "TEVA PHARMACEUTICALS USA INC",
      "ZYDUS PHARMACEUTICALS USA INC",
      "SUN PHARMACEUTICAL INDUSTRIES LTD",
      "APOTEX INC"
    ],
    "type": "category",
    "axisLabel": {
      "rotate": 45,
      "interval": 0
    }
  },
  "yAxis": {
    "type": "value"
  },
  "legend": {
    "data": [
      "Total Product Count"
    ]
  },
  "series": [
    {
      "data": [
        1221,
        967,
        794,
        790,
        760,
        742,
        714,
        692,
        646,
        569
      ],
      "name": "Total Product Count",
      "type": "bar"
    }
  ],
  "tooltip": {
    "trigger": "axis",
    "axisPointer": {
      "type": "shadow"
    }
  }
}