SQL Updating records based on most recent date - sql

I am trying to update two fields (UNIT_COST) from (LST_RECV_COST) and VEND_ITEM_NO) from same filed (VEND_ITEM_NO).
All fields in one table (PO_VEND_ITEM). The table has sequence rows sorted by date filed (LST_RECV_DAT).
I want to update the data with record in the most recent date row.
I have used the following code
UPDATE
PO_VEND_ITEM
SET
UNIT_COST = LST_RECV_COST,
VEND_ITEM_NO = VEND_ITEM_NO,
WHERE
LST_RECV_DAT = (SELECT MAX(LST_RECV_DAT)
It always get any error message. I am new to sql and do not know which code can work.
Could you advise please?

You have some syntax mistakes.
Try this.
UPDATE PO_VEND_ITEM
SET UNIT_COST = LST_RECV_COST,
VEND_ITEM_NO = VEND_ITEM_NO
WHERE LST_RECV_DAT = (SELECT MAX(LST_RECV_DAT) FROM PO_VEND_ITEM)

use below DML to update table based on max() field
update PO_VEND_ITEM
set UNIT_COST = LST_RECV_COST
from
(select max(LST_RECV_DAT) LST_RECV_DAT,
VEND_ITEM_NO from PO_VEND_ITEM
group by VEND_ITEM_NO) t1
where VEND_ITEM_NO = t1.VEND_ITEM_NO and t1.LST_RECV_DAT = LST_RECV_DAT

Related

How to replace a value in one field by a value from another field (different column) within the same view table (SQL)?

I'd like to know if it is possible to replace a value from one field by using a value from another column and a different row.
For Example, click this to view the table image.
I'd like the SumRedeemed value 400 in row 15 to be replaced by the value -1*(-395); the value -395 comes from the EarnPointsLeft in row 6 (both have the same CID meaning that they are the same person). Any suggestions?
You need this update statement:
update t
set t.sumredeemed = (-1) * (select earnpointsleft from FifoPtsView where cid = t.cid)
from FifoPtsView t
where t.cid = 5000100008 and t.earnpointsleft = 0
This will work if the select statement will return only 1 row.
you can simply update your table
update t
set t.sumredeemed = ABS(t2.earnpointsleft )
from FifoPtsView t join FifoPtsView t2 on t.cid = t.cid and isnull(t2.earnpointsleft,0)>0
if you want negative values you can remove ABS ,
please give me your feedbacks

ORA-00904 Invalid Identifier - Update Statement With Two Tables

I'm working with PeopleSoft Campus Solutions, and we need to update about 22,000 rows of data. This is data between the tables of ACAD_PLAN_VW and ACAD_PROG. Students are listed on both, so their IDs match between the two.
Basically what we are trying to do is say that when the ID, academic career, student career number, effective sequence, and effective date match, AND where the academic plan (their degree, as stored on the ACAD_PLAN_VW) is a specific value, update the ACAD_PROG on the ACAD_PROG table to X value.
I tried to do some very interesting combinations of FROM statements, constantly getting errors. After some researching, I found out that SQLTools doesn't really like FROM statements within UPDATE statements, so I rewrote it to just make the connections manually. I'm assuming I'm doing this right, unless I need to reword it.
The statement I have is:
UPDATE PS_ACAD_PROG SET PS_ACAD_PROG.ACAD_PROG = 'UGDS'
WHERE PS_ACAD_PLAN_VW.EMPLID = PS_ACAD_PROG.EMPLID
AND PS_ACAD_PLAN_VW.ACAD_CAREER = PS_ACAD_PROG.ACAD_CAREER
AND PS_ACAD_PLAN_VW.STDNT_CAR_NBR = PS_ACAD_PROG.STDNT_CAR_NBR
AND PS_ACAD_PLAN_VW.EFFSEQ = PS_ACAD_PROG.EFFSEQ
AND PS_ACAD_PLAN_VW.EFFDT = PS_ACAD_PROG.EFFDT
AND PS_ACAD_PLAN_VW.ACAD_PLAN = 'DSTDS'
Theoretically, I would assume that this would update any student who has those connections. However, the error that I'm currently getting is as follows:
ORA-00904: "PS_ACAD_PLAN_VW"."ACAD_PLAN": invalid identifier
I have, as of yet, been unable to figure out the issue. I do have the correct access to view and update those fields, and the field does indeed exist.
Oracle doesn't know it should use the PS_ACAD_PLAN_VW table. Somehow you should reference it.
For example can you try this way?
UPDATE (
select
PS_ACAD_PROG.ACAD_PROG,
PS_ACAD_PLAN_VW.ACAD_PLAN
from
PS_ACAD_PROG,
PS_ACAD_PLAN_VW
where
PS_ACAD_PLAN_VW.EMPLID = PS_ACAD_PROG.EMPLID
AND PS_ACAD_PLAN_VW.ACAD_CAREER = PS_ACAD_PROG.ACAD_CAREER
AND PS_ACAD_PLAN_VW.STDNT_CAR_NBR = PS_ACAD_PROG.STDNT_CAR_NBR
AND PS_ACAD_PLAN_VW.EFFSEQ = PS_ACAD_PROG.EFFSEQ
AND PS_ACAD_PLAN_VW.EFFDT = PS_ACAD_PROG.EFFDT
)
SET
ACAD_PROG = 'UGDS'
WHERE
ACAD_PLAN = 'DSTDS'
Try to use the table which is not getting identified,by keeping in where clause so that you can use the select statement.That will help identifying the tables.
The following implementation should work:
UPDATE PS_ACAD_PROG
SET ACAD_PROG = 'UGDS'
WHERE ROWID IN(SELECT PROG.ROWID
FROM PS_ACAD_PROG PROG
, PS_ACAD_PLAN_VW PLAN
WHERE PLAN.EMPLID = PROG.EMPLID
AND PLAN.ACAD_CAREER = PROG.ACAD_CAREER
AND PLAN.STDNT_CAR_NBR = PROG.STDNT_CAR_NBR
AND PLAN.EFFSEQ = PROG.EFFSEQ
AND PLAN.EFFDT = PROG.EFFDT
AND PLAN.ACAD_PLAN = 'DSTDS');

SQL, return a row multiple times from single table based on variable

I'm creating some box labels using iReport and need to multiply out the data for each box label.
I have 2 parameters, #id for the record id and #typ for the package type required
SELECT
dr_id,
dr_to_customer,
dr_company_name,
dr_address_0,
dr_address_1,
dr_address_2,
dr_address_3,
dr_postcode,
dr_contact,
dr_per_packs,
dr_per_boxes,
dr_per_pallets
FROM delreq
WHERE dr_id = #id AND ??timesTOrepeat?? = #typ (a string)
I need to return each row x times based on the qty's in either packs, boxes or pallets.
So, I need to first select the qty based on #typ then work out how to multiply out the rows.
Help.
You can try to achieve this using outer join. Try this:
SELECT
dr_id,
dr_to_customer,
dr_company_name,
dr_address_0,
dr_address_1,
dr_address_2,
dr_address_3,
dr_postcode,
dr_contact,
dr_per_packs,
dr_per_boxes,
dr_per_pallets
FROM delreq a, (select -1 id from all_objects where rownum < #typ) b
WHERE dr_id = #id AND a.dr_id(+) = b.id;
I am assuming here that dr_id will not have value -1 also assuming db is oracle [not sure abut other DB.

How do I update a subset of values in DB2 based on related tables?

I am trying to remove the last 10 characters from a CLOB field in a DB2 database. I can do so using:
UPDATE report
SET comment = LEFT(comment, (LENGTH(comment) - 10))
However, I want to limit the truncation to a subset of the rows based on whether or not the report is in the current reporting period. I tried this...
UPDATE report
SET comment =
( SELECT LEFT(comment, (LENGTH(comment) - 10))
FROM report
INNER JOIN report_period
ON report.report_period_id = report_period.report_period_id
WHERE report_period.name = '2013 Interim Report' )
...but I get
The result of a scalar fullselect, SELECT INTO statement, or
VALUES INTO statement is more than one row
What am I doing wrong?
Never mind... asking the question helped clear it up in my head. Just had to move the join to the WHERE clause....
UPDATE report
SET comment = LEFT(comment, (LENGTH(comment) - 10))
WHERE report_id IN
( SELECT report_id
FROM report
INNER JOIN report_period
ON report.report_period_id = report_period.report_period_id
WHERE report_period.name = '2013 Interim Report' )

Update 1 field in a table from another field in a different table (OS400, not a 1 to 1 relationship)

Im trying to update a field in a table from another field in a different table.
The table being updated will have multiple records that need updating from 1 match in the other table.
Example, i have a 1 million row sales history file. Those million records have aproximately 40,000 different sku codes, each row has a date and time stamp. Each sku will have multiple records in there.
I added a new field called MATCOST (material cost).
I have a second table containing SKU and the MATCOST.
So i want to stamp every line in table 1 with the corresponding SKU's MATCOST in table2. I cannot seem to achieve this when its not a 1 to 1 relationship.
This is what i have tried:
update
aulsprx3/cogtest2
set
matcost = (select Matcost from queryfiles/coskitscog where
aulsprx3/cogtest2.item99 = queryfiles/coskitscog.ITEM )
where
aulsprx3/cogtest2.item99=queryfiles/coskitscog.ITEM
But that results in the SQL error: Column qualifier or table COSKITSCOG undefined and highlighting the q in the last reference to queryfiles/coskitscog.Item
Any idea's ?
Kindest Regards
Adam
Update: This is what my tables look like in principle. 1 Table contains the sales data, the other contains the MATCOSTS for the items that were sold. I need to update the Sales Data table (COGTEST2) with the data from the COSKITCOG table. I cannot use a coalesce statement because its not a 1 to 1 relationship, most select functions i use result in the error of multiple selects. The only matching field is Item=Item99
I cant find a way of matching multiple's. In the example we would have to use 3 SQL statements and just specify the item code. But in live i have about 40,000 item codes and over a million sales data records to update. If SQL wont do it, i suppose i'd have to try write it in an RPG program but thats way beyond me for the moment.
Thanks for any help you can provide.
Ok this is the final SQL statement that worked. (there were actually 3 values to update)
UPDATE atst2f2/SAP20 ct
SET VAL520 = (SELECT cs.MATCOST
FROM queryfiles/coskitscog cs
WHERE cs.ITEM = ct.pnum20),
VAL620 = (SELECT cs.LABCOST
FROM queryfiles/coskitscog cs
WHERE cs.ITEM = ct.pnum20),
VAL720 = (SELECT cs.OVRCOST
FROM queryfiles/coskitscog cs
WHERE cs.ITEM = ct.pnum20),
WHERE ct.pnum20 IN (SELECT cs.ITEM
FROM queryfiles/coskitscog cs)
This more compact way to do the same thing should be more efficient, eh?
UPDATE atst2f2/SAP20 ct
SET (VAL520, VAL620, VAL720) =
(SELECT cs.MATCOST, cs.LABCOST, cs.OVRCOST
FROM queryfiles/coskitscog cs
WHERE cs.ITEM = ct.pnum20)
WHERE ct.pnum20 IN (SELECT cs.ITEM
FROM queryfiles/coskitscog cs)
Qualify the columns with correlation names.
UPDATE AULSPRX3/COGTEST2 A
SET A.matcost = (SELECT matcost
FROM QUERYFILES/COSKITSCOG B
WHERE A.item99 = B.item)
WHERE EXISTS(SELECT *
FROM QUERYFILES/COSKITSCOG C
WHERE A.item99 = C.item)
From UPDATE, I'd suggest:
update
aulsprx3/cogtest2
set
(matcost) = (select Matcost from queryfiles/coskitscog where
aulsprx3/cogtest2.item99 = queryfiles/coskitscog.ITEM)
where
aulsprx3/cogtest2.item99=queryfiles/coskitscog.ITEM
Note the braces around matcost.