50 lines
1.9 KiB
Plaintext
50 lines
1.9 KiB
Plaintext
SELECT
|
|
i.item_number AS "Item Number",
|
|
COALESCE(NULLIF(i.item_descrip1, ''), i.item_descrip2) AS "Description",
|
|
CASE i.item_type
|
|
WHEN 'P' THEN 'Purchased'
|
|
WHEN 'M' THEN 'Manufactured'
|
|
WHEN 'R' THEN 'Reference'
|
|
WHEN 'T' THEN 'Tooling'
|
|
WHEN 'O' THEN 'Outside Process'
|
|
WHEN 'B' THEN 'Breeder'
|
|
WHEN 'C' THEN 'Co-Product'
|
|
WHEN 'F' THEN 'Phantom'
|
|
ELSE i.item_type
|
|
END AS "Item Type",
|
|
clc.classcode_code AS "Class Code",
|
|
COALESCE(bom_count.num_boms, 0) AS "BOMs Used In",
|
|
COALESCE(qoh.qty_on_hand, 0) AS "QOH",
|
|
cc.comment_user AS "Created By",
|
|
COALESCE(i.item_created, cc.comment_date)::date AS "Date Created",
|
|
last_txn.last_transaction_date::date AS "Last Transaction",
|
|
(CURRENT_DATE - COALESCE(last_txn.last_transaction_date, i.item_created, cc.comment_date)::date) AS "Days Unused"
|
|
FROM item i
|
|
LEFT JOIN classcode clc ON clc.classcode_id = i.item_classcode_id
|
|
LEFT JOIN (
|
|
SELECT bomitem_item_id, COUNT(DISTINCT bomitem_parent_item_id) AS num_boms
|
|
FROM bomitem
|
|
GROUP BY bomitem_item_id
|
|
) bom_count ON bom_count.bomitem_item_id = i.item_id
|
|
LEFT JOIN (
|
|
SELECT itemsite_item_id, SUM(itemsite_qtyonhand) AS qty_on_hand
|
|
FROM itemsite
|
|
GROUP BY itemsite_item_id
|
|
) qoh ON qoh.itemsite_item_id = i.item_id
|
|
LEFT JOIN comment cc
|
|
ON cc.comment_source_id = i.item_id
|
|
AND cc.comment_source = 'I'
|
|
AND cc.comment_text = 'Created'
|
|
LEFT JOIN (
|
|
SELECT
|
|
isite.itemsite_item_id,
|
|
MAX(ih.invhist_transdate) AS last_transaction_date
|
|
FROM itemsite isite
|
|
JOIN invhist ih ON ih.invhist_itemsite_id = isite.itemsite_id
|
|
GROUP BY isite.itemsite_item_id
|
|
) last_txn ON last_txn.itemsite_item_id = i.item_id
|
|
WHERE i.item_active
|
|
AND (CURRENT_DATE - COALESCE(last_txn.last_transaction_date, i.item_created, cc.comment_date)::date)
|
|
>= '{{UnusedDays.text}}'::int
|
|
ORDER BY "Days Unused" DESC
|