How do I update every row of a particular column with values from another column? - sql

I am trying to update a column called software_id with a possible three values from a lookup table. So update user softwares software id column with the values from the softwares table of the id column. But there are only three rows in the softwares table. In the user softwares table there are over 1000. But when I run the query below only three rows get updated and the rest are left as null.
So the softwares id column is number and goes 1,2,3 and the user_sofwares software_id column is all null.
When I run the following query.
UPDATE user_software c
SET sotware_id = (
SELECT DISTINCT id
FROM softwares
WHERE id = c.id
);
Only the first three rows of the destination table get updated. I want every row on the user_softwares column to be updated with either 1,2 or 3. So it should be 1,2,3,1,2,3 for example.
I keep getting this error whenever i tried to update all rows
Error report - ORA-01427: single-row subquery returns more than one row

You can do this with a MERGE statement:
MERGE INTO (SELECT software_id, 1, ora_hash(ROWID, 2) + 1 AS fake_id
FROM user_software) u_soft
USING (SELECT DISTINCT id
FROM softwares) sftw
ON (sftw.id = u_soft.fake_id)
WHEN MATCHED THEN UPDATE SET u_soft.software_id = sftw.id;
(considering your matches are unique)

Related

Get the "most" optimal row in a JOIN

Problem
I have a situation in which I have two tables in which I would like the entries from table 2 (lets call it table_2) to be matched up with the entries in table 1 (table_1) such that there are no duplicates rows of table_2 used in the match up.
Discussion
Specifically, in this case there are datetime stamps in each table (field is utcdatetime). For each row in table_1, I want to find the row in table_2 in which has the closed utcdatetime to the table 1 utcdatetime such that the table2.utcdatetime is older than the table_1 utcdatetime and within 30 minutes of the table 1 utcdatetime. Here is the catch, I do not want any repeats. If a row in table 2 gets gobbled up in a match on an earlier row in table 1, then I do not want it considered for a match later.
This has currently been implemented in a Python routine, but it is slow to iterate over all of the rows in table 1 as it is large. I thought I was there with a single SQL statement, but I found that my current SQL results in duplicate table 2 rows in the output data.
I would recommend using a nested select to get whatever results you're looking for.
For instance:
select *
from person p
where p.name_first = 'SCCJS'
and not exists (select 'x' from person p2 where p2.person_id != p.person_id
and p.name_first = 'SCCJS' and p.name_last = 'SC')

Update one column where another column within same table equals a column in another table

I have a Query for retrieving data from one table where one column equals one specific value and one column equals a column in another table.
Like this:
SELECT a.IdOrdre, b.FR_Ordre
FROM Ordre a
INNER JOIN OrdreKategori b ON a.IdOrdre=b.FR_Ordre
WHERE b.FR_Kategori=57
I now would like to update the column FR_Kategori to another value than 57. How would I structure the UPDATE Query in order to achieve this?
Since you didn't tag your DBMS, and every one has different syntax for UPDATE..JOIN , here is an ANSI-SQL answer:
UPDATE OrdreKategori t
SET t.fr_kategori = Other_Val
WHERE EXISTS(SELECT 1 FROM Ordre s
where s.IdOrdre=t.FR_Ordre)
AND t.fr_kategori = 57

SQL INNER JOIN vs. WHERE ID IN(...) not the same results

I was surprised by the outcome of these two queries. I was expecting same from both. I have two tables that share a common field but there is not a relationship set up. The table (A) has a field EventID varchar(10) and table (B) has a field XXNumber varchar(15).
Values from table B column XXNumber are referenced in table A column EventID. Even though XXNumber can hold 15 chars, none of the 179K rows of data is longer than 10 chars.
So the requirement was:
"To avoid Duplicate table B and table A entries, if the XXNumber is contained in a table A >“Event ID” number, then it should not be counted."
To see how many common records I have I ran this query first - call it query alpha"
SELECT dbo.TableB.XXNumber FROM dbo.TableB WHERE dbo.TableB.XXNumber in
( select distinct dbo.TableA.EventId FROM dbo.TableA )
The result was 5322 rows.
The following query - call it query delta which looks like this:
SELECT DISTINCT dbo.TableB.XXNumber, dbo.TableB.EventId
FROM dbo.TableB INNER JOIN dbo.TableA ON dbo.TableB.XXNumber= dbo.TableB.EventId
haas returned 4308 rows.
Shouldn't the resulting number of rows be the same?
The WHERE ID IN () version will select all rows that match each distinct value in the list (regardless of whether you code DISTINCT indide the inner select or not - that's irrelevant). If a given value appears in the parent table more than once, you'll get multipke rows selected from the parent table for that single value found in the child table.
The INNER JOIN version will select each row from the parent table once for every successful join, so if there are 3 rows in the child table with the value, and 2 in the parent, then there will be 6 rows rows in the result for that value.
To make them "the same", add 'DISTINCT' to your main select.
To explain what you're seeing, we'd need to know more about your actual data.

Update with multiple rows

i am using UPDATE to update all records in the table with value from another table:
UPDATE x.ADR_TEMP SET LOCATIONID = (SELECT ID FROM x.OBJECTRELATIONSHIP WHERE PROPERTYCLASS = 'PHYSICALLOCATION')
in both tables are the same number of records. I just need to read id from one table and put them into another.
I have error: ORA-01427: single-row subquery returns more than one row
I am new in this subject and suspect that solution is very simple. Please help me
UPDATE x.ADR_TEMP SET LOCATIONID = (
SELECT ID
FROM x.OBJECTRELATIONSHIP
WHERE PROPERTYCLASS = 'PHYSICALLOCATION' AND x.ADR_TEMP.relatedKey = x.OBJECTRELATIONSHIP.relatedKey )
if your tables are related to each other with some way like this(relatedKey is a column which relates to the second table mentioned), you can try this one.(not sure or perfect)

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