SELECT id, product_code, uom, procurement_type, proc_cd, type, proc_seq, l_part_no, l_part_cd, level, no_off, remarks, datetime FROM bom_detail WHERE 1
this is my bom detail table
SELECT id, prod_code, part_code, alt_part_cd, alt_no_off, alt_price, alt_flag FROM sm_alt_bom WHERE 1
Sample ALT BOM
SELECT id, po_no, prod_cd, input_part_cd, alt_part_cd, qty, prep_by, datetime FROM po_item_alt WHERE 1
po_item_alt
I want to make the change the query given Bellow
SELECT final_part_code from (
SELECT
bd.product_code,
bd.type,
bd.l_part_no,
COALESCE(
(
SELECT a.alt_part_cd
FROM po_item_alt a
JOIN sm_alt_bom sab
ON sab.prod_code = a.prod_cd
AND sab.part_code = bd.l_part_cd
AND sab.alt_part_cd = a.alt_part_cd
WHERE
a.prod_cd = bd.product_code
AND a.po_no = ?
AND a.prod_cd=?
AND a.input_part_cd = bd.l_part_cd
LIMIT 1
),
bd.l_part_cd
) AS final_part_code
code
FROM
bom_detail bd
WHERE
bd.product_code = ? order by bd.l_part_no ASC) p
TO insert data
INSERT INTO bom_detail (product_code, uom, procurement_type, proc_cd, type, proc_seq, l_part_no, l_part_cd, level, no_off, remarks) VALUES (?,?,?,?,?,?,?,?,?,?,?)
Assistant
User
What about uom and no_off they should be come from therir respective table
Assistant
Let's clarify what you want to do:
You already have a query that selects a "final_part_code" from bom_detail, optionally replacing l_part_cd with an alternative part code from po_item_alt joined with sm_alt_bom based on certain conditions.
Now, you want to insert data into bom_detail using this logic to determine the l_part_cd (or final part code).
Understanding
Your select involves:
Checking if there is an alternative part (alt_part_cd) in the po_item_alt and sm_alt_bom tables for a particular product_code and l_part_cd.
If yes, select the alternative (alt_part_cd), else original l_part_cd as final_part_code.
Your insert is a normal INSERT with these fields (sample):
You want to insert a row into bom_detail where the l_part_cd value uses the "final_part_code" like your SELECT query provides.
Approach to solve
Since the insert is one row at a time (?), you will have to determine the final_part_code in your application before insertion OR use an INSERT ... SELECT statement that performs the same logic to insert rows.
Option 1: Determine final_part_code in your app first, then insert
You run the select query (your existing query with proper parameters) once, get final_part_code, and then use this value in your insert.
Advantage: Simpler, no complex SQL insert needed.
Option 2: Use INSERT ... SELECT to insert derived rows at once
If you want to insert multiple rows derived from bom_detail, but remapping l_part_cd to final_part_code using your logic, then you can do something like:
sql
INSERT INTO bom_detail (
product_code, uom, procurement_type, proc_cd, type, proc_seq, l_part_no, l_part_cd, level, no_off, remarks
)
SELECT
bd.product_code,
bd.uom,
bd.procurement_type,
bd.proc_cd,
bd.type,
bd.proc_seq,
bd.l_part_no,
COALESCE(
(
SELECT a.alt_part_cd
FROM po_item_alt a
JOIN sm_alt_bom sab
ON sab.prod_code = a.prod_cd
AND sab.part_code = bd.l_part_cd
AND sab.alt_part_cd = a.alt_part_cd
WHERE
a.prod_cd = bd.product_code
AND a.po_no = ? -- Your PO number parameter
AND a.prod_cd = ?
AND a.input_part_cd = bd.l_part_cd
LIMIT 1
),
bd.l_part_cd
) AS l_part_cd,
bd.level,
bd.no_off,
bd.remarks
FROM bom_detail bd
WHERE bd.product_code = ?
ORDER BY bd.l_part_no ASC
In this way:
You select from existing bom_detail rows for a product_code
Determine the alternative part code as per your logic
Insert those rows back into bom_detail (probably with a different product_code or as a new copy)
Important points
Make sure you don’t create duplicate id primary keys on insert. (Here id is auto increment presumably, so it's omitted in INSERT columns to auto-generate)
Adapt the insert columns and select columns carefully.
Set the properWHERE and parameters.
Summary snippet for INSERT ... SELECT
sql
INSERT INTO bom_detail (
product_code, uom, procurement_type, proc_cd, type, proc_seq, l_part_no, l_part_cd, level, no_off, remarks
)
SELECT
bd.product_code, bd.uom, bd.procurement_type, bd.proc_cd, bd.type, bd.proc_seq,
bd.l_part_no,
COALESCE(
(
SELECT a.alt_part_cd
FROM po_item_alt a
JOIN sm_alt_bom sab ON sab.prod_code = a.prod_cd AND sab.part_code = bd.l_part_cd AND sab.alt_part_cd = a.alt_part_cd
WHERE a.prod_cd = bd.product_code
AND a.po_no = ?
AND a.prod_cd = ?
AND a.input_part_cd = bd.l_part_cd
LIMIT 1
),
bd.l_part_cd
) AS l_part_cd,
bd.level, bd.no_off, bd.remarks
FROM bom_detail bd
WHERE bd.product_code = ?
ORDER BY bd.l_part_no ASC;
Run this query with the appropriate parameters (po_no, product_code twice) to insert the adjusted rows.
If you want help writing this as a prepared statement or adjusting it for your environment, please provide details.
Got it. You want to insert rows into bom_detail but for each row:
uom and no_offshould come from their respective tables (probably from sm_alt_bom in the case of alternatives, or fallback to bom_detail values).
What do we have?
bom_detail has product_code, uom, no_off, ...
You want updated values of uom and no_off from sm_alt_bom if alternative part matched, else the original from bom_detail.
Alternative alt_no_off is in sm_alt_bom
alt_price is presumably unused here, but maybe relevant elsewhere.
So, you want the insert query logic like this:
Select from bom_detail as base.
For each row, find if an alternative part exists (alt_part_cd) via the join with po_item_alt and sm_alt_bom.
If alternative part exists, pick uom and no_off from sm_alt_bom (i.e. alt_uom, alt_no_off)
Else pick uom and no_off from bom_detail.
How to implement the logic in SQL?
You will need a LEFT JOIN or correlated subqueries to sm_alt_bom based on your conditions.
Updated insert statement with required logic
sql
INSERT INTO bom_detail (
product_code, uom, procurement_type, proc_cd, type, proc_seq, l_part_no, l_part_cd, level, no_off, remarks
)
SELECT
bd.product_code,
-- If alternative part exists, get uom from sm_alt_bom else fall back to bd.uom
COALESCE(sab.alt_uom, bd.uom) AS uom,
bd.procurement_type,
bd.proc_cd,
bd.type,
bd.proc_seq,
bd.l_part_no,
-- final part code logic same as before
COALESCE(
(
SELECT a.alt_part_cd
FROM po_item_alt a
JOIN sm_alt_bom sab2 ON sab2.prod_code = a.prod_cd AND sab2.part_code = bd.l_part_cd AND sab2.alt_part_cd = a.alt_part_cd
WHERE a.prod_cd = bd.product_code
AND a.po_no = ?
AND a.prod_cd = ?
AND a.input_part_cd = bd.l_part_cd
LIMIT 1
),
bd.l_part_cd
) AS l_part_cd,
bd.level,
-- If alternative part exists, get no_off(alt_no_off) from sm_alt_bom else fall back to bd.no_off
COALESCE(sab.alt_no_off, bd.no_off) AS no_off,
bd.remarks
FROM bom_detail bd
-- Join to sm_alt_bom to get alt uom/no_off for the alternative part code if exists
LEFT JOIN sm_alt_bom sab
ON sab.prod_code = bd.product_code
AND sab.part_code = bd.l_part_cd
AND sab.alt_part_cd = (
SELECT a.alt_part_cd
FROM po_item_alt a
WHERE a.prod_cd = bd.product_code
AND a.po_no = ?
AND a.input_part_cd = bd.l_part_cd
LIMIT 1
)
WHERE bd.product_code = ?
ORDER BY bd.l_part_no ASC;
Notes:
We join sm_alt_bom sab on the matching alternative part code obtained via a subquery on po_item_alt.
Use COALESCE between sab.alt_uom and bd.uom to select alternative uom if exists else original
Similarly for no_off use sab.alt_no_off if alternative exists else original bd.no_off.
Use placeholders (?) to bind your parameters for:
po_no for purchase order number (used twice)
product code (used twice)
product code for WHERE clause
If you want to avoid repeating subqueries, or for better performance:
You can rewrite with CTE or derived tables (depending on your DBMS support), or optimize in your application logic.
Final reminder:
Replace ? with actual parameters in your application.
Confirm that the logic matches your business rules.
Test for possible cases:
Row with no alternative => fallback values used
Row with alternative => alt values properly applied
If you want me to create a tested, ready-to-run query for MySQL or another database, or with sample data, please let me know!