I’ve been visiting this site for a while now and many of the responses on here has been most helpful. However, I’m now stuck with an SQL that I can’t seem to find just the right solution for.
(the $packplant and $ym is already defined earlier in the program)
SELECT
A.in_house_supplier_cd,
B.maker_cd,
A.packing_plant_cd,
A.parts_no,
substr(A.actual_delivery_date,1,6),
A.actual_delivered_qty
FROM
TRN_DELIVERY_NO A,
TRN_PARTS B
WHERE
A.ISSUE_NO = B.ISSUE_NO
AND A.PACKING_PLANT_CD = '$packplant'
AND B.PACKING_PLANT_CD = '$packplant'
AND A.PARTS_NO = B.PARTS_NO
AND A.IN_HOUSE_SUPPLIER_CD = B.IN_HOUSE_SUPPLIER_CD
AND A.ACTUAL_DELIVERY_DATE LIKE '$ym%'
ORDER BY
in_house_supplier_cd, maker_cd, parts_no;
This sql works fine. However, what I need is that the “A.actual_delivered_qt” to be sum(A.actual_delivered_qty)… in other words, I need the sum of that particular parts and not individual quantities that were received.
When I add the “sum..” part (or even with adding a GROUP BY parts_no), the sql gives a “column ambiguously defined” error.
I believe that I’ve already assigned the correct table to each column and therefore would really appreciate it if someone could point out the errors as I’ve been stuck with this for quite a while now. Cheers!
You will need to add a
GROUP BYstatement, for example on parts_no, but then you will have an issue with the rest of the columns in your select statement.For example, if you have 3 records for the same part no on different days and you are grouping by part_no and calculating the total number of items within that group number, the date no longer makes sense. The best you can do is select the max value from the date, but again, this doesn’t make much sense.
You should think about what data really makes sense to include in the select statement when you are grouping by part_no and then revise the columns in the select statement to meet this new design.