| 1 | I’ve attached the updated packing report dataview if you want to use it as a starting point for updating the OW dataview. Here is the relevant SQL for just the WITH and sub-select. The WITH CTE pulls all the tax and geo names for the regulations and the sub-select carefully compares them to the order geography and item taxonomy. I switch from using XML PATH to the STRING_AGG function, but had to use a sub-sub-select to get DISTINCT geo values for the list. (g3 is the final recipient geopgraphy.)
|
|---|
| 2 |
|
|---|
| 3 | 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
|
|---|
| 4 | , tr.regulation_type_code, g.country_code, g.adm1, cvl.title
|
|---|
| 5 | FROM taxonomy_regulation_map trm
|
|---|
| 6 | LEFT JOIN taxonomy_regulation tr ON tr.taxonomy_regulation_id = trm.taxonomy_regulation_id
|
|---|
| 7 | LEFT JOIN geography g ON g.geography_id = tr.geography_id
|
|---|
| 8 | LEFT JOIN code_value cv ON g.country_code = cv.value AND cv.group_name = 'GEOGRAPHY_COUNTRY_CODE'
|
|---|
| 9 | LEFT JOIN code_value_lang cvl ON cv.code_value_id = cvl.code_value_id and cvl.sys_lang_id = 1
|
|---|
| 10 | LEFT JOIN taxonomy_species ts ON ts.taxonomy_species_id = trm.taxonomy_species_id
|
|---|
| 11 | LEFT JOIN taxonomy_species rts ON rts.taxonomy_species_id = ts.current_taxonomy_species_id
|
|---|
| 12 | LEFT join taxonomy_genus tg ON tg.taxonomy_genus_id = trm.taxonomy_genus_id
|
|---|
| 13 | LEFT JOIN taxonomy_genus rtg ON rtg.taxonomy_genus_id = COALESCE(rts.taxonomy_genus_id, tg.current_taxonomy_genus_id)
|
|---|
| 14 | LEFT JOIN taxonomy_family rtf ON rtf.taxonomy_family_id = COALESCE(rtg.taxonomy_family_id, trm.taxonomy_family_id)
|
|---|
| 15 | )
|
|---|
| 16 |
|
|---|
| 17 | …
|
|---|
| 18 |
|
|---|
| 19 | (SELECT STRING_AGG(regtext, '$ ') FROM
|
|---|
| 20 | (SELECT DISTINCT COALESCE(adm1, '') AS regtext
|
|---|
| 21 | FROM treg
|
|---|
| 22 | WHERE regulation_type_code IN ('SEED', 'TERRESTRIAL', 'AQUATIC', 'PARASITIC')
|
|---|
| 23 | AND treg.family_name = tf.family_name
|
|---|
| 24 | AND COALESCE(treg.genus_name, tg.genus_name) = tg.genus_name
|
|---|
| 25 | AND COALESCE(treg.species_name, ts.species_name) = ts.species_name
|
|---|
| 26 | AND COALESCE(treg.subspecies_name, ts.subspecies_name, '') = COALESCE(ts.subspecies_name, '')
|
|---|
| 27 | AND COALESCE(treg.variety_name, ts.variety_name, '')= COALESCE(ts.variety_name, '')
|
|---|
| 28 | AND COALESCE(treg.subvariety_name, ts.subvariety_name, '')= COALESCE(ts.subvariety_name, '')
|
|---|
| 29 | AND COALESCE(treg.forma_name, ts.forma_name, '')= COALESCE(ts.forma_name, '')
|
|---|
| 30 | AND treg.country_code = g3.country_code AND COALESCE(treg.adm1, g3.adm1, '') = COALESCE(g3.adm1,'')
|
|---|
| 31 | ) disttreg
|
|---|
| 32 | ) AS noxious_restriction
|
|---|
| 33 |
|
|---|
| 34 |
|
|---|
| 35 | Kurt
|
|---|