Opened 5 years ago

Closed 5 years ago

#2321 closed enhancement (fixed)

Updates to the SQL for order_wizard_get_packet_label

Reported by: Pete Cyr Owned by: Gary Kinard
Priority: medium Milestone:
Component: Dataviews (CT) Version: N/A
Severity: Keywords:
Cc: lisa.burke@…, david.peters@…, stacey.estrada@… Database Engine: N/A
Operating System: N/A

Description

In support of the AdCom barcodes/labels subcommittee please implement these three updates to the SQL for order_wizard_get_packet_label. Below are suggestions for SQL that worked in prototype development (all line numbers are in reference to the SQL Server SQL code for order_wizard_get_packet_label currently in production on npgsWeb server):

1) To support proper collating of order items per order request line 5 should be changed from:

,Row_number() Over (ORDER BY (select name from taxonomy_species ts WHERE ts.taxonomy_species_id = a.taxonomy_species_id), inventory_number_part1, inventory_number_part2) AS office_use

to:

,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

2) To support order items with quantity_shipped less than 1 change line 62 from:

AND oi.quantity_shipped >= 1

to:

AND oi.quantity_shipped > 0

3) To support use of a 'universal' packet label that uses the site long name in the packet label header (instead of a 'hard coded' site name) insert this line below line 19:

,(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

Attachments (1)

order_wizard_get_packet_label.sql​ (7.2 KB ) - added by Pete Cyr 5 years ago.

Download all attachments as: .zip

Change History (9)

comment:1 by Kurt Endress, 5 years ago

Owner: changed from Kurt Endress to Pete Cyr
Status: new → assigned

comment:2 by Pete Cyr, 5 years ago

Owner: changed from Pete Cyr to Gary Kinard

Gary would you please handle this. Kurt still insists that he is not responsible for the dataviews used by the CT wizards. Thanks!!!

comment:3 by Gary Kinard, 5 years ago

I will schedule a Zoom meeting for early next week that includes Kurt, Pete, Gary, and David to discuss this.

comment:4 by Pete Cyr, 5 years ago

Cc: stacey.estrada@… added

comment:5 by Pete Cyr, 5 years ago

This is the updated SQL that has been tested on npgsTest1.agron.iastate.edu in Ames. It is confirmed to fix this Trac ticket:

/*
SQL for order_wizard_get_packet_label...
*/

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

, tr.regulation_type_code, g.country_code, g.adm1, cvl.title

FROM taxonomy_regulation_map trm
LEFT JOIN taxonomy_regulation tr ON tr.taxonomy_regulation_id = trm.taxonomy_regulation_id
LEFT JOIN geography g ON g.geography_id = tr.geography_id
LEFT JOIN code_value cv ON g.country_code = cv.value AND cv.group_name = 'GEOGRAPHY_COUNTRY_CODE'
LEFT JOIN code_value_lang cvl ON cv.code_value_id = cvl.code_value_id and cvl.sys_lang_id = 1
LEFT JOIN taxonomy_species ts ON ts.taxonomy_species_id = trm.taxonomy_species_id
LEFT JOIN taxonomy_species rts ON rts.taxonomy_species_id = ts.current_taxonomy_species_id
LEFT join taxonomy_genus tg ON tg.taxonomy_genus_id = trm.taxonomy_genus_id
LEFT JOIN taxonomy_genus rtg ON rtg.taxonomy_genus_id = COALESCE(rts.taxonomy_genus_id, tg.current_taxonomy_genus_id)
LEFT JOIN taxonomy_family rtf ON rtf.taxonomy_family_id = COALESCE(rtg.taxonomy_family_id, trm.taxonomy_family_id)
WHERE COALESCE(trm.is_exempt, 'N') != 'Y'

)
SELECT

oi.order_request_item_id
,oi.order_request_id
,oi.sequence_number
,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
,(select min(plant_name) from accession_inv_name an, inventory i where

i.inventory_id = an.inventory_id and i.accession_id = a.accession_id and
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

,i.accession_id
,i.inventory_number_part1
,i.inventory_number_part2
,i.inventory_number_part3
,o.requestor_cooperator_id
,o.final_recipient_cooperator_id
,o.ship_to_cooperator_id
,imp.distribution_default_quantity
,i.inventory_maint_policy_id
,oi.inventory_id
,(SELECT c.site_id FROM cooperator c WHERE c.cooperator_id = i.owned_by) AS site_id
,(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
,oi.name
,oi.external_taxonomy
,(SELECT taxonomy_species_id FROM accession a WHERE i.accession_id = a.accession_id) AS taxonomy_species_id
,(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
,i.storage_location_part1
,i.storage_location_part2
,i.storage_location_part3
,i.storage_location_part4
,(SELECT COALESCE(i.storage_location_part1, ) + '_' + COALESCE(i.storage_location_part2, ) + '_' + COALESCE(i.storage_location_part3, ) + '_' + COALESCE(i.storage_location_part4, )) AS storage_location
,i.quantity_on_hand
,i.quantity_on_hand_unit_code
,i.availability_status_code
,oi.quantity_shipped
,oi.quantity_shipped_unit_code
,oi.distribution_form_code
,oi.status_code
,oi.status_date
,oi.note
,oi.source_cooperator_id
,i.hundred_seed_weight
,(SELECT i.hundred_seed_weight * oi.quantity_shipped / 100) AS order_weight
,(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
,(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
,(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
,(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
,(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
,(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
,(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
,(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
,(SELECT STRING_AGG(regtext, ', ') FROM

(SELECT DISTINCT regulation_type_code + ' in ' + COALESCE(treg.adm1, ) + ', ' + treg.country_code AS regtext

FROM treg
WHERE regulation_type_code IN ('SEED', 'TERRESTRIAL', 'AQUATIC', 'PARASITIC')

AND treg.family_name = tf.family_name
AND COALESCE(treg.genus_name, tg.genus_name) = tg.genus_name
AND COALESCE(treg.species_name, ts.species_name) = ts.species_name
AND COALESCE(treg.subspecies_name, ts.subspecies_name, ) = COALESCE(ts.subspecies_name, )
AND COALESCE(treg.variety_name, ts.variety_name, )= COALESCE(ts.variety_name, )
AND COALESCE(treg.subvariety_name, ts.subvariety_name, )= COALESCE(ts.subvariety_name, )
AND COALESCE(treg.forma_name, ts.forma_name, )= COALESCE(ts.forma_name, )
AND treg.country_code = g3.country_code AND COALESCE(treg.adm1, g3.adm1, ) = COALESCE(g3.adm1,)

) disttreg

) AS noxious_restriction

,(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
,oi.web_order_request_item_id
,(SELECT a.accession_number_part1 FROM accession a WHERE i.accession_id = a.accession_id) AS accession_number_part1
,(SELECT a.accession_number_part2 FROM accession a WHERE i.accession_id = a.accession_id) AS accession_number_part2
,(SELECT a.accession_number_part3 FROM accession a WHERE i.accession_id = a.accession_id) AS accession_number_part3

FROM

order_request_item oi
INNER JOIN inventory i ON oi.inventory_id = i.inventory_id
INNER JOIN inventory_maint_policy imp ON i.inventory_maint_policy_id = imp.inventory_maint_policy_id
INNER JOIN accession a ON a.accession_id = i.accession_id
INNER JOIN taxonomy_species ts ON ts.taxonomy_species_id = a.taxonomy_species_id
INNER JOIN taxonomy_genus tg ON tg.taxonomy_genus_id = ts.taxonomy_genus_id
INNER JOIN taxonomy_family tf ON tf.taxonomy_family_id = tg.taxonomy_family_id
INNER JOIN order_request o ON o.order_request_id = oi.order_request_id
LEFT JOIN cooperator c3 ON o.final_recipient_cooperator_id = c3.cooperator_id
LEFT JOIN geography g3 ON c3.geography_id = g3.geography_id

WHERE oi.order_request_id IN (:orderrequestid)
AND oi.quantity_shipped > 0
ORDER BY oi.sequence_number

comment:6 by Pete Cyr, 5 years ago

NOTE: The Trac system inadvertently removed the empty single quotes (like ) from the SQL above so do not use the SQL in the comment above. Instead use the SQL in the attached files (named order_wizard_get_packet_label.sql).

by Pete Cyr, 5 years ago

comment:7 by Kurt Endress, 5 years ago

Dataview order_wizard_get_packet_label_20210831.dataviewxml from Pete's attached SQL loaded on npgsDev for testing.

comment:8 by Lisa Burke, 5 years ago

Resolution: → fixed
Status: assigned → closed
Note: See TracTickets for help on using tickets.