I need to insert non-duplicated values... This "merge with self-reference" is not working as expected, but not generated a error message... Can I use similar thing?
MERGE INTO mydb.mytab AS Tref
USING mydb.mytab AS T ON Tref.id=T.id
WHEN NOT MATCHED THEN INSERT VALUES (123, 'etc');
(update but not change the question) Seems that other construction, as suggested here, is
INTO INTO mydb.mytab
SELECT t1.*
FROM ( select stack(2, 123,'ect1', 20,'etc2') as (id,etc) ) t1
LEFT OUTER JOIN mydb.mytab t2 ON t1.id = t2.id
WHERE t1.id is null
;
it is not elegant (there are other?) but it is working (!). There is some alternative with MERGE?
Merge will insert only if mydb.mytab does not contain matching rows
MERGE INTO mydb.mytab AS Tref
USING ( select stack(2, 123,'ect1', 20,'etc2') as (id,etc) ) AS T ON Tref.id=T.id
WHEN NOT MATCHED THEN INSERT VALUES (t.id, t.etc) --columns positions should match those in Tref
;
Related
Say I've already created table3, and try to insert data into it using the following code
WITH table1
AS
(SELECT 1 AS key, 'One' AS value),
table2
AS
(SELECT 1 AS key, 'I' AS value)
INSERT TABLE table3
SELECT t1.key, t1.value, t2.value
FROM table1 t1
JOIN table2 t2
ON (t1.key = t2.key)
However, I got an error as cannot recognize input 'insert' in select clause. If I simply delete the insert sentence, then the query runs just fine.
Is this a syntax problem? Or I cannot use with clause to insert?
Use INTO or OVERWRITE depending on what you need:
INSERT INTO TABLE table3 --this will append data, keeping the existing data intact
or
INSERT OVERWRITE TABLE table3 --will overwrite any existing data
Read manual: Inserting data into Hive Tables from queries
I have two tables T1,T2 I have to add the column id of T1 in T2 and update a value in T1.
So I am able to get the values from T2 using RETURNING,but when using values in UPDATE WHERE then not able to UPDATE the column
Example:
WITH "T1S" AS (INSERT INTO "T1" VALUES()RETURNING id AS "T1id" ),
"T2S" AS (INSERT INTO "T2"(t1_id) VALUES(
(SELECT "T1id" FROM "T1S"))
)RETURNING t1_id AS "T1id",t2_id AS "T2id"
)
UPDATE "T1" set value=t2_id WHERE id IN (SELECT t1_id FROM "T2S")
Image of query
If I understand correctly, this is the logic that you want:
WITH T1S AS (
INSERT INTO "T1"
VALUES ()
RETURNING id AS T1id
),
T2S AS (
INSERT INTO "T2" (t1_id)
SELECT T1ID
FROM T1S
RETURNING *
)
UPDATE "T1" t1
SET value = T2S.t2_id
FROM T2S
WHERE t1.id = T2S.t1_id;
Notes:
Using double quotes for table and column names is a bad practice. Using them for CTEs and column aliases is an absurdity. They just make references harder.
The solution is that you want the join the table to the CTE in the final UPDATE.
In a DB2-400 SQL join, can the USING() clause be used with one or more AND ON clauses for a single join..? This is for a situation where some field names are the same, but not all, so USING() would only apply to part of the join.
I could have sworn I've done this before and it worked, but now it eludes me.
I've tried various combinations as shown below, but none of them work. Perhaps I'm simply mistaken and it's not possible:
SELECT * FROM T1 INNER JOIN T2 USING (COL1,COL2) AND ON (T1.COL3=T2.COL4)
SELECT * FROM T1 INNER JOIN T2 ON (T1.COL3=T2.COL4) AND USING (COL1,COL2)
SELECT * FROM T1 INNER JOIN T2 ON (T1.COL3=T2.COL4), USING (COL1,COL2)
SELECT * FROM T1 INNER JOIN T2 USING (COL1,COL2,(1.COL3=T2.COL4))
Checking the syntax diagram here https://www.ibm.com/support/knowledgecenter/en/ssw_ibm_i_72/db2/rbafzjoinedt.htm
I suggest that the only options for JOIN USING are a comma separated list of columns
JOIN table-reference USING ( column-name [, column-name] ... )
and you can't mix USING with ON
You can use where:
SELECT *
FROM T1 INNER JOIN
T2 USING (COL1, COL2)
WHERE T1.COL3 = T2.COL4;
Another alternative would be to use a subquery to rename the column in one of the tables.
I want to insert data into a table from a select. This works fine so far...
INSERT INTO table_2
SELECT t.id, 1
FROM table_1 t
WHERE t.title LIKE '%search%';
But when I run this a second time, the statement raises an exception, because some of the rows already exist.
What can I do to get around this?
Thanks for your help,
Urkman
You can insert rows where they don't already exist, by adding that as a clause.
insert into table_2
select t.id, 1
from table_1 t
where t.title like '%search%'
and not exists (select t2.id from table_2 t2 where t2.id = t.id);
I'm trying to build a query to grab all rows from Table 1, then check if a corresponding row exists in Table 2, and if so, pull some data, JOIN and insert into Table 3. This will make more sense in pseudocode - so here's what I'm trying to do, procedurally, I'm just not sure how to implement this in PL/SQL.
For all rows in T1:
For all rows in T2:
if (T1.mycolumn - T2.othercolumn) > 0:
INSERT INTO T3 (foo, bar) VALUES T1.foo, T2.bar
Thanks!
It sounds like you are describing something like
INSERT INTO t3( foo, bar )
SELECT t1.foo,
t2.bar
FROM t1
LEFT OUTER JOIN t2 ON (t1.mycolumn > t2.othercolumn)
I'm not sure that's actually what you want, though. Some sample data and the expected output would be helpful. In what you're describing, t1.foo values may get inserted many times into t3 because they match many different rows in t2. Normally, there would be some sort of key that is common to t1 and t2 that you would use to determine whether an appropriate t2 row exists and the t1.mycolumn > t2.otherColumn condition would just be another predicate
INSERT INTO t3( foo, bar )
SELECT t1.foo,
t2.bar
FROM t1
LEFT OUTER JOIN t2 ON ( t1.someKey = t2.someKey
AND t1.mycolumn > t2.othercolumn)
insert into T3 (foo, bar)
select T1.foo, T2.bar
from T1, T2
where T1.mycolumn - T2.othercolumn > 0