SQL-Oracle: Updating table multiple row based on values contained in the same table - sql

I have one table named: ORDERS
this table contains OrderNumber's which belong to the same person and same address lines for that person.
However sometimes the data is inconsistent;
as example looking at the table screenshot: Orders table with bad data to fix -
you all can noticed that orderNumber 1 has a name associated to and addresses line1-2-3-4. sometimes those are all different by some character or even null.
my goal is to update all those 3 lines with one set of data that is already there and set equally all the 3 rows.
to make more clear the result expected should be like this:
enter image description here
i am currently using a MERGE statement to avoid a CURSOR (for loop )
but i am having problems to make it work
here the SQL
MERGE INTO ORDERS O USING
(SELECT
INNER.ORDERNUMBER,
INNER.NAME,
INNER.LINE1,
INNER.LINE2,
INNER.LINE3,
INNER.LINE4
FROM ORDERS INNER
) TEMP
ON( O.ORDERNUMBER = TEMP.ORDERNUMBER )
WHEN MATCHED THEN
UPDATE
SET
O.NAME = TEMP.NAME,
O.LINE1 = TEMP.LINE1,
O.LINE2 = TEMP.LINE2,
O.LINE3 = TEMP.LINE3,
O.LINE4 = TEMP.LINE4;
the biggest issues i am facing is to pick a single row out of the 3 randomly - it does not matter whihc of the data - row i pick to update the line/s
as long i make the records exaclty the same for an order number.
i also used ROWNUM =1 but it in multip[le updates will only output one row and update maybe thousand of lines with the same address and name whihch belong to an order number.
order number is the join column to use ...
kind regards

A simple correlated subquery in an update statement should work:
update orders t1
set (t1.name, t1.line1, t1.line2, t1.line3, t1.line4) =
(select t2.name, t2.line1, t2.line2, t2.line3, t2.line4
from orders t2
where t2.OrderNumber = t1.OrderNumber
and rownum < 2)

Related

Find a single row and update it with nested queries

Good evening everyone, I'm trying to do an update on a Table but I can't really make it work
The feature needed is:
-Watch a field on a form, it contains the number of people that need to sit at the restaurant table.
-Find the first free table that has enough seats, set it as busy and assign a random waiter
Any idea?
more db infos:
Table "Waiters" is composed by ID(Autonumber),Name(Short Text). Has 2 names atm
Table "Tables" is composed by ID(Autonumber),Seats(Number),Busy(y/n),Waiter(short text). All tables have a fixed number of seats and have no Waiter + not busy
SOLUTION:
In the end i used "First" for the assignment and it works perfectly as it follows:
UPDATE Tables SET Tables.Waiter = DLookUp("FirstName","TopWtr")
WHERE ID IN (SELECT FIRST (ID)
FROM Tables
WHERE Seats >= Val(Forms!Room!Text12) AND Waiter Is Null);
Top wasn't working because it was returning multiple records - every table with same number of seats - and couldn't make it work with DISTINCT. This works probably because the table is already ordered by seats
Thanks to June7 for the input
Cannot SET a field value to result of a SELECT subquery - SELECT returns a dataset not a single value. Can return a single value with domain aggregate function.
Build a query object named TopWtr:
SELECT Top 1 ID FROM Waiters ORDER BY Rnd(ID);
Then use DLookup to pull that value. The Busy field seems redundant because if table has a waiter assigned that would indicate busy.
UPDATE Tables SET Tables.Waiter = DLookUp("ID","TopWtr"), Tables.Busy = True
WHERE ID IN (SELECT TOP 1 ID FROM Tables
WHERE Seats >= Val(Forms!Room!Testo17) AND Waiter Is Null
ORDER BY Seats)
An INNER JOIN may be preferable to WHERE clause:
UPDATE Tables INNER JOIN (SELECT TOP 1 ID FROM Tables
WHERE Seats >= Val(Forms!Room!Testo17) AND Waiter Is Null
ORDER BY Seats) AS T1
ON Tables.ID = T1.ID
SET Tables.Waiter = DLookUp("ID","TopWtr"), Tables.Busy = True

How to update table field value in one table from field value in another table

I am trying to update field value from one table to another.
Item with bomRev='A' in Destination table look like show below
Same Item bomRev='A' in source table looks like
I want to update partid field in destination table for bomRev=A by the value in Source filed i want to destination looks exactly like the source.
I tried this but no luck
UPDATE [MIBOMD]
SET [MIBOMD].[partId] = [assy].[partId]
FROM [MIBOMD] INNER JOIN [assy] ON [MIBOMD].[partId] = [assy].[partId]
WHERE bomRev='A' and [bomItem]='600797' AND [MIBOMD].[partId]!=[assy].[partId];
UPDATE m
SET [partId] = a.[partId]
FROM
[MIBOMD] m
INNER JOIN
[assy] a
ON m.[bomItem] = a.[ItemId]
AND m.bomEntry = a.bomEntry
WHERE
m.bomRev='A'
AND m.[bomItem]='600797'
AND m.[partId]!=a.[partId];
You actually were pretty close! Just a couple of key differences. Before I explain I have used Table Aliases in the code I provided it is a shorthand way of referring to the table throughout the query that will make it a little easier to follow and read. To Alias a table after the table name in the from statement simply add a space and an alias or a space " AS " alias.
Now your Join as on partid in your version and that was your main issue. Because you want the records where partid are not the same so you can change the partid of the assy table. Looking at your dataset I was able to determine that the shared key was mibomd.bomItem and assy.ItemId. After clearing that up everything should be good.
Per your comment the only other thing that needed to be added was a second condition on the join to make it unique. [MIBOMD].bomEntry = assy.bomEntry
A little about join conditions. Typically you always want to figure out what the unique relationship between the 2 tables are (bomItem = ItemId and bomEntry = bomEntry) and that is what will go in the ON area of the join. Rarely that will be different and will be for very specific purposes.
Per your comment on how to insert the missing records
INSERT INTO MIBOMD (bomItem, bomRev, bomEntry, lineNbr, dType, partId)
SELECT
bomItem = a.ItemId
,bomRev = 'A' --or change the value to what you want
,a.bomEntry
,lineNbr = ???? --not sure how you are figure this out do if you wan it to be the next line number you can figure that out automatically if you need
,a.partId
FROM
assy a
LEFT JOIN MIBOMD m
ON a.ItemId = m.bomItem
AND a.bomEntry = m.bomEntry
WHERE
m.bomItem IS NULL
This time you would use a left join from assy to mibomd and figure out when they don't match mibomd.bomItem IS NULL

Update from inner join

I can't seem to get my update from inner join method to work. I have tried to write it a few different ways to no avail. Rows get affected but the product table's values are never actually updated.
Select code to test (should return no results after update query is run):
SELECT
[P].[ProductCode],
[P].[PriceSelling],
[ID].[InclPrice],
[P].[StockQty],
[ID].[Qty]
FROM
dbo.Sync_ImportData AS [ID]
INNER JOIN
dbo.Sync_Product AS [P]
ON
[P].[ProductCode] = [ID].[ISBN]
WHERE
([P].[StockQty] <> [ID].[Qty] OR [P].[PriceSelling] <> [ID].[InclPrice])
Update Code:
UPDATE [P]
SET
[P].[StockQty] = [ID].[Qty],
[P].[PriceSelling] = [ID].[InclPrice]
FROM
dbo.Sync_Product AS [P]
INNER JOIN
dbo.Sync_ImportData AS [ID]
ON
[P].[ProductCode] = [ID].[ISBN]
WHERE
([P].[StockQty] <> [ID].[Qty] OR [P].[PriceSelling] <> [ID].[InclPrice])
Basically I need to update the stock qty and price if the Imported Data a) has a record for the product (ISBN matches ProductCode) and b) either value needs to be updated (qty or price is different from ImportData)
You could see this behavior if there were more than one match between the tables. In particular, if ISBN is not unique in Sync_ImportData:
SELECT id.ISBN, COUNT(*)
FROM dbo.Sync_ImportData id
GROUP BY id.ProductCode
HAVING COUNT(*) > 1
If there are duplicates, then one of the rows would be used for the update -- arbitrarily. The changes would not match the second row.

condition that applies to multiple lines before update is made in another table

I have two tables, one table is linked by a foreign key to the other.
I need to create an update statement that would update table one based on a condition in table two. However, the conditions have to relate to one or more lines in table two.
Example - Table Two
Order A
line 1 = open (status column)
line 2 = closed (staus column)
When line 1 is also closed (same order number - in this case A), the condition is met so the order will then be closed (updated to closed) in the other table. Table on only has header information (no multiple lines).
I am having trouble with righting a condtion that looks at multiple lines in table two (all lines have to be closed) before the update to table one is made.
Any helpful suggestions would be appreciated.
Assuming that (1) the key name is ordernum, and (2) EVERY table1 entry has at least one entry in table2, this is a simple query that should work for you. Basically, the not exists clause is testing that there are no open lines in the second table.
update table1
set table1.status = 'closed'
where table1.status = 'open'
and not exists
(select 1
from table2
where table2.ordernum = table1.ordernum
and table2.status = 'open')
This may need tweaked further based on your business requirements.
Update based on user request: You could try this, but performance may take a hit, I've not tested it:
update table1
set table1.status = 'closed',
table1.count_lines =
(select count(1)
from table2 y
where y.ordernum = table1.ordernum
)
where table1.status = 'open'
and not exists
(select 1
from table2 z
where z.ordernum = table1.ordernum
and z.status = 'open')
Update 2: Try this from your last comment. You are wanting to update the ZORDER table with the sum of all the line item prices. So you must UPDATE ZORDER, not ZORDERLINE. The total is found by summing EXTENDED_PRICE on all ZORDERLINE rows that match the order id. There may be some additional business logic needed in the first sub-query (eg., if you need to exclude certain statuses like canceled items), but this should get you very close.
UPDATE zorder
SET zorder.status = 3,
zorder.total =
(SELECT SUM(Y.EXTENDED_PRICE)
FROM ZORDERLINE Y
WHERE Y.order_id = zorder.order_id)
WHERE zorder.status = 1
and not exists
(select 1
from zorderline z
where z.order_id = zorder.order_id
and z.status = 1)

Microsoft SQL Trigger: Will multiple join records pointing to the same record fail to update?

Background:
I have a set of products for each numbered week starting at 1.
I have a product "library" which is week zero holding the last saved product from any time period.
I have a trigger that fires upon update or insert which keeps the "library" item up to date from the inserted items.
Since there can be duplicate products in the same event using a "sequence" field, my join will create multiple records to update from, which target the same library record.
Some questions:
Will these multiples fail the update command?
Is there a better way to update the single library product?
Code:
-- PK is ID, Week #, and Sequence #
update p set p.name = i.name
from product p join inserted i on
p.id = i.id and p.week = 0 and p.sequence = 1
Note: "inserted" can have multiple events. ID is like a UPC, Week is an identity, and Sequence is like an identity, but starts at 1 for each week. You can have a sequence of 2 while not having a sequence of 1 because they can delete products.
Sample Data:
ID WeekSequence Name
12345 1 3 Lego inserted first
12345 2 2 Lego Toy inserted second
12345 2 3 Lego Toy inserted second
Result data:
ID WeekSequence Name
12345 0 1 Lego Toy Was "Lego" now is "Lego Toy"
According to this BOL entry:
Use caution when specifying the FROM
clause to provide the criteria for the
update operation. The results of an
UPDATE statement are undefined if the
statement includes a FROM clause that
is not specified in such a way that
only one value is available for each
column occurrence that is updated,
that is, if the UPDATE statement is
not deterministic.
In other words, if an UPDATE statement uses a FROM clause that has multiple rows meeting the criteria to update a single row, it is unknown which row of new data will be used. In your sample data, it is unknown whether the result was updated from the name in the row with Sequence=2 or Sequence=3.
So, if it doesn't matter which row is used for the update, what you're currently doing will work just fine. If this is a problem however, you need to write your update's FROM and WHERE clauses so that only one row is returned for each item, possibly something like the following:
;with insert2 as (
select id, week, sequence, name,
row_number() over(partition by id order by week desc, sequence desc) as [descOrd]
from inserted
)
update p
set p.name = i.name
from product p
join insert2 i on p.id = i.id and p.week = 0 and p.sequence = 1
where i.descOrd=1