I need UPDATE some column. t1_id belongs table "h" and t2_id is column table "w". The picture shows the values equal to each other, but have different names, i.e. data of "18840263133600217"
= data of "339910"
How can I rename all t1_id to name from t2_id? I want to avoid the error "subquery in the expression returned more than one row"..
update h set h_user_id =
(select distinct w.h_user_id
from h inner join w
on h.h_ip = w.h_ip
where "some condition")
Can you use UPDATE FROM?
UPDATE h SET h_user_id = w.h_user_id
FROM w WHERE h.h_ip = w.h_ip AND "some condition"
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
I have an issue in information link SQL query which joins multiple tables where some dynamic column names are returned. They are stored in label and value attribute of the below query. Please note we had certain fields in the database which when null doesn't appear as column in the output. For e.g a field test1 in the database has all values null then test1 doesn't get returned. However, even if a single value (row) of test1 has some data, I see test1 in the output. The requirement is always have this field in the output irrespective of whether it has the data for not. Please see the query below and point out what is causing issue.
SELECT
I1."item_uid" AS "ITEMUID",
I1."item_reference_uid" AS "ITEMREFERENCEUID",
w2."workflow_abbrv" AS "STATUS",
s3."label" AS "LABEL",
s3."value" AS "VALUE",
a4."active_status_desc" AS "ACTIVESTATUSDESC",
I5."item_reference_uid" AS "PARENTID",
I5."item_desc_display" AS "PARENTDESCDISPLAY"
FROM
("synaptica"."dbo"."sub_elements" s3 LEFT OUTER JOIN "synaptica"."dbo"."ITEMS_REPOSITORY" I1 ON I1."item_uid" = s3."item_uid")
LEFT OUTER JOIN("synaptica"."dbo"."ITEMS_REPOSITORY" I5
RIGHT OUTER JOIN(select "syndetic_key_item", "rel_type_abbrv", "syndetic_rel_item" from "synaptica"."dbo"."SYNDETIC_NETWORK" s55
left join "synaptica"."dbo"."RELATIONSHIP_TYPES" R66 ON S55."syndetic_rel_type" = R66."rel_type_uid"
where (R66."rel_type_abbrv" = 'PhysParent' or R66."rel_type_abbrv" is null))S6
ON
I5."item_uid" = S6."syndetic_rel_item") ON I1."item_uid" = S6."syndetic_key_item",
"synaptica"."dbo"."workflow_types" w2,
"synaptica"."dbo"."active_statuses" a4,
"synaptica"."dbo"."OBJECT_CLASSES" O8,
"synaptica"."dbo"."approval_statuses" a9
WHERE
(I1."item_active_status" = a4."active_status_uid")
AND (a9."approval_status_uid" = I1."item_approval_status")
AND (I1."item_object_class" = O8."object_class_uid")
AND (I1."item_workflow" = w2."workflow_uid")
AND ((left(O8."object_class_abbrv",
7) = left(?VER,
7)))
AND (s3."label" <> 'CLS360_OBJECT_ID')
AND (RIGHT(O8."object_class_abbrv",
10) = 'PHYS CLASS')
I had two tables first one is the real table second one is the temporary. I had to compare this two table . First table had 'KartelaKod' field should match with temp's 'KartelaKodu' field, First's 'OzellikKod' field should match with temp's 'OzellikKodu',first's 'AltKod' field should match with temp's 'StokKodu'.
Finally if it doesn't match exactly it should return ''.
PS: You should have to group first table according to VrtUrunKod.
In example if MAM|002's (which is VrtUrunKod) OzellikKod field doesn't match with temp table's OzellikKodu field, it should look for another VrtUrunKod record.
Edit1:isnull((Select TOP 1 VrtUrunKod From KonfigOlusmusOzellikler k Where Sirket_Kod = #sp_Sirket_Kod and AnaUrunKod = #spStokKod and
exists(Select * From #TempDegisen t Where t.KartelaKodu = k.KartelaKodu and t.OzellikKodu = k.OzellikKod and t.StokKodu = k.AltKod)),'') this is what i written so far you can ignore Sirket_Kod field and parameter.
This should produce a list of rows where there is a k with no matching t, and at with no matching k. If no rows result, all rows are matched:
SELECT *
FROM
KonfigOlusmusOzellikler k
FULL OUTER JOIN
#TempDegisen t
ON
t.KartelaKodu = k.KartelaKodu and
t.OzellikKodu = k.OzellikKod and
t.StokKodu = k.AltKod
WHERE t.KartelaKodu IS NULL OR k.KartelaKodu IS NULL
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