I am trying to update one filled with a lot of data.
This is my table
I want to update NameAlias with new names for Major but this is my question if I have a lot of major names and ids how can I write a query that is good and short?
I know it should be something like this for one column.
Update Major Set NameAlias='Mathemathic' where IdMajor=1;
But what should I do if I have a lot of data for one field?
I want my result to be something like this:
You can use case when expression -
Update Major Set NameAlias=
case when namemajor='Math' then 'Mathemathic'
when namemajor='Computer' then 'Software'
when namemajor='Art' then 'Painthing'
when namemajor='History' then 'FranceHistory'
when namemajor='Music' then 'Piano' end
You should create a temp table containing the id, NameAlias accordingly like below.
select Id, NameAlias
into #TempTable
from (
values(1, 'Mathemathic'),
(2, 'Software'),
(3, 'Painthing')
-- The rest of values
)v(Id, NameAlias)
Then you can update the Major table
Update m
SET m.NameAlias = t.NameAlias
From Major m
INNER JOIN #TempTable t on m.IdMajor = t.Id
There is no need to create a temporary table to do this. Just use a derived table in the update query:
update m
set m.NameAlias = v.newAlias
from Major m join
(values(1, 'Mathematic'),
(2, 'Software'),
(3, 'Painting')
) v(idMajor, newAlias)
on m.IdMajor = v.IdMajor;
Related
I have a question about the SQL Query.
Basically, I would like to get a sample value from the testCategory = compareTestCat and testType = compTestType.
I am not really sure if we can compare the first row with the fourth row within the same query.
Is there any way that I can do this?
Try SELF JOIN to compare values within the same table.
Try the Following:
SELECT A.*
FROM YourTable A
JOIN YourTable B ON A.testCategory = B.compareTestCat and A.testType = B.compTestType
A and B are the different alias names for the same table.
Based on the very sparse information you provided, this looks like what you need:
SELECT *
FROM
TestTable
WHERE
testCategory = compareTestCat
AND testType = compTestType;
UPDATE
If you are interested in comparing different rows in the same table with each other, then you will need to go with a self join on the table. Examples of solutions for this have already been provided by others.
I need to select entire columns from table compression_query_table into Tenth_mile but INSERT deletes columns already in that Table that aren't in Compression_query_Table and I cannot figure out how to use UPDATE on multiple columns at the same time.
Considering your session_name is unique, you could probably write two separate statements: INSERT and UPDATE for this:
Update Statement:
UPDATE Tenth_mile
INNER JOIN compression_query_table cq on Tenth_mile.session_name = cq.session_name
SET Tenth_mile.State = cq.State,
Tenth_mile.route_number = cq.route_number;
Insert Statement:
INSERT INTO Tenth_mile (session_name, state, route_number)
SELECT session_name
,STATE
,route_number
FROM compression_query_table cq
WHERE NOT EXISTS
(SELECT tm.session_name
FROM Tenth_Mile tm
WHERE tm.session_name = cq.session_name
);
Note: I don't have MS-Access installed on my machine, so I don't have
a way to test this. Also, I would run the update before running the insert statement (aka upsert).
Hope this helps!
I am trying to insert data into a table in Oracle database. Data already exists, but not all of the data, and I can't just delete the data and reinsert it all. Is there a way to insert data into the table (without knowing what data I am missing). My script is running, but no data is actually inserting (and I do know there is data missing. I intentionally took data out to test its re-insertion.)
Insert into item (item, descr)
select distinct a.SUBORD, a.SUBORD_DESCR FROM EXIDE.UDT_BOM a, item b
where b.item = a.subord and not exists
(select b.item from item b, exide.udt_bom a where a.subord = b.ITEM)
If I follow what you're doing, you can use the merge statement for this:
merge into item i
using (select subord, subord_descr from exide.udt_bom) u
on (i.item = u.subord)
when not matched then insert (item, descr) values (u.subord, u.subord_descr);
SQL fiddle demo.
This also has the advantage that if the udt_bom has new descriptions for existing items, you can update those in the item table too:
merge into item i
using (select subord, subord_descr from exide.udt_bom) u
on (i.item = u.subord)
when matched then update set descr = u.subord_descr
when not matched then insert (item, descr) values (u.subord, u.subord_descr);
Another fiddle.
You have too many references to too many tables. With the not exists clause, the query does not need explicit joins:
Insert into item(item, descr)
select distinct b.SUBORD, b.SUBORD_DESCR
FROM EXIDE.UDT_BOM b
where not exists (select i.item from item i where b.subord = i.ITEM);
If there are no duplicates, in udt_bom, I would get rid of the distinct as well. And, queries are more readable when you use table abbreviations as aliases, rather than meaningless letters like a, b, and so on.
Is there any way to do some kind of "WITH...UPDATE" action on SQL?
For example:
WITH changes AS
(...)
UPDATE table
SET id = changes.target
FROM table INNER JOIN changes ON table.id = changes.base
WHERE table.id = changes.base;
Some context information: What I'm trying to do is to generate a base/target list from a table and then use it to change values in another table (changing values equal to base into target)
Thanks!
You can use merge, with the equivalent of your with clause as the using clause, but because you're updating the field you're joining on you need to do a bit more work; this:
merge into t42
using (
select 1 as base, 10 as target
from dual
) changes
on (t42.id = changes.base)
when matched then
update set t42.id = changes.target;
.. gives error:
ORA-38104: Columns referenced in the ON Clause cannot be updated: "T42"."ID"
Of course, it depends a bit what you're doing in the CTE, but as long as you can join to your table withint that to get the rowid you can use that for the on clause instead:
merge into t42
using (
select t42.id as base, t42.id * 10 as target, t42.rowid as r_id
from t42
where id in (1, 2)
) changes
on (t42.rowid = changes.r_id)
when matched then
update set t42.id = changes.target;
If I create my t42 table with an id column and have rows with values 1, 2 and 3, this will update the first two to 10 and 20, and leave the third one alone.
SQL Fiddle demo.
It doesn't have to be rowid, it can be a real column if it uniquely identifies the row; normally that would be an id, which would normally never change (as a primary key), you just can't use it and update it at the same time.
I've tried searching for this particular topic here, but haven't found the answer... Anyway, my aim is to update table (let's call it t_item), specifically column owner_id with values depending on another table (t_item_geo which is in turn linked to t_geo).
I'm not entirely sure whether the syntax below is actually valid for update statements.
UPDATE t_item SET owner_id= 6993 WHERE t_item.owner_id in
(SELECT t_item.owner_id FROM
t_item,
t_item_geo,
t_geo
WHERE
t_item.id = t_item_geo.item_id and
t_item_geo.geo_id = t_geo.id and
t_item.owner_id in (SELECT id FROM t_user WHERE network_id='fffffff') and
t_geo.id in (SELECT id FROM t_geo WHERE full_name = 'yyyyyyy')
);
Anyway, my problem with this query is that it updates far more rows than it should - if I separate just the select statement Oracle returns ~750 rows but the udpate itself updates more than 4000 rows. It's almost as if the condition was completely ignored - which would point me to perhaps incorrect syntax.
I need to update specific value in the table based on the select from few other 'joined' tables. Hope it makes sense.
Thanks for any contribution!
UPDATE: sorry - maybe it wasn't clear from the question itself, but the correct number of edited items should be ~750 and not ~4000. Thanks!
try this
MERGE INTO t_item
USING
(
SELECT t_item.owner_id FROM
t_item,
t_item_geo,
t_geo,
t_item.rowid rowid_sub
WHERE
t_item.id = t_item_geo.item_id and
t_item_geo.geo_id = t_geo.id and
t_item.owner_id in (SELECT id FROM t_user WHERE network_id='fffffff') and
t_geo.id in (SELECT id FROM t_geo WHERE full_name = 'yyyyyyy')
) on (rowid = rowid_sub)
WHEN MATCHED THEN
UPDATE SET owner_id= 6993;