| 1 | WITH treg AS (SELECT rtf.family_name, rtg.genus_name, rts.species_name, rts.subspecies_name, rts.variety_name, rts.subvariety_name, rts.forma_name
|
|---|
| 2 | , tr.regulation_type_code, g.country_code, g.adm1, cvl.title
|
|---|
| 3 | FROM taxonomy_regulation_map trm
|
|---|
| 4 | LEFT JOIN taxonomy_regulation tr ON tr.taxonomy_regulation_id = trm.taxonomy_regulation_id
|
|---|
| 5 | LEFT JOIN geography g ON g.geography_id = tr.geography_id
|
|---|
| 6 | LEFT JOIN code_value cv ON g.country_code = cv.value AND cv.group_name = 'GEOGRAPHY_COUNTRY_CODE'
|
|---|
| 7 | LEFT JOIN code_value_lang cvl ON cv.code_value_id = cvl.code_value_id and cvl.sys_lang_id = 1
|
|---|
| 8 | LEFT JOIN taxonomy_species ts ON ts.taxonomy_species_id = trm.taxonomy_species_id
|
|---|
| 9 | LEFT JOIN taxonomy_species rts ON rts.taxonomy_species_id = ts.current_taxonomy_species_id
|
|---|
| 10 | LEFT join taxonomy_genus tg ON tg.taxonomy_genus_id = trm.taxonomy_genus_id
|
|---|
| 11 | LEFT JOIN taxonomy_genus rtg ON rtg.taxonomy_genus_id = COALESCE(rts.taxonomy_genus_id, tg.current_taxonomy_genus_id)
|
|---|
| 12 | LEFT JOIN taxonomy_family rtf ON rtf.taxonomy_family_id = COALESCE(rtg.taxonomy_family_id, trm.taxonomy_family_id)
|
|---|
| 13 | WHERE COALESCE(trm.is_exempt, 'N') != 'Y'
|
|---|
| 14 | )
|
|---|
| 15 | SELECT
|
|---|
| 16 | oi.order_request_item_id
|
|---|
| 17 | ,oi.order_request_id
|
|---|
| 18 | ,oi.sequence_number
|
|---|
| 19 | ,Row_number() Over (PARTITION BY oi.order_request_id ORDER BY oi.order_request_id, (select name from taxonomy_species ts WHERE ts.taxonomy_species_id = a.taxonomy_species_id), inventory_number_part1, inventory_number_part2, inventory_number_part3) AS office_use
|
|---|
| 20 | ,(select min(plant_name) from accession_inv_name an, inventory i where
|
|---|
| 21 | i.inventory_id = an.inventory_id and i.accession_id = a.accession_id and
|
|---|
| 22 | plant_name_rank = (select MIN(plant_name_rank) from accession_inv_name an2, inventory i2 where an2.inventory_id = i2.inventory_id and i2.accession_id = a.accession_id)) AS plant_name
|
|---|
| 23 | ,i.accession_id
|
|---|
| 24 | ,i.inventory_number_part1
|
|---|
| 25 | ,i.inventory_number_part2
|
|---|
| 26 | ,i.inventory_number_part3
|
|---|
| 27 | ,o.requestor_cooperator_id
|
|---|
| 28 | ,o.final_recipient_cooperator_id
|
|---|
| 29 | ,o.ship_to_cooperator_id
|
|---|
| 30 | ,imp.distribution_default_quantity
|
|---|
| 31 | ,i.inventory_maint_policy_id
|
|---|
| 32 | ,oi.inventory_id
|
|---|
| 33 | ,(SELECT c.site_id FROM cooperator c WHERE c.cooperator_id = i.owned_by) AS site_id
|
|---|
| 34 | ,(SELECT s.site_long_name FROM site s WHERE s.site_id = (SELECT c.site_id FROM cooperator c WHERE c.cooperator_id = o.owned_by)) AS site_long_name
|
|---|
| 35 | ,oi.name
|
|---|
| 36 | ,oi.external_taxonomy
|
|---|
| 37 | ,(SELECT taxonomy_species_id FROM accession a WHERE i.accession_id = a.accession_id) AS taxonomy_species_id
|
|---|
| 38 | ,(SELECT s.geography_id FROM accession_source s WHERE i.accession_id = s.accession_id AND accession_source_id = (SELECT MIN(accession_source_id) FROM accession_source s2 WHERE i.accession_id = s2.accession_id AND is_origin = 'Y')) AS geography_id
|
|---|
| 39 | ,i.storage_location_part1
|
|---|
| 40 | ,i.storage_location_part2
|
|---|
| 41 | ,i.storage_location_part3
|
|---|
| 42 | ,i.storage_location_part4
|
|---|
| 43 | ,(SELECT COALESCE(i.storage_location_part1, '') + '_' + COALESCE(i.storage_location_part2, '') + '_' + COALESCE(i.storage_location_part3, '') + '_' + COALESCE(i.storage_location_part4, '')) AS storage_location
|
|---|
| 44 | ,i.quantity_on_hand
|
|---|
| 45 | ,i.quantity_on_hand_unit_code
|
|---|
| 46 | ,i.availability_status_code
|
|---|
| 47 | ,oi.quantity_shipped
|
|---|
| 48 | ,oi.quantity_shipped_unit_code
|
|---|
| 49 | ,oi.distribution_form_code
|
|---|
| 50 | ,oi.status_code
|
|---|
| 51 | ,oi.status_date
|
|---|
| 52 | ,oi.note
|
|---|
| 53 | ,oi.source_cooperator_id
|
|---|
| 54 | ,i.hundred_seed_weight
|
|---|
| 55 | ,(SELECT i.hundred_seed_weight * oi.quantity_shipped / 100) AS order_weight
|
|---|
| 56 | ,(SELECT TOP 1 percent_viable FROM inventory_viability iv WHERE iv.inventory_id = i.inventory_id ORDER BY iv.tested_date DESC, inventory_viability_id DESC) AS percent_viable
|
|---|
| 57 | ,(SELECT TOP 1 percent_normal FROM inventory_viability iv WHERE iv.inventory_id = i.inventory_id ORDER BY iv.tested_date DESC, inventory_viability_id DESC) AS percent_normal
|
|---|
| 58 | ,(SELECT TOP 1 percent_dormant FROM inventory_viability iv WHERE iv.inventory_id = i.inventory_id ORDER BY iv.tested_date DESC, inventory_viability_id DESC) AS percent_dormant
|
|---|
| 59 | ,(SELECT TOP 1 tested_date FROM inventory_viability iv WHERE iv.inventory_id = i.inventory_id ORDER BY iv.tested_date DESC) AS tested_date
|
|---|
| 60 | ,(SELECT TOP 1 inventory_viability_rule_id FROM inventory_viability iv WHERE iv.inventory_id = i.inventory_id ORDER BY iv.tested_date DESC, inventory_viability_id DESC) AS inventory_viability_rule_id
|
|---|
| 61 | ,(SELECT SUBSTRING((SELECT ', ' + ipr1.type_code AS [text()] FROM accession_ipr ipr1 WHERE ipr1.accession_id = i.accession_id AND ipr1.expired_date IS NULL ORDER BY ipr1.accession_id FOR XML PATH ('')), 2, 1000)) AS ipr_restriction
|
|---|
| 62 | ,(SELECT DISTINCT 'xPVP' FROM accession_ipr ipr2 WHERE ipr2.accession_id = i.accession_id AND ipr2.type_code = 'PVP' AND ipr2.expired_date IS NOT NULL) AS xpvp_warning
|
|---|
| 63 | ,(SELECT SUBSTRING((SELECT ', ' + iq.quarantine_type_code + ' = ' + iq.progress_status_code AS [text()] FROM accession_quarantine iq WHERE iq.accession_id = i.accession_id AND iq.released_date IS NULL ORDER BY iq.accession_id FOR XML PATH ('')), 2, 1000)) AS quarantine_restriction
|
|---|
| 64 | ,(SELECT STRING_AGG(regtext, ', ') FROM
|
|---|
| 65 | (SELECT DISTINCT regulation_type_code + ' in ' + COALESCE(treg.adm1, '') + ', ' + treg.country_code AS regtext
|
|---|
| 66 | FROM treg
|
|---|
| 67 | WHERE regulation_type_code IN ('SEED', 'TERRESTRIAL', 'AQUATIC', 'PARASITIC')
|
|---|
| 68 | AND treg.family_name = tf.family_name
|
|---|
| 69 | AND COALESCE(treg.genus_name, tg.genus_name) = tg.genus_name
|
|---|
| 70 | AND COALESCE(treg.species_name, ts.species_name) = ts.species_name
|
|---|
| 71 | AND COALESCE(treg.subspecies_name, ts.subspecies_name, '') = COALESCE(ts.subspecies_name, '')
|
|---|
| 72 | AND COALESCE(treg.variety_name, ts.variety_name, '')= COALESCE(ts.variety_name, '')
|
|---|
| 73 | AND COALESCE(treg.subvariety_name, ts.subvariety_name, '')= COALESCE(ts.subvariety_name, '')
|
|---|
| 74 | AND COALESCE(treg.forma_name, ts.forma_name, '')= COALESCE(ts.forma_name, '')
|
|---|
| 75 | AND treg.country_code = g3.country_code AND COALESCE(treg.adm1, g3.adm1, '') = COALESCE(g3.adm1,'')
|
|---|
| 76 | ) disttreg
|
|---|
| 77 | ) AS noxious_restriction
|
|---|
| 78 | ,(SELECT taxonomy_genus_id FROM taxonomy_species ts LEFT JOIN accession a ON ts.taxonomy_species_id = a.taxonomy_species_id WHERE i.accession_id = a.accession_id) AS taxonomy_genus_id
|
|---|
| 79 | ,oi.web_order_request_item_id
|
|---|
| 80 | ,(SELECT a.accession_number_part1 FROM accession a WHERE i.accession_id = a.accession_id) AS accession_number_part1
|
|---|
| 81 | ,(SELECT a.accession_number_part2 FROM accession a WHERE i.accession_id = a.accession_id) AS accession_number_part2
|
|---|
| 82 | ,(SELECT a.accession_number_part3 FROM accession a WHERE i.accession_id = a.accession_id) AS accession_number_part3
|
|---|
| 83 | FROM
|
|---|
| 84 | order_request_item oi
|
|---|
| 85 | INNER JOIN inventory i ON oi.inventory_id = i.inventory_id
|
|---|
| 86 | INNER JOIN inventory_maint_policy imp ON i.inventory_maint_policy_id = imp.inventory_maint_policy_id
|
|---|
| 87 | INNER JOIN accession a ON a.accession_id = i.accession_id
|
|---|
| 88 | INNER JOIN taxonomy_species ts ON ts.taxonomy_species_id = a.taxonomy_species_id
|
|---|
| 89 | INNER JOIN taxonomy_genus tg ON tg.taxonomy_genus_id = ts.taxonomy_genus_id
|
|---|
| 90 | INNER JOIN taxonomy_family tf ON tf.taxonomy_family_id = tg.taxonomy_family_id
|
|---|
| 91 | INNER JOIN order_request o ON o.order_request_id = oi.order_request_id
|
|---|
| 92 | LEFT JOIN cooperator c3 ON o.final_recipient_cooperator_id = c3.cooperator_id
|
|---|
| 93 | LEFT JOIN geography g3 ON c3.geography_id = g3.geography_id
|
|---|
| 94 | WHERE oi.order_request_id IN (:orderrequestid)
|
|---|
| 95 | AND oi.quantity_shipped > 0
|
|---|
| 96 | ORDER BY oi.sequence_number |
|---|