Skip to content
Advertisement

MS Access Transform Query to MySQL Query

I am moving a MS Access database to MySQL and I am running into a problem with a few of the queries.

I am about 80% finished with transferring the queries, but this particular one is giving me trouble.

TRANSFORM Nz(Max(IIf([charge_description_code] In ('AHS','AHW','AHL','RES','SAT'),"Y","N")), "N") AS Data
SELECT shipment_details.tracking_number, shipment_details.invoice_number
FROM shipment_details
WHERE tracking_number is not null
and charge_category_detail_code not in ('RADJ')
GROUP BY shipment_details.tracking_number, shipment_details.invoice_number
PIVOT shipment_details.charge_description_code In (SAT,AHW,AHS,AHL,RES);

I expect the results to be displayed like:

tracking_number invoice_number  SAT AHW AHS AHL RES
1Z2XXXXXXXXX625816  1329102975  N   N   N   N   N
1Z3YYYYYYYYY610514  1329109647  N   N   Y   N   N
1Z3ZZZZZZZZZ142605  1329109647  N   N   N   Y   Y

Advertisement

Answer

Consider conditional aggregation which would still work in MS Access (replace IF with IIF below). Usually, Access’ crosstab query is for pivoted columns more than a handful unlike your current version of five. Also, your NZ is redundant as NULL resolves to N per the conditional logic.

SELECT s.tracking_number, 
       s.invoice_number,
       MAX(IF(s.[charge_description_code] = 'SAT', 'Y', 'N')) AS 'SAT',
       MAX(IF(s.[charge_description_code] = 'AHW', 'Y', 'N')) AS 'AHW',
       MAX(IF(s.[charge_description_code] = 'AHL', 'Y', 'N')) AS 'AHL',
       MAX(IF(s.[charge_description_code] = 'AHS', 'Y', 'N')) AS 'AHS',
       MAX(IF(s.[charge_description_code] = 'RES', 'Y', 'N')) AS 'SAT'

FROM shipment_details s
WHERE s.tracking_number IS NOT NULL
  AND s.charge_category_detail_code NOT IN ('RADJ')
  AND s.charge_description_code IN ('AHS','AHW','AHL','RES','SAT')

GROUP BY s.tracking_number, 
         s.invoice_number
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement