I want to fill all columns in one table basing on columns from select with left join from two others:
update TAB1 as P
set P.COL1 = (
select CODE from (
select * from TAB2 as A left outer join TAB3 as T on A.TAGID = T.ID
) as O
where P.ACTID = O.ACTID
);
It works properly on Oracle, but when i want to execute it on h2 I got this error:
Duplicate column name "ID"; SQL statement
I dont know where is a problem. I could'nt find any solution for that.
Thx for the answers
This statement is your problem:
(select * from TAB2 as A left outer join TAB3 as T on A.TAGID = T.ID)
Presumably, you have an ID in both tables, so the SELECT * returns two columns named ID. I'm surprised this works in Oracle -- but perhaps Oracle optimizes the code because the IDs are not needed.
Just return the value you want:
(select ?.CODE from TAB2 as A left outer join TAB3 as T on A.TAGID = T.ID)
The question mark is either A or T, depending on which table the value comes from.
Related
I would like to combine two tables.
The first table (tbl1) contains all Articles I need.
The second table (tbl2) contains some additional information - but not for every article.
That means in tbl2 are some columns where there is no value.
I am using the following join:
SELECT *
FROM tbl1
LEFT JOIN tbl2 ON tbl1.c4 = tbl2.C4
this join filters all articles, where tbl2.c4 = ''.
But I need the total articles that are listed in tbl1.
How can I manage that?
It is based on Oracle
You can use window functions. I think:
SELECT *
FROM (SELECT t1.*, COUNT(*) OVER () as cnt
FROM tbl1
) t1 LEFT JOIN
tbl2
ON t1.c4 = tbl2.C4;
These queries both get results:
SELECT * FROM Table1 WHERE Criteria = '5'
SELECT * FROM Table1 WHERE Criteria = '3'
This query gets results:
SELECT *
FROM Table1 p, Table2 m
WHERE p.UID = m.ID
AND Criteria = '5'
This query does not:
SELECT *
FROM Table1 p, Table2 m
WHERE p.UID = m.ID
AND Criteria = '3'
I am trying to convert these to a proper join which returns results even if there are no records in the right table.
I have tried the following
SELECT *
FROM Table1 p LEFT JOIN Table2 m ON p.UID = m.ID
WHERE p.Criteria = '3'
AND m.OtherCriteria = 'Moron'
--0 results
My limited understanding was that LEFT join is what I needed. I want data from the left table even if there is no data in the right table that matches. Since this didn't work I also tried right join, left outer join, right outer join and full join. None returned results.
What am I missing?
This is too long for a comment. Your query:
SELECT *
FROM Table1 p LEFT JOIN
Table2 m
ON p.UID = m.ID AND p.Criteria = '3';
Should be returning a row for all rows in table1. If there is no match, then the values will be NULL for table2. This is easily demonstrated: Here is a MySQL example on SQL Fiddle. Because this is standard behavior, it should work on almost any database.
Note that this query is quite different from this one:
SELECT *
FROM Table1 p LEFT JOIN
Table2 m
ON p.UID = m.ID
WHERE p.Criteria = '3';
This query returns no rows, because no rows match the WHERE clause. The filtering happens (conceptually) after the LEFT JOIN.
I changed the code in the SQL Fiddle slightly, so that query is:
select *
from (select 5 as criteria, 1 as id union all
select 6, 1 union all
select 7, 2
) table1 left join
(select 1 as id, 'x' as x
) table2
on table1.id = table2.id and criteria = 3;
As a note: you should always use explicit join syntax. Simple rule: Never use commas in the FROM clause.
If your database is returning no rows, then it is behaving in a non-standard manner or your interface has decided to filter the rows for some reason.
Best way is to explain in pseudo code
How do I Get x,
When table1.activity == "some_string"
Then x = table1.line_number in that same row.
I'm doing an INNER JOIN and I'm doing checks on table 2. Basically I don't want to join that row if table1.activity == "some_string"
well it's not mentioned table2 in your pseudo code
but you could filter values in the inner join ON statement or WHERE statement of the query. It depends what you want By example, (In where Section)
SELECT * FROM table1 AS pivot
INNER JOIN table2 USING(id)
WHERE pivot.activity <>'not_want_these_kind_of_Records';
Or in ON Section
SELECT * FROM table1 AS pivot
INNER JOIN table2 AS t2 ON t2.id=pivot.id
AND t2.activity <>'not_want_these_kind_of_Records';
The second One filter the results before join to the pivot table
Regards
I am trying to join on whether a string(a column from table 1) is present in list of strings(a column from table 2) in Hive QL. Can anyone please help me with the syntax.
SELECT
A.id
FROM tab1 A
inner join tab2 B
ON (
(array_contains(B.purchase_items, A.item_id) = true )
)
Above SQL does not work.
First, unless Hive QL is backwards, your query is wrong upfront:
SELECT A.ID FROM A tab1
will return nothing because you've declared table "A" as "tab1". Either reverse the Alias or correct the table alias reference: (I assume tab1 is the table name, so go with option 1)
SELECT A.ID from tab1 A
--OR
SELECT tab1.id from A tab1
Second, joins do not work based on conditional criteria, they ARE the conditional criteria. Sort of...
For example:
SELECT A.ID
FROM tab1 A
INNER JOIN tab2 B
ON A.item_id = B.purchase_item
is almost like doing a simple cross join with a WHERE condition:
SELECT A.ID
FROM tab1 A, tab2 B --better to use it straight as "FROM tab1 A cross join tab2 B"
WHERE a.item_id = b.purchase_item
You can use LEFT SEMI JOIN, which would retrieve rows from left side table with columns matched from right side table.
SELECT A.id FROM tab1 A
LEFT SEMI JOIN tab2 B
ON A.col1 = B.col1 AND <any-other-join-cond>;
Note that the SELECT and WHERE clauses can’t reference columns from the right hand table.
I'm not a SQL Server expert and I'm struggling with this query. Can anyone help?
DELETE
FROM PPTMAILLISTC.dbo.emailTables
WHERE email IN (SELECT *
FROM PPTMAILLISTC.dbo.emailTables tab1
INNER JOIN PPTMAILLISTAB.dbo.emailTables tab2
ON tab1.email = tab2.email)
SQL Server Management Studio returns.
Msg 116, Level 16, State 1, Line 1
Only one expression can be specified in the select list when the subquery is not introduced with EXISTS.
Basically, there are 2 separate tables both called the same (dbo.emailTables) in 2 separate databases (PPTMAILLISTC and PPTMAILLISTAB).
Where the both databases have the same results (which I can find out using the join i.e.)
SELECT *
FROM PPTMAILLISTC.dbo.emailTables tab1
INNER JOIN PPTMAILLISTAB.dbo.emailTables tab2
ON tab1.email = tab2.email
I want to delete the results of this join from PPTMAILLISTC.dbo.emailTables.
You can get rid of the use of IN and just use your inner SELECT statement and convert it to a DELETE and just reference the ALIAS (tab1) of the table you actually want to affect like this:
DELETE tab1
FROM PPTMAILLISTC.dbo.emailTables tab1
INNER JOIN PPTMAILLISTAB.dbo.emailTables tab2 ON tab1.email = tab2.email
You need something like this:
DELETE FROM PPTMAILLISTC.dbo.emailTables
WHERE email IN (SELECT tab1.email
FROM PPTMAILLISTC.dbo.emailTables tab1 INNER JOIN
PPTMAILLISTAB.dbo.emailTables tab2
ON tab1.email = tab2.email
);
Even if both tables have only one column, the * resolves to two columns tab1.email and tab2.email. As the error message says, the select list for an in subquery can have only one column.
EDIT:
This is actually simpler to write as:
DELETE FROM PPTMAILLISTC.dbo.emailTables
WHERE email IN (SELECT tab2.email
FROM PPTMAILLISTAB.dbo.emailTables tab2
);
You don't need to do the join in the in subquery.