SQL Updating a particular rows based on the ID in another table - sql

I have two tables
Price
RET_ID DBS_ID RRP
Database
DBS_ID PRO_ID
I would like to use UPDATE such that I can increase RRP in the Price table by 20% if the PRO_ID in the Database table is = 1
UPDATE
( SELECT RRP
FROM PRICE
JOIN database
ON database.dbs_id = price.dbs_id
)
SET rrp = 100
WHERE (database.pro_ID = 1);
I've been trying all manner of INNER JOIN ... ON to no avail.
Thanks.

You can put the filtering condition in the where clause. Here is a method using exists:
update price p
set rpr = rpr * 1.2
where exists (select 1
from database d
where d.dbs_id = p.dbs_id and d.pro_id = 1
);
Here is another way using in:
update price p
set rpr = rpr * 1.2
where p.dbs_id in (select d.dbs_id
from database d
where d.pro_id = 1
);

You can use this query
MERGE INTO PRICE P
USING DATABASE D ON
(P.DBS_ID=D.DBS_ID) AND
D.PRO_ID=1
WHEN MATCHED
THEN
UPDATE SET
P.RRP=P.RRP*1.2;
When you are updating table from another table, always use MERGE function which is an amazing concept in SQL

UPDATE Price P SET RRP = RRP*1.2
WHERE EXISTS
(select 1 from Database
where DBS_ID = P.DBS_ID AND PRO_ID = 1);

Related

Adding value of COUNT to an already existing value

I have a table for managing inventory movements and a table to manage stock.
I want to add the count of movements that the item exists in between given dates to the stock table.
My update statement looks like this:
UPDATE inventory
SET quantity = quantity + 1
WHERE ItemID IN
( SELECT ItemID FROM movements
WHERE group = '3' AND store = '500'
AND DateMove BETWEEN 20201219 AND 20201223 )
AND StoreNumber = '500'
How can I change this query to add the amount of times that the ItemID appears in movements to the quantity in the inventory table?
I've been thinking that I can add a count(itemID) and group by and add an alias in the subquery and use the alias after the + but it doesn't seem to work.
Thanks for any help
Using an UPDATE, you can use a correlated subquery:
UPDATE inventory i
SET quantity = (i.quantity +
(SELECT COUNT(*)
FROM movements m
WHERE m.ItemId = i.ItemId AND
m.group = 3 AND m.store = i.store AND
m.DateMove BETWEEN 20201219 AND 20201223
)
)
WHERE i.store = 500 AND
EXISTS (SELECT 1
FROM movements m
WHERE m.ItemId = i.ItemId AND
m.group = 3 AND m.store = 500 AND
m.DateMove BETWEEN 20201219 AND 20201223
);
Note that I removed the single quotes around 500 and 3. These values look like numbers. Only use the single quotes if they are strings.
Oracle also allows you to update using a subquery under some circumstances, so this should work as well:
update (select i.*,
(SELECT COUNT(*)
FROM movements m
WHERE m.ItemId = i.ItemId AND
m.group = 3 AND m.store = i.store AND
m.DateMove BETWEEN 20201219 AND 20201223
) as inc_quantity
from inventory i
where store = 500
)
set quantity = quantity + inc_quantity
where quantity > 0;
You appear to want a MERGE statement and to COUNT the movements:
MERGE INTO inventory dst
USING (
SELECT ItemID,
store,
COUNT(*) AS num_movements
FROM movements
WHERE group = 3
AND store = 500
AND DateMove BETWEEN DATE '2020-12-19' AND DATE '2020-12-23'
GROUP BY
ItemId,
store
) src
ON ( src.ItemId = dst.ItemId AND dst.StoreNumber = src.store )
WHEN MATCHED THEN
UPDATE
SET quantity = dst.quantity + src.num_movements;
(Also, if values are numbers then use number literals and not string literals and, for dates, use date literals and not numbers.)
You need a correlated subquery. For brevity, I've omitted all other where conditions.
UPDATE inventory AS inv
SET quantity = quantity + (SELECT COUNT(*) FROM movements AS mov WHERE mov.itemID = inv.itemID);
I think it's better to create a view such as
CREATE OR REPLACE VIEW v_inventory AS
SELECT i.*,
SUM(CASE
WHEN i.StoreNumber = 500 AND m."group" = 3 AND m.store = 500 AND
m.DateMove BETWEEN date'2020-12-19' AND date'2020-12-23'
THEN
1
ELSE
0
END) OVER (PARTITION BY m.ItemID) AS mov_quantity
FROM inventory i
LEFT JOIN movements m
ON m.ItemID = i.ItemID
rather than applying a DML for the sake of good db design, since the desired count already can be calculated, and may yield for later confusions

PL/SQL Update Query

Attempting to update a table I have created with null values from another table I have created in PL/SQL:
I was able to utilize the below update in SQL Server but am running into issues within PL/SQL (dont ask why I am running it in both)
Overall_Inventory = Table created with some populated values and some null valuse; this is the table requiring updates to those null values
task_table = Table also created, but contains value needing to be updated into T1
update dbh.overall_inventory
set dbh.overall_inventory.case_due_date = tsk.TASK_ACTION_TIMESTAMP
from dbh.overall_inventory,
(SELECT tsk.INQ_KEY,
min(tsk.TASK_ACTION_TIMESTAMP) as TASK_ACTION_TIMESTAMP
FROM dbh.task_table tsk
inner join dbh.overall_inventory Inv
on tsk.INQ_KEY = inv.inq_key
where tsk.ACTION_CD = '324'
group by tsk.INQ_KEY
) tsk
where tsk.INQ_KEY = dbh.overall_inventory.inq_key`
Oracle doesn't support from clause in the update statement. In this situation merge statement can be used.
merge into overall_inventory oi
using (select tsk.inq_key,
min(tsk.task_action_timestamp) as task_action_timestamp
from task_table tsk
join overall_inventory Inv
on tsk.inq_key = inv.inq_key
where tsk.action_cd = '324'
group by tsk.inq_key) tsk
on (tsk.inq_key = oi.inq_key )
when matched then
update
set case_due_date = tsk.task_action_timestamp
where case_due_date is null -- as I understood only NULL values
-- need to be updated
Note: Not tested because no sample data and desired result were provided.
I think you're looking for update over a select:
UPDATE (
  SELECT product_id, category_id
  FROM product) st
SET st.category_id = 5
WHERE st.category_id = 4;

Update if not exists, if exists check if code is different from 1 and update

I'm trying to build a SQL command to update my table
Update a row in myProduct if it does not exists in table order, or if it exists, check in table order if the code is != 1 to update
UPDATE myProduct
SET statut = 3
WHERE
myProduct_id NOT IN (SELECT myProduct_id
FROM order
WHERE code != 1
AND myProduct.myProduct_id = order.myProduct_id)
OR NOT EXISTS IN (SELECT myProduct_id FROM order)
Thank you in advance
use the below code..
UPDATE p
SET statut = 3
FROM myProduct p
WHERE NOT EXISTS
(SELECT 1
FROM order o
WHERE code != 1
AND p.myProduct_id = 0.myProduct_id)
I believe the logic is: Update all rows where the corresponding row in order is not 1 (or none exists).
If so, you can do this with a simpler statement:
UPDATE myProduct p
SET statut = 3
WHERE NOT EXISTS (SELECT 1
FROM orders o
WHERE o.code = 1 AND p.myProduct_id = o.myProduct_id);
You don't specify the database, but the above is standard SQL.
Note: order is a really bad name for a table, because it is a SQL keyword. That is why I used orders instead of order.
UPDATE myProduct SET statut = 3
WHERE NOT EXISTS(SELECT 1 FROM order WHERE code != 1 AND myProduct.myProduct_id = order.myProduct_id )

sql using select and adding to a table

SELECT `pro`.`St`, `s`.`Quantity`
FROM `s`
LEFT JOIN `web`.`pro` ON `s`.`Pro_id` = `pro`.`ProdID`
the above query results in a table like
st quantity
132 1
11 1
st is from one table and quantity is from another
using this select statement I want to add the resulting quantity to the st row in the other table.
Basically updating the second table by adding the quantity i get from this select statement.
UPDATE `web`.`pro`
SET `pro`.`st` = `pro`.`st` + `s`.`Quantity`
FROM `web`.`pro`
JOIN `shopping cart` `s`
ON `s`.`Pro_id` = `pro`.`ProdID`;
Something like that ?

Selective update in SQL Server

I've created a junction table like this one:
http://imageshack.us/scaled/landing/822/kantotype.png
I was trying to figure out a query that could able to select some rows - based on the PokémonID - and then updating only the first or second row after the major "filtering".
For example:
Let's suppose that I would like to change the value of the TypeID from the second row containing PokémonID = 2. I cannot simply use UPDATE KantoType SET TypeID = x WHERE PokémonID = 2, because it will change both rows!
I've already tried to use subqueries containing IN,EXISTS and LIMIT, but with no success.
Its unclear what are your trying to do. However, you can UPDATE with JOIN like so:
UPDATE
SET k1.TypeID = 'somethng' -- or some value from k2
FROM KantoType k1
INNER JOIN
(
Some filtering and selecting
) k2 ON k1.PokémonID = k2.PokémonID
WHERE k1.PokémonID = 2;
Or: if you want to UPDATE only the two rows that have PokémonID = 2 you can do this:
WITH CTE
AS
(
SELECT *,
ROW_NUMBER() OVER(ORDER BY TypeID) rownum
FROM KantoType
WHERE PokemonID = 2
)
UPDATE c
SET c.TypeID = 5
FROM CTE c
WHERE c.rownum = 1;
SQL Fiddle Demo
I can suggest something like this if you just need to update a single line in your table:
UPDATE kantotype
SET
type = 2
WHERE pokemon = 2
AND NOT EXISTS (SELECT * FROM kantotype k2
WHERE kantotype.type > k2.type
AND kantotype.pokemon = k2.pokemon)
It would be easier to get the first or last item of the table if you had unique identifier field in your table.
Not sure even if you are trying to update the row with PokemenID =2 by doing a major filtering on TypeID... So just out of assumptiong (big one), you can give a try on Case
UPDATE yourtable a
LEFT JOIN youtable b on a.pokeid = b.pokeid
SET a.typeid = (CASE
WHEN a.typeid < b.typeid THEN yourupdatevalue
WHEN a.typeid > b.typeid THEN someothervalue
ELSE a.typeid END);
If you know the pokemon ID and the type id then just add both to the where clause of your query.
UPDATE KantoType
SET TypeID = x
WHERE PokémonID = 2
AND TypeID=1
If you don't know the type ID, then you need to provide more information about what you're trying to accomplish. It's not clear why you don't have this information.
Perhaps think about what is the unique identifier in your data set.