Adding a variable to a join table - sql

I'm struggling to figure out how I can add a convenience data column to a join table to use for evaluation.
Sorry if the terminology is incorrect, I'm a bit of an SQL newbie.
For this query structure:
SELECT t1.id
FROM
table1 t1
INNER JOIN
table2 t2 ON t1.c2 = t2.c1
WHERE
t1.c5=5;
...I would like to add a column in the join table that is calculated using a function of a few of t1s columns. For example, the sum of t1.x, t1.y, t1.z in a variable called score that can then be referenced in the WHERE clause.
The reason behind wanting to do this is it will be referenced multiple times and will reduce verbosity and help readability.
I presume it will look something like:
SELECT t1.id
FROM
table1 t1
INNER JOIN
table2 t2 ON t1.c2 = t2.c1
-- function(t1.x, t1.y, t1.z) as score
WHERE
t1.c5=5;
--AND score ...
Using PostgreSQL 9.3

If the score is calculated from the t1 columns, then use a subquery on t1:
SELECT t1.id
FROM (select t1.*, function(t1.x, t1.y, t1.z) as score
from table1 t1
) t1 INNER JOIN
table2 t2
ON t1.c2 = t2.c1
WHERE t1.c5 = 5 AND
t1.score . . .

A good option would be to use the having clause.
Select t1.id, SUM(t1.x + t1.y + t1.z)
FROM table1 t1 inner join table2 t2 on t1.c2 = t2.c1
WHERE t1.c5 = 5
GROUP BY t1.id
Having SUM(t1.x + t1.y + t1.z) = ...
Or, you could do a sub-query in the from clause. The below code makes some assumptions on structure.
SELECT t1.id
FROM
(Select SUM(t1.x + t1.y + t1.z) as score, t1.c5, t1.c2, t1.id
FROM table1 t1
GROUP BY t1.id
) t1
INNER JOIN table2 t2 on t1.c2 = t2.c1
WHERE
t1.c5=5
And t1.score = ...

Related

SAS SQL inner join two columns able to match one column

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?

SQL - Sharing fields between derived tables

Is it possible to access fields across derived tables?
SELECT *
FROM (SELECT ID, COL1A FROM Table1) T1
JOIN (SELECT ID, COL2A FROM Table2) T2
ON T1.ID = T2.ID
JOIN (SELECT ID, (COL3A + T2.COL2A) AS SUM FROM Table3) T3
ON T1.ID = T3.ID
You would put the expressions using multiple columns in the SELECT clause:
SELECT t1.ID, t1.COL1A, t2.COL2A, (t3.COL3A + t2.COL2A) as sum
FROM Table1 T1 JOIN
Table2 T2
ON T1.ID = T2.ID JOIN
Table3 T3
ON T1.ID = T3.ID;
There is no need for derived tables at all.

Subquery in Inner join - Multi-part identifer could not be bound

I have the following query and I am getting a multi-part identifier could not be bound exception. I realize why and my next thought was to use a CROSS APPLY, which didn't work. What I really need to do is join that subquery to make sure I get the most recent entry for a particular column.
Unfortunately, I've run out of ideas... How do I accomplish this query?
SELECT t1.*
FROM dbo.Table1 t1 (nolock)
INNER JOIN
(
SELECT TOP 1 t2_s.c1, t2_s.c2, t2_s.c4
FROM dbo.Table2 t2_s
WHERE t2_s.c1 = t1.c1
ORDER BY t2_s.dateStamped DESC
) t2
on t2.c1 = t1.c1
INNER JOIN dbo.Table3 t3
on t3.c1 = t2.c2 and t3.c2 = 1
WHERE t1.c2 = 'xxx'
Assuming you are using SQL Server, then CROSS APPLY should do what you want:
SELECT t1.*
FROM dbo.Table1 t1 (nolock) CROSS APPLY
(SELECT TOP 1 t2_s.c1, t2_s.c2, t2_s.c4
FROM dbo.Table2 t2_s
WHERE t2_s.c1 = t1.c1
ORDER BY t2_s.dateStamped DESC
) t2 INNER JOIN
dbo.Table3 t3
on t3.c1 = t2.c2 and t3.c2 = 1
WHERE t1.c2 = 'xxx';

Oracle SQL: JOIN by dblink

I use next SQL-query in Oracle DB:
SELECT T1.*,
T3.*
FROM MyTable1 T1
INNER JOIN MyTable2 T2 ON T2.Id1 = T1.Id
LEFT JOIN MyTable3#dblink1 T3 ON T3.Id2 = T2.Id
This query is very simple and fast (about 1 min, T1 contain about 1 million rows, T3 more then 10 million rows). Now I want to use MyTable4 from dblink1 for filtering selected rows data. For it, I use subquery:
SELECT T1.*,
T3.*
FROM MyTable1 T1
INNER JOIN MyTable2 T2 ON T2.Id1 = T1.Id
LEFT JOIN (SELECT Sub_T1.*
FROM MyTable3#dblink1 Sub_T1
INNER JOIN MyTable4#dblink1 Sub_T2 ON Sub_T2.Id3 = Sub_T1.Id
WHERE
Sub_T2.MyColumn1 = 'required value') T3 ON T3.Id2 = T2.Id
But this query is too slow (more then 20min). If I rewrite this query to:
SELECT T1.*,
T3.*
FROM MyTable1 T1
INNER JOIN MyTable2 T2 ON T2.Id1 = T1.Id
LEFT JOIN MyTable3#dblink1 T3 ON T3.Id2 = T2.Id
LEFT JOIN MyTable4#dblink1 T4 ON T4.Id3 = T3.Id
WHERE
T4.MyColumn1 = 'required value'
Then my query work fast again, but I donn't like result (I want to see columns of T3 as null, if WHERE return false).
How to improve my second query, for speed up it?
Does phrasing the query with parentheses solve the problem?
SELECT T1.*,
T3.*
FROM MyTable1 T1 INNER JOIN
MyTable2 T2
ON T2.Id1 = T1.Id LEFT JOIN
(MyTable3#dblink1 T3 JOIN
MyTable4#dblink1 T4
ON T4.Id3 = T3.Id AND
T4.MyColumn1 = 'required value'
)
ON T3.Id2 = T2.Id;
Or, also:
SELECT T1.*,
T3.*
FROM MyTable1 T1 INNER JOIN
MyTable2 T2
ON T2.Id1 = T1.Id LEFT JOIN
MyTable3#dblink1 T3
ON T3.Id2 = T2.Id
EXISTS (SELECT 1 FROM MyTable4#dblink1 T4 WHERE T4.Id3 = T3.Id AND T4.MyColumn1 = 'required value'
)

join with the on supplied later

I have a query in the following format
select
*
from
Table1 t1
inner join Table2 t2
inner join Table3 t3 on t2.ID = t3.ID
on t3.ID = t1.ID
What I do know:
Not providing the last on condition results in an error.
Additionally changing the first join condition from on t2.ID = t3.ID to on t1.ID = t2.ID results in an error that t1.ID could not be bound.
Obviously the above examples are arbitrary and may not actually produce a practically useful result. However, an explanation of what providing the on later is actually doing would be great.
Thanks
EDIT
I'm not trying to change the question to something that works but to understand what MSSQL is doing when I provide it.
You can use the format you specified (presuming the correct table aliases), if you use parenthesis.
Select ... -- never use Select *
From (Table1 As T1
Join Table2 As T2
On T2.ID = T1.ID)
Join Table3 As T3
On T3.ID = T1.ID
However, with equi-joins (inner joins) it really makes no difference and it is easier to read if you do not use parenthesis. However, this format is very useful with outer joins. Take the following two examples:
Example 1
Select ...
From Table1 As T1
Left Join Table2 As T2
On T2.T1_ID = T1.ID
Join Table3 As T3
On T3.T2_ID = T2.ID
Example 2
Select ...
From Table1 As T1
Left Join (Table2 As T2
Join Table3 As T3
On T3.T2_ID = T2.ID)
On T2.T1_ID = T1.ID
Suppose in this situation, that T3.T2_ID is a non-nullable foreign key to Table2. In Example1, the Inner Join to Table3 will effectively filter out rows that would have been null because the given T2.T2_ID does not exist in Table1. However, in the second example, the join between Table2 and Table3 is done before the Left Join to Table1 is processed. Thus, we'll get the same rows from Table1 and Table2 as:
Example 3
Select ...
From Table1 As T1
Left Join Table2 As T2
On T2.T1_ID = T1.ID
Assuming you meant t1 rather than t, then your query:
select
*
from
Table1 t1
inner join Table2 t2
inner join Table3 t3 on t2.ID = t3.ID
on t3.ID = t1.ID
...can be made rather more clear by the addition of the brackets it doesn't really need:
select
*
from
Table1 t1
inner join
(Table2 t2 inner join Table3 t3 on t2.ID = t3.ID) on t3.ID = t1.ID
Effectively, you're explicitly saying "join t2 to t3, then join t1 to that."
Does that help?
First off--you don't define what t is
Table1 is aliased t1
Table2 is aliased t2
Table3 is aliased t3
But there is no plain t.
Second, you are not doing a join of t1 to t2, but of t1 to t3 and then t3 to t2. That will work. If there is a relation between t1 and t2 (t1.ID=t2.ID) then that "on" statement should directly follow the inner join statement for t2:
select
*
from
Table1 t1
inner join Table3 t3 on t1.ID = t3.ID
inner join Table2 t2 on t3.ID = t2.ID
UPDATE (based on your update)
are t1.ID, t2.ID, and t3.ID all the same data type?