@@ -187,39 +187,33 @@ export async function GET(req: NextRequest) {
187187 WHERE ${ whereClause } ` ,
188188 params
189189 ) ,
190- // Body counts by distinct proper_title
190+ // Body counts (from latest_versions view - one per series)
191191 query < { body : string ; count : number } > (
192- `SELECT un_body as body, COUNT(DISTINCT proper_title )::int as count
193- FROM ${ DB_SCHEMA } .reports
194- WHERE un_body IS NOT NULL AND proper_title IS NOT NULL
192+ `SELECT un_body as body, COUNT(* )::int as count
193+ FROM ${ DB_SCHEMA } .latest_versions
194+ WHERE un_body IS NOT NULL
195195 GROUP BY un_body ORDER BY count DESC`
196196 ) ,
197- // Year range (min/max )
197+ // Year range (from latest_versions view )
198198 query < { min_year : number ; max_year : number } > (
199- `SELECT
200- MIN(COALESCE(date_year, CASE WHEN publication_date ~ '^\\d{4}' THEN SUBSTRING(publication_date FROM 1 FOR 4)::int END))::int as min_year,
201- MAX(COALESCE(date_year, CASE WHEN publication_date ~ '^\\d{4}' THEN SUBSTRING(publication_date FROM 1 FOR 4)::int END))::int as max_year
202- FROM ${ DB_SCHEMA } .reports
203- WHERE proper_title IS NOT NULL`
199+ `SELECT MIN(effective_year)::int as min_year, MAX(effective_year)::int as max_year
200+ FROM ${ DB_SCHEMA } .latest_versions`
204201 ) ,
205- // Get subject term counts - count by unique report title (not by version/symbol)
206- // Only include subjects that appear in more than one report (excluding credentials)
202+ // Subject term counts (from latest_versions - one per series, excluding credentials)
207203 query < SubjectCount > (
208- `SELECT subject as subject, COUNT(DISTINCT proper_title)::int as count
209- FROM ${ DB_SCHEMA } .reports, unnest(subject_terms) as subject
210- WHERE proper_title IS NOT NULL
211- AND subject != 'Representative''s credentials'
204+ `SELECT subject, COUNT(*)::int as count
205+ FROM ${ DB_SCHEMA } .latest_versions, unnest(subject_terms) as subject
206+ WHERE subject != 'Representative''s credentials'
212207 GROUP BY subject
213- HAVING COUNT(DISTINCT proper_title ) > 1
208+ HAVING COUNT(* ) > 1
214209 ORDER BY count DESC, subject`
215210 ) ,
216- // Entity counts by distinct proper_title
211+ // Entity counts (from latest_versions view)
217212 query < { entity : string ; count : number } > (
218- `SELECT re.entity, COUNT(DISTINCT r.proper_title)::int as count
219- FROM ${ DB_SCHEMA } .reporting_entities re
220- JOIN ${ DB_SCHEMA } .reports r ON re.symbol = r.symbol
221- WHERE re.entity IS NOT NULL AND r.proper_title IS NOT NULL
222- GROUP BY re.entity ORDER BY count DESC`
213+ `SELECT entity, COUNT(*)::int as count
214+ FROM ${ DB_SCHEMA } .latest_versions
215+ WHERE entity IS NOT NULL
216+ GROUP BY entity ORDER BY count DESC`
223217 ) ,
224218 ] ) ;
225219
0 commit comments