Its just a basic cte with inner join and insert command dont know what is the mistake I did please help
My table t1
ID Name
1 A
2 B
3 C
My table t2
ID Name
1 A
2 B
What I'm trying to do is, inner join two table and take unmatched records. Insert the unmatched record into table t2. This is what I wrote
with scd(scdid,scdname)
as
(select t1.id as scdid, t1.name as scdname from t1 where t1.id not in (select t1.id from t1 inner join t2 on t1.id = t2.id))
insert into t2(id,name) select scd.scdid, scd.scdname from scd;
Something went wrong it says
SQL Error: ORA-00928: missing SELECT keyword
00928. 00000 - "missing SELECT keyword"
Please help, it must be simple mistake i guess
Edit: apologies for the mistake, yes I'm using Oracle server.
Here is the query:
with scd(scdid, scdname) as
(select t1.id as scdid, t1.name as scdname
from t1
where t1.id not in (select t1.id from t1 inner join t2 on t1.id = t2.id)
)
insert into t2(id, name)
select scd.scdid, scd.scdname
from scd;
This query looks syntactically correct for SQL Server. However, you have an Oracle error. Try writing the query as:
insert into t2(id, name)
select t1.id as scdid, t1.name as scdname
from t1
where t1.id not in (select t1.id from t1 inner join t2 on t1.id = t2.id);
Or better yet as:
insert into t2(id, name)
select t1.id as scdid, t1.name as scdname
from t1
where t1.id not in (select t2.id from t2);
Note that the Oracle syntax for a CTE with an insert is to place the with after the insert. However, a CTE is not needed for this.
EDIT: In Oracle syntax:
insert into t2(id, name)
with scd(scdid, scdname) as
(select t1.id as scdid, t1.name as scdname
from t1
where t1.id not in (select t1.id from t1 inner join t2 on t1.id = t2.id)
)
select scd.scdid, scd.scdname
from scd;
In Oracle, the with statement is part of a select.
Related
How can I do something like select * from T1 inner join T2 on (T1.ID=T2.ID OR T1.ID2=T2.ID)
When I execute this code, it seems to fall in a infinity loop so I guess I'm wrong.
In other words, how can I match one of two columns from T1 to one column from T2
T1
ID ID2
1 10
2 20
T2
ID value
1 dummy10
20 dummy20
Result
ID ID2 value
1 10 dummy10
2 20 dummy20
Try to do like this:
select *
from T1, T2
where T1.ID = T2.ID or T1.ID2 = T2.ID
you can use 2 select statements with union, like this:
select
t1.ID,
t1.ID2,
t2.value
from Table1 as t1
inner join Table2 as t2 on t1.ID = t2.ID
UNION
select
t1.ID,
t1.ID2,
t2.value
from Table1 as t1
inner join Table2 as t2 on t1.ID2 = t2.ID
/* this will exclude values selected by other statement */
where t1.ID2 not in (select ID2 from Table1 inner join Table2 on Table1.ID = Table2.ID)
The only issue I can see with the code you provide is that you have not specified from which table you want the common column ID to be selected:
proc sql;
select
t1.*
,t2.value
from t1
inner join t2
on t1.id = t2.id or t1.id2 = t2.id;
quit;
Otherwise, your code should work. Perhaps the size of the data being joined is the problem?
I have two tables t1 & t2. In t1, there are 1641787 records. In t2, there are 33176007 records. I want to take two columns from table2 and keep everything of t1. When I use left join with t1 to t2, I got more records than t1. I would like to get a similar number of records as t1 after joining. Please give me a suggestion. Here is my code:
SELECT t1.*,
t2.City
FROM t1
LEFT JOIN
t2
ON t1.ID = t2.ID;
You can aggregate and choose an arbitrary value:
select t1.*, t2.city
from t1 left join
(select t2.id, any_value(t2.city) as city
from t2
group by t2.id
) t2
on t1.id = t2.id;
select t.* from table1 t where t.id NOT IN(
select Id from t2 where usrId in
(select usrId from t3 where sId=value));
I the result i need is like if there are matching id's in t1 and t2 then those id's should be omitted and only the remaining rows should be given to me. I tried converting into join but it is giving me the result i wanted. Below is my join query.
SELECT t.* FROM table1 t JOIN table2 t2 ON t.Id <> t2.Id
JOIN table3 t3 ON t3.Id=t2.Id WHERE t3.sId= :value
This doesn't feth me the correct result. it was returning all the rows, but i want to restrict the result based on the matching id's in table t1 and table t2. Matching id's should be ommited from the result.I will be passing the value for sId.
I believe this to be an accurate refactor of your query using joins. I don't know if we can do away with the subquery, but in any case the logic appears to be the same.
select t1.*
from table1 t1
left join
(
select t2.Id
from table2 t2
inner join table3 t3
on t2.usrId = t3.usrId
where t3.sId = <value>
) t2
on t1.Id = t2.Id
where t2.Id is null
Let's break down and solve problem step by step.
So your query
select t.* from table1 t where t.id NOT IN(
select Id from t2 where usrId in
(select usrId from t3 where sId=value));
on converting the inner query to JOIN will yield
select t.* from table1 t where t.id NOT IN
(SELECT T2.ID FROM T2 JOIN T3 on T2.UsrID =T3.UsrID and T3.sID=value)
which on further converting to JOIN with outer table will be
select t.* from table1 t LEFT JOIN
(SELECT T2.ID FROM T2 JOIN T3 on T2.UsrID =T3.UsrID and T3.sID=value)t4
ON t.id =T4.ID
WHERE t4.ID is NULL
In case you completely want to remove sub-query you can try like this
SELECT t.*
FROM table1 t
LEFT JOIN T2
ON T.ID=T2.ID
LEFT JOIN T3
ON T3.UsrId=T2.UsrID AND T3.sId=value
WHERE T3.UsrID IS NULL
For example we have the following SQL query
select T1.name
from table1 T1
join table2 T2 ON T1.id = T2.id
I have made join table2 only if it is NOT empty. If table2 is empty I don't make join
select T1.name
from table1 T1
Can I solve it by one SQL query?
It would be good to use only SQL standard.
INSERT INTO dbo.VehiclesCategories ( VehicleId, CategoryId )
VALUES ( t1.ID , t2.ID)
SELECT t1.ID, t2.ID
FROM dbo.Vehicle t1
inner JOIN dbo.VehicleCategory t2 ON t1.Category = t2.Name
In the above SQL, I want to insert two values into a M:M join table. The SELECT statement works fine and I get the desired results. The INSERT statement throws the following error:
Msg 128, Level 15, State 1, Line 2
The name "t1.ID" is not permitted in this context. Valid expressions are constants, constant expressions, and (in some contexts) variables. Column names are not permitted.
How do I insert the two values (t1.ID and t2.ID) into the table?
That's because in the context of the INSERT, t1 and t2, don't exist.
If you combine those two queries, I think it will do exactly what you want:
INSERT INTO dbo.VehiclesCategories ( VehicleId, CategoryId )
SELECT t1.ID, t2.ID
FROM dbo.Vehicle t1
inner JOIN dbo.VehicleCategory t2 ON t1.Category = t2.Name
Do not include the VALUES (t1.ID, t2.ID) part at all.
Try this
INSERT dbo.VehiclesCategories ( VehicleId, CategoryId )
SELECT t1.ID, t2.ID
FROM dbo.Vehicle t1
JOIN dbo.VehicleCategory t2 ON t1.Category = t2.Name
just delete VALUES ( t1.ID , t2.ID) from insert into