SQL Noob compare table in opencart - sql

I have one table called VEGAN with model numbers that i want 3800 rows with model or SKU codes. thats it.
In the main oc_products table there are 15000 products / rows with over 20 columns, In one of those columsn there is a control for enabled which can be set to 0 or 1.
I want to Disable all products in the main oc_products table if they are not in the VEGAN table, or Disable(set status to 0) them all first and Enable them (set status to 1) if they are in the Vegan Table.
So far I have come up with this... I am a first timer here.
FIRST DISABLE EVERYTHING...
UPDATE oc_product
SET status= '0'
WHERE 1
then...
UPDATE oc_product SET status= '1' WHERE 'ocproduct.model' = ‘VEGAN.SKU'
It doesn't work and Im stuck,
please help.

Try this query...
update oc_product set status = 1 where model in (select sku from vegan)
This assumes that oc_product.model matches vegan.sku.
Any vegan.sku with a matching oc_product.model will get oc_product.status set to 1.

For your second update statement you should do something like:
UPDATE oc_product SET status = '1'
WHERE ocproduct.model IN (SELECT SKU FROM Vegan)
or you can use the alternate format:
UPDATE oc_product SET status = '1'
FROM oc_product
INNER JOIN Vegan
ON oc_product.model = Vegan.SKU

Related

SQL Updating records based on most recent date

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

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

How to deduct quantity of items in database?

I'm creating a system where I must deduct and add by one if the item is borrowed/returned. In my system I have 2 tables:
tblItems(_itemnumber_, _itemname_, _quantity_)
tblBorrow(_dateborrowed_, _datedue_, _status_)
Where if I borrowed an item the status will be borrowed and the quantity will deducted by 1 and if the status is returned the quantity will added by 1. This is my idea(I already how to add and update) but i don`t know how to code this idea. I'd really appreciate your help.
I`m using imports system.data.oledb
First, the underscores might be eliminated to improve readability.
Next, tblBorrow needs itemnumber, else all Borrowings look the same.
tblBorrow(_itemnumber_,_dateborrowed_, _datedue_, _status_)
Then the Borrow_Clicked button would have code to the effect of
If tblItems for this _itemnumber_ has _quantity_ <= 0 then
msg"There is no Quantity to Borrow"
exit
else
Update tblItems Set _quantity_ = _quantity_ - 1 Where _itemnumber_ = x
Insert tblBorrow Item#, Date, Due, "Borrowed"
End If
There would also be a Returned_Click button, with code to the effect of
If tblBorrow for this Item# exists with a "Borrowed" status then
Update tblItems Set _quantity_ = _quantity_ + 1 Where _itemnumber_ = x
Update tblBorrow Set Status = "Returned"
else
msg"Item status is not Borrowed"
End If
Each of these should also edit the Item# to insure that is it valid.

SQL displaying wrong records in OR condition

I am struggling to write query for the below scenario, can any one help me please?
I have two column error and priority both are numeric fields. need to fetch based on the condition. If i select the value 3 for example from UI screen, it has to look for record in the table.
if the value match in any of the column i have to return the record i have written like this
Select *
FROM WorkItems
WHERE [System.Title] like '%defect%'
AND [System.WorkItemType] in ('Incident','bug')
AND [ErrorClass] =3
OR [Microsoft.VSTS.Common.Priority] =3
AND [Customer] = 'XYZ'
order by [System.WorkItemType]
Its giving false record and fetching all the records with priority or errorclass = 3, i need the records only workitem type = incident or bug for error class=3 or priority = 3
its returning all the records from the table.
If i put and condition like this [ErrorClass] = 3 and [Microsoft.VSTS.Common.Priority] = 3 its returning only if both the value are 3. I need all the records which match 3 along with above condition
try
Select *
FROM WorkItems
WHERE System.Title like '%defect%'
AND System.WorkItemType in ('Incident', 'bug')
AND (ErrorClass = 3 OR Microsoft.VSTS.Common.Priority = 3)
AND Customer = 'XYZ'
order by System.WorkItemType

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.