inner join after insert into temp table - sql

I can run two separate SQL statement where the first INSERT INTO a temp table and the second SQL statement runs an INNER JOIN between the temp table and another table.
I was trying to run the two statements in a single SQL statement but I am getting syntax error (using access).
first statement:
INSERT INTO temp SELECT id from t1 where a_column='Yes'
second statement:
SELECT * from t2 INNER JOIN t2.id = temp.id
Is there a way to run the two in a single statement?

These are two very different operations. You can't insert and output data all at once (at least not in Access).
If you are doing this through queries you'll need to make a second query. Each query in Access can only execute one statement.
You also need to specify the two tables you are joining together. Each statement is independent and has not bearing of reference for what came before.
SELECT *
FROM t2
INNER JOIN temp on t2.id = temp.id
Though depending on what you wish to accomplish (I'm not sure why you need a temp table) you might get away with this
SELECT *
FROM t2
INNER JOIN t1 ON t2.id = t1.id
WHEREt1.a_column='Yes'

Related

Multiple SELECTs in one query using same WITH

This query filters some IDs from a master table, and returns all data abpout that IDs from two other tables as two record sets:
WITH filteredIds (fid) AS
(
SELECT id
FROM MasterTable
WHERE <somecondition>
)
SELECT *
FROM Table1
RIGHT JOIN filteredIds ON (Table1.id = filteredIds.fid);
WITH filteredIds (fid) AS
(
SELECT id
FROM MasterTable
WHERE <somecondition>
)
SELECT *
FROM Table2
RIGHT JOIN filteredIds ON (Table2.id = filteredIds.fid);
This works so far, but it would be great to have just one WITH clause, since the condition is always the same. Further more, the condition is often written manually, since this is used to collect some diagnostics data.
But this
WITH filteredIds (fid) AS
(
SELECT id
FROM MasterTable
WHERE <somecondition>
)
SELECT *
FROM Table1
RIGHT JOIN filteredIds ON (Table1.id = filteredIds.fid);
SELECT *
FROM Table2
RIGHT JOIN filteredIds ON (Table2.id = filteredIds.fid);
does not work, SQL Server claims it does not know object filteredIds in the last query.
Have I overseen something, or doesn't it work that way? I guess the alternative would be a temporary table.
Once a CTE has been consumed in a query, it cannot be reused in another query. There is no direct workaround as far as I know. The closest thing perhaps to want you want would be to create a bona fide temporary table:
CREATE TABLE #temp (fid int);
INSERT INTO #temp (fid)
SELECT id FROM MasterTable WHERE <somecondition>;
Then, you may reuse the temporary table directly:
SELECT * FROM Table1 a RIGHT JOIN #temp b ON a.id = b.fid;
SELECT * FROM Table2 a RIGHT JOIN #temp b ON a.id = b.fid;
You can't do what you want with CTE. CTE doesn't exist outside the query where it was defined.
I think that the closest thing to what you want is called a "view".
CREATE VIEW filteredIds
AS
SELECT id AS fid
FROM MasterTable
WHERE <somecondition>
;
And now you can use that view in many other queries.
SELECT *
FROM Table1
RIGHT JOIN filteredIds ON (Table1.id = filteredIds.fid);
SELECT *
FROM Table2
RIGHT JOIN filteredIds ON (Table2.id = filteredIds.fid);
Furthermore, in SQL Server it will work exactly like a CTE in a sense that the list of filtered IDs is not materialized and the engine will optimize the whole query that references the view. It will inline the view definition into the outer / main query as if you wrote everything in one big query. Just like it would do with CTE.
With a temporary table you explicitly write this temporary result to disk and read it back.
In some cases it may be better, in some not.

Pass values as parameter from select query

I want to pass values from output of select query to another query. Basically both queries will be part of a stored procedure. e.g.
select Id, RelId
from tables
There will be multiple rows returned by above query and I want to pass them to the following query
select name
from table2
where Id = #Id and MgId = #RelId
Please suggest
You cannot pass multiple values in SQL.
But maybe you can just join your 2 tables, that would be far more efficient.
Not knowing your table schemes I suggest something like this. You might have to adapt this to your actual table schemas off course
select name
from table2 t2
inner join tables t on t2.Id = t.Id
and t2.MgId = t.RelId
EDIT
As Gordon mentioned in his answer, this approach can show double rows in your result.
If you don't want that than here are 2 ways of getting rid of the doubles
select distinct name
from ...
or by grouping by adding this at the end of the statement
group by name
Though this will work, avoiding the doubles like in Gordon's answer is better
I would suggest using exists:
select t2.name
from table2 t2
where exists (select 1
from tables t
where t2.Id = t.Id and t2.MgId = t.RelId
);
The difference between exists and join is that this will not generate duplicates, if there are multiple matches between the tables.
Or...
SELECT *
INTO #Table1
FROM ...
SELECT *
INTO #Table2
FROM ...
SELECT *
FROM #Table1 T1
JOIN #Table2 T2
DROP TABLE #Table1, #Table2

Overwrite a table using results from a query

I am using Sqlite3. What is the most efficient way to:
Execute a query to join two tables t1 and t2. The query is designed to keep all of the columns from t1, and just add columns from t2. It will either keep all rows (left outer join) or just keep the matching rows.
Using the result, update t1 in the database to look exactly like the results of the query.
Try this:
select a.*, b.hostname, b.mac
from tablea a left join
tableb b on a.ip_address = b.ip

Creating a table that includes only records that exist in one table without existing in another

I'm a newbie here and hoping someone can help with this sql. I've created two tables, one of which holds EVERY record, another which contains the records that I DON'T want in my table.
I tried joining them in the way I researched that is supposed to work, to include only records where they ARE NOT In the second table, but I'm getting an error.
The SQL is:
Create table t3 as
(Select * from t1
Left Outer join t2
on (t1.ID = t2.Orig_ID and t1.ID_Line = t2.Orig_ID_Line)
Where t2.Orig_ID is null
and t2.Orig_ID_Line is null)
This should be simple. However, i'm getting an error that says "Duplicate column name in Orig_ID"
HELP!
Thanks.
You were very close with your original statement, but forgot to limit the columns to those from t1, so you had twice as many columns as intended. Try:
CREATE TABLE t3 AS
(SELECT t1.* FROM t1 -- Key change * -> t1.*
LEFT OUTER JOIN t2
ON (t1.ID = t2.Orig_ID AND t1.ID_Line = t2.Orig_ID_Line)
WHERE t2.Orig_ID IS NULL
AND t2.Orig_ID_Line IS NULL)
Try this
Create table t3 as
select * from t1 where (t1.ID, t1.ID_Line) not in ( select t2.ID, t2.Orig_ID_Line from t2 where t2.ID is not null and t2.Orig_ID_Line is not null )
You get duplicate column name error, because you join and select both fields in both tables. So same column names return from the query and table can not have same column names. What you need is to select columns only in t1 table
select t1.* ......
But you do not need join operation. What you need is simple. Using "not in" operator is what you need. Have a look at an sql tutorial for in/not in operators.

Unable to understand query

I am working on an SSIS job that contains a complex query.
It has some thing like :
some sql statements
left outer join
(
select query joining two more tables )
table1
here, i am unable to understand what that table1 mean ? Is it a kind of temporary view
created . This table1 is used in the other parts of query . But, actually the table1 does
exists in the database.
Is it like , the results of the select query in the parenthesis is created as table1
Please clarify me on this..
I am not able to put down my code because of Security Policies
Here is SQL Fiddel example
Below is the sample query
Select Temp1.id,Table1.id Table1_id
from Temp1
left Outer join
(
Select Temp2.id
from Temp2
join Temp3
On Temp2.id = Temp3.id
) Table1
on Temp1.id = Table1.Id
In above example table1 is the Alias for data coming from joinsof two tables (temp2 and temp3)
table1 is an alisas your subquery. It's the name of subquery you can use with columns for example table1.col1
It is an alias for the query in the parenthesis.
If you would remove that you would get an error.
Aliases are also good when you have the same column in more than on joined tables, so you can distinquish them.
For instance if colX is both in Table1 and Table2 you would have a query like:
SELECT T1.colX,T2.colX
FROM Table1 T1
JOIN Table2 T2
ON T1.id = T2.id