I have two tables table1, table2. Both the tables can be joined based on empID.
I have a new column in table1 called tabseqno. I want to update tabseqno of table1 with tabseqno from table2.
UPDATE TABLE1 SET TABLE1.TABSEQNO =TABLE2.TABSEQNO
WHERE TABLE1.EMPID= TABLE2.EMPID AND TABLE2.GROUPID=99
Either:
update table1 set table1.tabseqno =
( select table2.tabseqno from table2
where table2.empid = table1.empid
and table2.groupid = 99);
or:
update table1 set table1.tabseqno =
( select table2.tabseqno from table2
where table2.empid = table1.empid
and table2.groupid = 99)
where exists
( select table2.tabseqno from table2
where table2.empid = table1.empid
and table2.groupid = 99);
depending on what you want to happen if there is no matching table2 row for a table 1 row (the first statement will set table1.tabseqno to null, the second will not update those rows at all).
Both only work if the table2 subquery can only return a maximum of 1 row for any empid.
Related
I have two tables, called table1 and table2. Both have date and id as columns, and table2 is a small subset of table1 entries. How do I remove all entries that exist in table2 from table1 by matching up date and id?
select from table1 where not([]date;id)in table2
/ Begin with example input of 10 entries
q)t1:([]date:.z.d+til 10;id:til 10;c:til 10)
/ Pick 3 random entries to form key table t2
q)t2:3?delete c from t1
/ Key t1 by date and id and drop entries matching t2
q)t2 _ `date`id xkey t1
I have a table, on which I have added a new column nxvl.
I need to do something like this
UPDATE table1
SET nxvl = nexval('my_sequence_1')
from table2
where table1.col_id = table2.id and table2.val_col=1
order by table1.date
UPDATE table1
SET nxvl = nexval('my_sequence_2')
from table2
where table1.col_id = table2.id and table2.val_col=2
order by table1.date
Postgres doesn't allow order by on update
It has around 100,000 rows.
Can it be done with Update command. Right now there are only 2 values in val_col ie 1 & 2
Since it's a one time thing, I don't mind doing it with functions and looping through each row and update.
For knowledge wondering if there's a better way of achieving it.
Using Postgres 12.2
You can set the order and fetch the associated sequence value in a subquery, then update table (without even considering the update order)
UPDATE table1
SET nxvl = a.seq
FROM
( select table1.id, nexval('my_sequence_1') seq
from table1
join table2
on table1.col_id = table2.id
and table2.val_col=1
order by table1.date
) a
WHERE table1.id = a.id;
So I have this table: table
How do I obtain the cod_Armazem where it doesn't have any of the stocks at 0?
I am using SELECT cod_Armazem FROM table WHERE stock>0;
This should work
SELECT DISTINCT
t1.cod_Armazem
FROM table t1
WHERE NOT EXISTS(
SELECT *
FROM table
WHERE stock = 0
AND cod_Armazem = t1.cod_Armazem);
I am working on postgres query to remove duplicates from a table. The following table is dynamically generated and I want to write a select query which will remove the record if the first row has duplicate values.
The table looks something like this
Ist col 2nd col
4 62
6 34
5 26
5 12
I want to write a select query which remove either row 3 or 4.
There is no need for an intermediate table:
delete from df1
where ctid not in (select min(ctid)
from df1
group by first_column);
If you are deleting many rows from a large table, the approach with an intermediate table is probably faster.
If you just want to get unique values for one column, you can use:
select distinct on (first_column) *
from the_table
order by first_column;
Or simply
select first_column, min(second_column)
from the_table
group by first_column;
select count(first) as cnt, first, second
from df1
group by first
having(count(first) = 1)
if you want to keep one of the rows (sorry, I initially missed it if you wanted that):
select first, min(second)
from df1
group by first
Where the table's name is df1 and the columns are named first and second.
You can actually leave off the count(first) as cnt if you want.
At the risk of stating the obvious, once you know how to select the data you want (or don't want) the delete the records any of a dozen ways is simple.
If you want to replace the table or make a new table you can just use create table as for the deletion:
create table tmp as
select count(first) as cnt, first, second
from df1
group by first
having(count(first) = 1);
drop table df1;
create table df1 as select * from tmp;
or using DELETE FROM:
DELETE FROM df1 WHERE first NOT IN (SELECT first FROM tmp);
You could also use select into, etc, etc.
if you want to SELECT unique rows:
SELECT * FROM ztable u
WHERE NOT EXISTS ( -- There is no other record
SELECT * FROM ztable x
WHERE x.id = u.id -- with the same id
AND x.ctid < u.ctid -- , but with a different(lower) "internal" rowid
); -- so u.* must be unique
if you want to SELECT the other rows, which were suppressed in the previous query:
SELECT * FROM ztable nu
WHERE EXISTS ( -- another record exists
SELECT * FROM ztable x
WHERE x.id = nu.id -- with the same id
AND x.ctid < nu.ctid -- , but with a different(lower) "internal" rowid
);
if you want to DELETE records, making the table unique (but keeping one record per id):
DELETE FROM ztable d
WHERE EXISTS ( -- another record exists
SELECT * FROM ztable x
WHERE x.id = d.id -- with the same id
AND x.ctid < d.ctid -- , but with a different(lower) "internal" rowid
);
So basically I did this
create temp t1 as
select first, min (second) as second
from df1
group by first
select * from df1
inner join t1 on t1.first = df1.first and t1.second = df1.second
Its a satisfactory answer. Thanks for your help #Hack-R
I have two tables say Table1 and Table2 that contains the following column with which I should join and perform an update a column of Table1 with the value of the same column present in Table2.
Columns for Join condition:
Table1.mem_ssn and Table2.ins_ssn
Table1.sys_id and Table2.sys_id
Table1.grp_id and Table2.grp_id
Column to update:
Table1.dtofhire=Table2.dtofhire
I need a way to bulk update (using single update query without looping) the above mentioned column in Oracle 11G.
Table1 does not contain any key constraint specified since it will be used as a staging table for Data upload.
Please help me out to update the same.
You can use the MERGE statement.
It should look something like this:
MERGE INTO table1 D
USING (SELECT * FROM table2 ) S
ON (D.mem_ssn = S.ins_ssn and D.sys_id = S.sys_id and D.grp_id=S.grp_id)
WHEN MATCHED THEN
UPDATE SET D.dtofhire=S.dtofhire;
UPDATE:
Since you have more than one row in table2 with the same (ins_ssn,sys_id,grp_id) and you want the max dtofhire, you should change the query in the using clause:
MERGE INTO table1 D
USING (SELECT ins_ssn, sys_id, grp_id, max(dtofhire) m_dtofhire
FROM table2
GROUP BY ins_ssn,sys_id,grp_id) S
ON (D.mem_ssn = S.ins_ssn and D.sys_id = S.sys_id and D.grp_id=S.grp_id)
WHEN MATCHED THEN
UPDATE SET D.dtofhire=S.m_dtofhire;
The query that I used to arrive the functionality is seen below
UPDATE table1 T2
SET dtofhire = (SELECT Max(dtofhire) AS dtofhire
FROM table2 T1
WHERE T2.mem_ssn = T1.ins_ssn
AND T2.sys_id = T1.sys_id
AND T2.grp_id = T1.grp_id
GROUP BY ins_ssn,
sys_id,
grp_id)
WHERE ( mem_ssn, sys_id, grp_id ) IN (SELECT ins_ssn,
sys_id,
grp_id
FROM table2 );