I would like update the column [ACIdent] from the table [Values].
With the subquery i try to get an ID from the same table.
On the point ??? i try to access to the column [PathName] of the respective row of the update script. But this solution doesn't work.
How can i do this? Many thanks in advance!
Update [Values]
Set ACIdent =
(Select b.valueIdent From Values as b Where b.SampleIdent = 0 and b.PathName = ???Values.PathName)
Where ValueIdent= 614
Restructure your query a bit so you can alias the table.
Update v
Set ACIdent =
(Select b.valueIdent From Values as b Where b.SampleIdent = 0 and b.PathName = v.PathName)
From [Values] v
Where v.ValueIdent= 614
Related
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
This might seem like a noob question, but here's the thing:
In the image, the first table name is facturaDetalle and the second one's facturamaster
I want to SUM all the total matching idfactura in facturadetalle and save them into the total column in the facturamaster table.
I'm working on a master-detail form in ASP.NET
UPDATE m
SET total = (SELECT SUM(d.total) FROM dbo.facturadetalle d WHERE d.idfactura = m.idfactura)
FROM dbo.facturamaster m
--WHERE m.total IS NULL
You can use UPDATE statement to do that.
UPDATE m
SET TOTAL = SUM(d.Total)
FROM idfactura AS m
INNER JOIN idfacturaldetelle AS d
ON m.idfactura = d.idfactura
How to update query in sql column by comparing two tables ? This might be duplicated question, but yet still cannot solve my problem. Any help would be appreciated.
What i've tried so far, but error
UPDATE b SET b.STAMP = b.STAMP + 10 FROM TB_FWORKERSCH b,TB_FWORKERCN a
WHERE a.ISSDATE>='20150401' AND a.UKEY=b.UKEY2 and b.STAMP=0 AND b.IG_SUMINS!=0
DB2 Database
DB2 doesn't allow a JOIN or FROM for an UPDATE clause (this is also not specified in the SQL standard).
You can achieve what you want with a co-related sub-query:
UPDATE tb_fworkersch b
SET stamp = stamp + 10
WHERE EXISTS (SELECT 1
FROM tb_fworkercn a
WHERE a.issdate >= '20150401'
AND a.ukey = b.ukey2)
AND b.stamp = 0
AND b.ig_sumins <> 0
Try this:
MERGE INTO TB_FWORKERSCH b
USING TB_FWORKERCN a
ON a.UKEY=b.UKEY2
AND a.ISSDATE>='20150401' AND b.STAMP=0 AND b.IG_SUMINS<>0
WHEN MATCHED
THEN UPDATE SET b.STAMP = b.STAMP + 10;
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.
What I have is a table with the following columns
All columns are nvarchar expect for the Date columns which are datetime
CreateDateA InvIDA StorageIDA CreateDateB InvIDB StorageIDB UniID
What I want to do is move the values on each row (based on UniID) into the represenative B columns where UniID is 1 less, so the 'A' columns where UniID = 2 should go to the B Columns where the UniID = 1.
This is the query i'm using but it's not updating any columns.
update InvSubmission
set CreateDateB = CreateDateA,
InvIDB = InvIDA ,
StorageIDB = StorageIDA
where StorageIDB = StorageIDA and UniID = (select UniID-1 from InvSubmission)
There is multiple storageid's in this table, however, each record has a match to at least one other row.
#PinnyM's answer is close, but in SQL Server, you have to name the updated table in the from clause also:
update toUpdate
set CreateDateB = sourceTable.CreateDateA,
InvIDB = sourceTable.InvIDA
FROM
InvSubmission toUpdate
inner Join
InvSubmission sourceTable
ON sourceTable.UniID = toUpdate.UniID + 1
AND sourceTable.StorageIDA = toUpdate.StorageIDB
Otherwise, this note becomes relevant:
If the object being updated is the same as the object in the FROM clause and there is only one reference to the object in the FROM clause, an object alias may or may not be specified
And then you're suffering the same problem that #Yuck pointed out - you're trying to compare the UniID value within a single row to a value one less than itself.
UniID = (select UniID-1 from InvSubmission) is equivalent to UniID = UniID - 1. So, for example, you're basically saying:
WHERE 2 = (2 - 1)
And that's never going to be true, which is why the query isn't modifying anything.
If both the A and B columns are in the same row you don't even need to qualify by using the ID clause. Otherwise you need to self-join like this:
update InvSubmission
set CreateDateB = B.CreateDateA,
InvIDB = B.InvIDA ,
StorageIDB = B.StorageIDA
from InvSubmission A INNER JOIN
InvSubmission B ON A.UniID = B.UniId - 1
where A.StorageIDB = A.StorageIDA;
As I mentioned it's a little confusing because your example makes it look like the columns are all contained in the same row...
It may actually depend on which RDBMS you are using (some are finicky on how aliases can be used), but you can try:
update InvSubmission
set CreateDateB = sourceTable.CreateDateA,
InvIDB = sourceTable.InvIDA ,
StorageIDB = sourceTable.StorageIDA
FROM InvSubmission sourceTable
WHERE sourceTable.UniID = InvSubmission.UniID + 1