--get a list of the most recent activations that exist for a given application.
create temp table nsde_activations as
select
    application_number_or_citation,
    count(distinct package_ndc) as package_count,
    max(marketing_start_date) as most_recent_start,
    max(marketing_end_date) as most_recent_end,
    max(inactivation_date) as most_recent_inactivation,
    max(reactivation_date) as most_recent_reactivation
from spl.nsde
group by application_number_or_citation
;

select count(*) from nsde_activations
where most_recent_end is null
;
/*
count
-----
 6602
*/


select count(*) from nsde_activations
where most_recent_end is NOT null
;
/*
count
-----
 10983
*/

/*
So, the current number of marketed compounds is how many NDA or ANDA (ANADA?) compounds there are.

*/

-- get count of drugs that you can select by first 3 letters
select
    left(application_number_or_citation, 3) as first_3,
    count(*) as row_count
from nsde_activations
group by first_3
;



select
    left(application_number_or_citation, 3) as first_3,
    count(*) as row_count
from nsde_activations
where first_3 in ()
group by first_3
;
