{"database": "fishbowl_archive", "table": "dataexport", "rows": [[19, "SELECT\r\n  p.num AS `Part Number`,\r\n  p.description AS `Part Description`,\r\n  pt.name AS `Inventory Type`,\r\n\r\n  /* Last PO Purchase Price:\r\n     1) last RECEIVED PO unit cost (preferred)\r\n     2) else last PO-issued/created line unit cost (fallback)\r\n     If neither exists => 'NoPOHist'\r\n  */\r\n  IFNULL(\r\n    CAST(\r\n      COALESCE(\r\n        (\r\n          SELECT poi.unitCost\r\n          FROM receiptitem ri\r\n          JOIN receipt r\r\n            ON r.id = ri.receiptId\r\n          JOIN poitem poi\r\n            ON poi.id = ri.poItemId\r\n          WHERE\r\n            ri.partId = p.id\r\n            AND r.poId IS NOT NULL\r\n            AND ri.dateReceived IS NOT NULL\r\n            AND poi.unitCost IS NOT NULL\r\n          ORDER BY ri.dateReceived DESC, ri.id DESC\r\n          LIMIT 1\r\n        ),\r\n        (\r\n          SELECT poi.unitCost\r\n          FROM poitem poi\r\n          JOIN po\r\n            ON po.id = poi.poId\r\n          WHERE\r\n            poi.partId = p.id\r\n            AND poi.unitCost IS NOT NULL\r\n          ORDER BY COALESCE(po.dateIssued, po.dateCreated) DESC, poi.id DESC\r\n          LIMIT 1\r\n        )\r\n      ) AS CHAR\r\n    ),\r\n    'NoPOHist'\r\n  ) AS `Last PO Purchase Price`,\r\n\r\n  /* Last Purchase Price Date:\r\n     1) receiptitem.dateReceived for last received PO\r\n     2) else PO issue/create date for last PO line\r\n     If neither exists => 'NoPOHist'\r\n  */\r\n  IFNULL(\r\n    CAST(\r\n      DATE(\r\n        COALESCE(\r\n          (\r\n            SELECT ri.dateReceived\r\n            FROM receiptitem ri\r\n            JOIN receipt r\r\n              ON r.id = ri.receiptId\r\n            JOIN poitem poi\r\n              ON poi.id = ri.poItemId\r\n            WHERE\r\n              ri.partId = p.id\r\n              AND r.poId IS NOT NULL\r\n              AND ri.dateReceived IS NOT NULL\r\n              AND poi.unitCost IS NOT NULL\r\n            ORDER BY ri.dateReceived DESC, ri.id DESC\r\n            LIMIT 1\r\n          ),\r\n          (\r\n            SELECT COALESCE(po.dateIssued, po.dateCreated)\r\n            FROM poitem poi\r\n            JOIN po\r\n              ON po.id = poi.poId\r\n            WHERE\r\n              poi.partId = p.id\r\n              AND poi.unitCost IS NOT NULL\r\n              AND COALESCE(po.dateIssued, po.dateCreated) IS NOT NULL\r\n            ORDER BY COALESCE(po.dateIssued, po.dateCreated) DESC, poi.id DESC\r\n            LIMIT 1\r\n          )\r\n        )\r\n      ) AS CHAR\r\n    ),\r\n    'NoPOHist'\r\n  ) AS `Last Purchase Price Date`\r\n\r\nFROM part p\r\nLEFT JOIN parttype pt\r\n  ON pt.id = p.typeId\r\n\r\nORDER BY p.num", "2026-02-18 10:12:09.009000", "2026-02-18 10:20:10.099000", "Last PO Purchase Price (most recent received PO unit cost; if none, falls back to most recent PO line unit cost). If no PO history exists at all, it returns NoPOHist", 17, "Last PO Purch Price Query"]], "columns": ["id", "dataQuery", "dateCreated", "dateLastModified", "description", "lastChangedUserId", "name"], "primary_keys": ["id"], "primary_key_values": ["19"], "units": {}, "query_ms": 1.3070349814370275}