Unable to understand query - sql

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

Related

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

SQL join to return a table with multiple columns from other tables replacing its own

I am trying to write an SQL query that will return Table1, which has 10 columns. This table consists of a primary key id, 4 foreign key Id columns, and 5 other columns that I want to return but not change. The goal is to do a join to replace the foreign key Ids with their descriptions that are held in other tables.
Here is one attempt with the first FK Id:
Select * from Table1 t1
left join Table2 t2
on t1.BranchId = t2.BranchId;
This left join returns the description from table2, but does not replace it.
Here is another with the first FK Id:
Select t2.BranchName from Table1 t1
left join Table2 t2
on t1.BranchId = t2.BranchId;
This returns the name I want, but does not return table1 fully.
For the sake of an example you could pretend that OtherName3, OtherName4, OtherName5 are in tables Table3, Table4, Table5, respectively.
This may seem trivial for experienced SQL devs, but I am having a hard time figuring out the syntax.
Thanks!
I'm not sure what you mean by replace it.
I think you just need to list out all the columns you want:
Select t1.col1, t1.col2, t1.col3, . . .,
t2.name
from Table1 t1 left join
Table2 t2
on t1.BranchId = t2.BranchId;
I don't know what you mean by 'replace' but you just need to qualify what columns from which table you want. That goes for all tables you are joined to, especially if they have the same column name in multiple tables. I put junk columns in since I don't know your tables but you should get the general idea.
Select t2.BranchName, t1.BranchId, t1.Name, t1.Amount, t2.BranchLocation from Table1 t1
left join Table2 t2
on t1.BranchId = t2.BranchId;
I think this is what you are looking for:
select t1.*, t2.BranchName from Table1 t1
left join Table2 t2
on t1.BranchId = t2.BranchId;
Return Table1 fully (all columns) and only the description (BranchName) from Table2.
If using SQL Server, see all syntax options for the SELECT clause here:
https://msdn.microsoft.com/en-us/library/ms176104.aspx

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.

SQL SELECT across two tables

I am a little confused as to how to approach this SQL query.
I have two tables (equal number of records), and I would like to return a column with which is the division between the two.
In other words, here is my not-working-correctly query:
SELECT( (SELECT v FROM Table1) / (SELECT DotProduct FROM Table2) );
How would I do this? All I want it a column where each row equals the same row in Table1 divided by the same row in Table2. The resulting table should have the same number of rows, but I am getting something with a lot more rows than the original two tables.
I am at a complete loss. Any advice?
It sounds like you have some kind of key between the two tables. You need an Inner Join:
select t1.v / t2.DotProduct
from Table1 as t1
inner join Table2 as t2
on t1.ForeignKey = t2.PrimaryKey
Should work. Just make sure you watch out for division by zero errors.
You didn't specify the full table structure so I will assume a common ID column to link rows in the tables.
SELECT table1.v/table2.DotProduct
FROM Table1 INNER JOIN Table2
ON (Table1.ID=Table2.ID)
You need to do a JOIN on the tables and divide the columns you want.
SELECT (Table1.v / Table2.DotProduct) FROM Table1 JOIN Table2 ON something
You need to substitue something to tell SQL how to match up the rows:
Something like: Table1.id = Table2.id
In case your fileds are both integers you need to do this to avoid integer math:
select t1.v / (t2.DotProduct*1.00)
from Table1 as t1
inner join Table2 as t2
on t1.ForeignKey = t2.PrimaryKey
If you have multiple values in table2 relating to values in table1 you need to specify which to use -here I chose the largest one.
select t1.v / (max(t2.DotProduct)*1.00)
from Table1 as t1
inner join Table2 as t2
on t1.ForeignKey = t2.PrimaryKey
Group By t1.v

How to join all columns from one table

I tried doing this but it failed.
SELECT table2.ID, table1.* FROM table2
LEFT JOIN table1 ON table1.ID = table2.table1ID
How do you select all columns from a table?
EDIT: There is no error in the above query. I don't know what caused the error but the code is now working.
You had field names conflict as both tables have ID field. You must to
SELECT table2.ID as t2_id, table1.* FROM table2
LEFT JOIN table1 ON table1.ID = table2.table1ID
What you have is syntactically correct, exactly what did you mean by it failed? Did you get an error message or just not the results you wanted? (BTW it is a bad practice to select *, only return the columns you need. In this case you do not need all the columns as the id field in table1 will have the exact same data as the file din table 2 it is joined to)
SELECT t2.ID, t1.* FROM table2 t2
LEFT JOIN table1 t1 ON t1.ID = t2.table1ID
this works on sql 2000+
If I am working inside a stored procedure where I have a defined #Table data type, there is no issue with using select table.* especially if I am using it for an output SELECT at the end. So the comment about production servers and network traffic in this case is meaningless as the entire stored procedure executes in memory. A select.* in this case is merely returning all the columns which have been defined ahead of time.