SELECT
  distinct ts.taxonomy_species_id,
  ts.nomen_number,
  ts.taxonomy_genus_id AS extended_genus_name,
  ts.species_name,
  ts.species_authority,
  ts.is_specific_hybrid,
  ts.subspecies_name,
  ts.subspecies_authority,
  ts.is_subspecific_hybrid,
  ts.variety_name,
  ts.variety_authority,
  ts.is_varietal_hybrid,
  ts.subvariety_name,
  ts.subvariety_authority,
  ts.is_subvarietal_hybrid,
  ts.forma_rank_type,
  ts.is_forma_hybrid,
  ts.forma_name,
  ts.forma_authority,
  ts.name_verified_date,
  ts.verifier_cooperator_id,
  ts.synonym_code,
  ts.current_taxonomy_species_id,
  ts.protologue,
  ts.protologue_virtual_path,
  ts.note,
  ts.hybrid_parentage,
  ts.name,
  ts.name_authority,
  ts.alternate_name,
  (select tg.genus_name from taxonomy_genus tg where tg.taxonomy_genus_id = ts.taxonomy_genus_id) AS genus_name,
  (select count(*) from accession a2 where a2.taxonomy_species_id = ts.taxonomy_species_id) AS accession_count,
  ts.priority1_site_id,
  ts.priority2_site_id,
  ts.curator1_cooperator_id,
  ts.curator2_cooperator_id,
  ts.restriction_code,
  ts.life_form_code,
  (select min(name) from taxonomy_common_name tc where ts.taxonomy_species_id = tc.taxonomy_species_id) AS species_common_name,
  ts.created_date,
  ts.created_by,
  ts.modified_date,
  ts.modified_by,
  ts.owned_date,
  ts.owned_by
FROM
    taxonomy_species ts
    INNER JOIN (
        SELECT taxonomy_species_id FROM taxonomy_species WHERE taxonomy_species_id IN (:taxonomyspeciesid)

        UNION SELECT taxonomy_species_id FROM accession WHERE accession_id in (:accessionid)

        UNION SELECT taxonomy_species_id FROM accession
            INNER JOIN inventory ON accession.accession_id = inventory.accession_id
            WHERE inventory_id in (:inventoryid)

        UNION SELECT taxonomy_species_id FROM taxonomy_species WHERE taxonomy_genus_id in (:taxonomygenusid)

        UNION SELECT taxonomy_species_id FROM taxonomy_species
            WHERE curator1_cooperator_id in (:cooperatorid)
               OR curator2_cooperator_id in (:cooperatorid)
               OR verifier_cooperator_id in (:cooperatorid)

        UNION SELECT taxonomy_species_id FROM taxonomy_geography_map WHERE geography_id in (:geographyid)

        UNION SELECT taxonomy_species_id FROM taxonomy_crop_map WHERE crop_id in (:cropid)

    ) list ON list.taxonomy_species_id = ts.taxonomy_species_id

