update table from another table in db2 9 - db2

hi i run this script "
UPDATE t1
SET T1.col1= T2.col1
FROM aaa t1 ,
bbb t2
WHERE
T1.col2=138802
AND T1.col3 >=8800084 and T1.col3 <=8852884
AND T1.col4=0
AND T1.col5=T2.col2"
and i get syntax error !!! (ILLEGAL USE OF KEYWORD FROM)
how i can run this script???

Here's a modified version:
UPDATE aaa t1
SET T1.col1 = (SELECT T2.col1 FROM bbb t2 WHERE T1.col5=T2.col2)
WHERE T1.col2=138802 AND T1.col3 >=8800084 and T1.col3 <=8852884 AND T1.col4=0
I isolated the T2 stuff in a subquery with an explicit SELECT. Note that the subquery will run for every row that is updated.

Related

select count from both tables using join in postgesql

how to find number of records in both table using join.
i have two tables table1 and table2 with same structure.
table1
id
item
1
A
1
B
1
C
2
A
2
B
table2
id
item
1
A
1
B
2
A
2
B
2
C
2
D
Output should be like this.
id
table1.itemcount
table2.itemcount
1
3
2
2
2
4
SELECT DISTINCT id, (
SELECT COUNT(*) FROM table1 AS table1_2 WHERE table1_2.id=table1.id
) AS "table1.itemcount", (
SELECT COUNT(*) FROM table2 AS table2_2 WHERE table2_2.id=table1.id
) AS "table2.itemcount"
FROM table1;
Assuming that each id is guaranteed to exist in both tables, the following would work
select
t1.id,
count(distinct t1.item) t1count,
count(distinct t2.item) t2count
from t1
join t2 on t1.id = t2.id
group by 1;
But if that is not guaranteed then we'll have to use full outer join to get unique ids from both tables
select
coalesce(t1.id, t2.id) id,
count(distinct t1.item) t1count,
count(distinct t2.item) t2count
from t1
full outer join t2 on t1.id = t2.id
group by 1;
We're using coalesce here as well for id because if it only exists in t2, t1.id would result in null.
#DeeStark's answer also works if ids are guaranteed to be in both tables but it's quite inefficient because count is essentially run twice for every distinct id in the table. Here's the fiddle where you can test out different approaches. I've prefixed each query with explain which shows the cost
Hope this helps

Return the most recent value when joining two tables

I am trying to join two tables and return the most recent value for a field.
Currently, if aa.time_day does not equal bb.time, then the bb.time field returns null. I would like this to return the most recent value less than or equal to the aa.time_date value.
My query currently looks like this:
Select
aa.day_time
aa.name
aa.value
bb.name
bb.target_value
bb.time
FROM
x.table1 aa LEFT JOIN y.table2 bb
ON aa.name = bb.name AND aa.day_time=bb.time
WHERE aa.day_time = TO_DATE(‘01/01/2017’,’DD/MM/YYYY’)
Searching Stackoverflow and other websites showed me a number of solutions, unfortunately nothing worked. The query below is the closest I got to success as it did not throw up an error message, however it ran for several hours and I had to stop it. The query above worked in about 5 seconds.
Select
aa.day_time
aa.name
aa.value
bb.name
bb.target_value
bb.time
FROM
x.table1 aa LEFT JOIN y.table2 bb
ON aa.name = bb.name AND aa.day_time=
(SELECT MAX (bb.time)
FROM y.table2
WHERE bb.time <= aa.day_time)
WHERE a.day_time = TO_DATE(‘01/01/2017’,’DD/MM/YYYY’)
I'm not familiar with SQL, so thank you very much for your help in advance.
If this is oracle and there is a one to many relationship t1 to t2 then a using a cte to find the most recent date from t2 might do it
DROP TABLE T1;
DROP TABLE T2;
CREATE TABLE T1(DAY_TIME DATE,NAME VARCHAR(3), VALUE NUMBER);
CREATE TABLE T2(DAY_TIME DATE,NAME VARCHAR(3), VALUE NUMBER);
TRUNCATE TABLE T1;
INSERT INTO T1(day_time,NAME,VALUE) VALUES (to_date('2018-01-01','YYYY-MM-DD'),'aaa',10);
TRUNCATE TABLE T2;
INSERT INTO T2(day_time,NAME,VALUE) VALUES (to_date('2017-01-01','YYYY-MM-DD'),'aaa',10);
INSERT INTO T2(day_time,NAME,VALUE) VALUES (to_date('2018-02-01','YYYY-MM-DD'),'aaa',10);
SELECT * FROM T1;
SELECT * FROM T2;
WITH cte AS
(
select name,day_time,value
from T2
where T2.day_time = (select MAX(t3.DAY_TIME) FROM T2 t3 WHERE t3.DAY_TIME <= TO_DATE('2018-01-01','YYYY-MM-DD') and t3.name = t2.name)
)
SELECT t1.name,t1.day_time,t1.value,
cte.name,cte.day_time,cte.value
from t1
left join cte on t1.name = cte.name
where t1.day_time = to_date('2018-01-01','YYYY-MM-DD');
NAME DAY_TIME VALUE NAME DAY_TIME VALUE
---- ---------------------- ---------- ---- ---------------------- ----------
aaa 01-JAN-2018 00:00:00 10 aaa 01-JAN-2017 00:00:00 10
If there are no entries at all in t2 then the t2 side of the select will by empty.

SQL: Joining tables using wildcards

When I join 2 tables
select t1.col1, t1.col2, t2.col3
from t1
left join t2
on t1.col2=t2.col3
Then I don't get any duplicate rows. However, when I try joining
tables using wildcards:
select t1.col1, t1.col2, t2.col3
from t1
left join t2
on t1.col2 like '%'||t2.col3
Then I'll get duplicate values. I saw this post that I think is
getting me somewhere but I couldn't really understand the solution.
Joining 2 Tables using a wildcard
It says I can use exists to get rid of duplicate values. I also
don't really understand his query. Here is the query in the other
post:
select *
from tableA a
where exists (select 1 from tableB b where a.id like '%' + b.id + '%');
What does the select 1 from tableB do?
I'm using PostgreSQL
This is what I've tried even though I don't understand it and still gives me duplicates:
select t1.col1,t1.col2,t2.col3
from t1
left join t2
on t1.col2 like '%'||t2.col3
where exists (select 1 from t2 where t1.col2 like '%'||t2.col3)
Use the DISTINCT clause:
SELECT DISTINCT t1.col1, t1.col2, t2.col3
FROM t1
LEFT JOIN t2 ON t1.col2 LIKE '%'||t2.col3;

Update using left join in netezza

I need to perform a left join of two tables in netezza during an update. How can i achieve this ? Left join with three tables are working but not with two tables.
UPDATE table_1
SET c2 = t2.c2
FROM
table_1 t1
LEFT JOIN table_2.t1
ON t1.c1=t2.c1
LEFT JOIN table_3 t3
ON t2.c1=t3.c1
this works but
UPDATE table_1
SET c2 = t2.c2
FROM table_1 t1
LEFT JOIN table_2.t1
ON t1.c1=t2.c1
this says like trying to update multiple columns.
Thanks,
Manirathinam.
When performing an UPDATE TABLE with a join in Netezza, it's important to understand that the table being updated is always implicitly INNER JOINed with the FROM list. This behavior is documented here.
Your code is actually joining table_1 to itself (one copy with no alias, and one with t1 as an alias). Since there is no join criteria between those two versions of table_1, you are getting a cross join which is providing multiple rows that are trying to update table_1.
The best way to tackle an UPDATE with an OUTER join is to employ a subselect like this:
TESTDB.ADMIN(ADMIN)=> select * from table_1 order by c1;
C1 | C2
----+----
1 | 1
2 | 2
3 | 3
(3 rows)
TESTDB.ADMIN(ADMIN)=> select * from table_2 order by c1;
C1 | C2
----+----
1 | 10
3 | 30
(2 rows)
TESTDB.ADMIN(ADMIN)=> UPDATE table_1 t1
SET t1.c2 = foo.c2
FROM (
SELECT t1a.c1,
t2.c2
FROM table_1 t1a
LEFT JOIN table_2 t2
ON t1a.c1 = t2.c1
)
foo
WHERE t1.c1 = foo.c1;
UPDATE 3
TESTDB.ADMIN(ADMIN)=> select * from table_1 order by c1;
C1 | C2
----+----
1 | 10
2 |
3 | 30
(3 rows)

left join from a group of tables to another group of tables in postgresql

I have some tables say t1 , t2 , t3.
I need to implement something like this in postgresql.
select * from (t1 , t2) left join t3
where t1.some_column = t3.some_column;
But postgresql complains
ERROR: syntax error at or near "," SQL state: 42601 Character: 77
You can't use from (t1,t2), you have to join them in some way.
Try something like this:
select * from t1
inner join t2 on t1.someColumn=t2.someColumn
left join t3 on t1.some_column = t3.some_column;