Inner join and 2 left joins - sql

The inner join works, i can left join the second table, but i cant join the third table.
I dont know why, even if i inner join the first table and then left join the third it doenst work.
If i inner join the table and left join with the second it works like said, but when i put the third table into the query it fails again.
create table xxxx AS select
t1.XXX,
t2.YYY,
t3.ZZZ
from
DB_AA..P1 t1
INNER JOIN DB_TT..TQ t5 on (t5.NR = t1.NR)
LEFT JOIN DB_II..GG t2 ON (t2.LS = t1.LS)
LEFT JOIN DB_AA..PP t3 ON (t3.NR= t1.NR)
Anybody see something i dont see?
I pass the code to a Netezza.

Related

MS Access SQL Query - Error in Join

i am new to SQL and have been trying to have an SQL query in MS Access to join multiple tables using the below sql. This is for doing access testing.
Have explained what i intent to achieve in bold
SELECT
Table1.Role,
Table1.Object,
Table 1 is the base table which has role, authorization object
Table2.Role,
Table2.User_Name,
Table 2 has the users mapped to the role
Table3.Org_Level_Desp,
Table3.Org_LEvel_Values_1,
Table 3 has controls at organization level, this determine which company or plant the user can access
Table4.Role_Description,
Table 4 has role descriptions for roles mentioned in Table 1
Table5.Full_Name,
Table5.Department,
Table 5 has user name and department. Common field is user name from table 2
FROM
Table1
RIGHT JOIN Table2 ON Table1.Role=Table2.Role
RIGHT JOIN Table3 ON Table1.Role=Table3.Role
RIGHT JOIN Table4 ON Table1.Role=Table4.rOLE
Joining required tabled from Table 2, 3 and 4 to Table 1
FROM
Table2
RIGHT JOIN Table5 ON Table2.USER_NAME=Table5.USer
Joining required tables from table 5 to table 1
I am getting multiple syntax error for the above query. I think i am missing on something basic - Can anyone help ?
Thanks !
Uday
Seems i cannot add more in comment. Sorry, below is the code that i updated, still getting the same error. Can you please advise ?
SELECT
TABLE1.Role,
TABLE1.Object,
TABLE1.Field_name,
TABLE1.Value,
TABLE1.[and],
TABLE1.ID_whether_object_is_deleted,
TABLE2.Role,
TABLE2.User_Name,
TABLE2.End_date,
TABLE3.Org_Level_Control,
TABLE3.Org_LEvel_Values_1,
TABLE3.Org_LEvel_Values_2,
Table4.Role_Description,
TABLE6.Field_Short_Description,
TABLE7.Object_Level_Desp,
TABLE8.Auth_Obj_Text,
TABLE5.Full_Name,
TABLE5.Department,
TABLE9.Valid_to,
TABLE9.Lock,
from ((((((TABLE1 INNER JOIN TABLE2 ON TABLE1.Role=TABLE2.Role)
INNER JOIN TABLE3 ON TABLE1.Role=TABLE3.Role)
INNER JOIN Table4 ON TABLE1.Role=Table4.role)
INNER JOIN TABLE6 ON TABLE1.Field_Name=TABLE6.Field_Name)
INNER JOIN TABLE7 ON TABLE1.[Object]=TABLE7.Org_Object)
INNER JOIN TABLE8 ON AGR_TABLE1.Field_name=TABLE8.Field_Name)
FROM
(TABLE2 INNER JOIN TABLE5 ON TABLE2.USER_NAME=TABLE5.[USer])
INNER JOIN TABLE9 ON TABLE2.USER_NAME=TABLE9.[User]);
Below is the code that i updated, still getting the same error. Can you please advise ?
SELECT
TABLE1.Role,
TABLE1.Object,
TABLE1.Field_name,
TABLE1.Value,
TABLE1.[and],
TABLE1.ID_whether_object_is_deleted,
TABLE2.Role,
TABLE2.User_Name,
TABLE2.End_date,
TABLE3.Org_Level_Control,
TABLE3.Org_LEvel_Values_1,
TABLE3.Org_LEvel_Values_2,
Table4.Role_Description,
TABLE6.Field_Short_Description,
TABLE7.Object_Level_Desp,
TABLE8.Auth_Obj_Text,
TABLE5.Full_Name,
TABLE5.Department,
TABLE9.Valid_to,
TABLE9.Lock,
from ((((((TABLE1 INNER JOIN TABLE2 ON TABLE1.Role=TABLE2.Role)
INNER JOIN TABLE3 ON TABLE1.Role=TABLE3.Role)
INNER JOIN Table4 ON TABLE1.Role=Table4.role)
INNER JOIN TABLE6 ON TABLE1.Field_Name=TABLE6.Field_Name)
INNER JOIN TABLE7 ON TABLE1.[Object]=TABLE7.Org_Object)
INNER JOIN TABLE8 ON AGR_TABLE1.Field_name=TABLE8.Field_Name)
I have no idea why you would want RIGHT JOIN, particularly if you are learning SQL. Start with INNER JOIN. If that is leaving out rows, then move to LEFT JOIN.
The syntax in MS Access uses parentheses:
SELECT . . .
FROM (((Table1 INNER JOIN
Table2
ON Table1.Role = Table2.Role
) INNER JOIN
Table3
ON Table1.Role = Table3.Role
) INNER JOIN
Table4
ON Table1.Role = Table4.rOLE
) INNER JOIN
Table5
ON Table2.USER_NAME = Table5.USer
You can then select the columns you want in the SELECT.
Note that the parentheses would look quite awkward in any other database.
Re: your updated SQL.
Value is a reserved word in Access SQL, so may be others like Role, Object, Lock. Put these into [square brackets], both in the SELECT and the FROM clause.
And you have an extraneous comma at the end of your SELECT clause:
TABLE9.Valid_to,
TABLE9.Lock, <== delete this comma
from ((((((TABLE1 INNER JOIN TABLE2 ON TABLE1.Role=TABLE2.Role)

Joining multiple tables in SQL

Can sombody Explains me about joins?
Inner join selects common data based on where condition.
Left outer join selects all data from left irrespective of common but takes common data from right table and vice versa for Right outer.
I know the basics but question stays when it comes to join for than 5, 8, 10 tables.
Suppose I have 10 tables to join. If I have inner join with the first 5 tables and now try to apply a left join with the 6th table, now how the query will work?
I mean to say now the result set of first 5 tables will be taken as left table and the 6th one will be considerded as Right table? Or only Fifth table will be considered as left and 6th as right? Please help me regarding this.
When joining multiple tables the output of each join logically forms a virtual table that goes into the next join.
So in the example in your question the composite result of joining the first 5 tables would be treated as the left hand table.
See Itzik Ben-Gan's Logical Query Processing Poster for more about this.
The virtual tables involved in the joins can be controlled by positioning the ON clause. For example
SELECT *
FROM T1
INNER JOIN T2
ON T2.C = T1.C
INNER JOIN T3
LEFT JOIN T4
ON T4.C = T3.C
ON T3.C = T2.C
is equivalent to (T1 Inner Join T2) Inner Join (T3 Left Join T4)
It's helpful to think of JOIN's in sequence, so the former is correct.
SELECT *
FROM a
INNER JOIN b ON b.a = a.id
INNER JOIN c ON c.b = b.id
LEFT JOIN d ON d.c = c.id
LEFT JOIN e ON e.d = d.id
Would be all the fields from a and b and c where all the ON criteria match, plus the values from d where its criteria match plus all the contents of e where all its criteria match.
I know RIGHT JOIN is perfectly acceptable, but I've found in my experience that it's unnecessary - I almost always just join things from left to right.
> Simple INNER JOIN VIEW code...
CREATE VIEW room_view
AS SELECT a.*,b.*
FROM j4_booking a INNER JOIN j4_scheduling b
on a.room_id = b.room_id;
You can apply join like this..
select a.*,b.*,c.*,d.*,e.*
from [DatabaseName].[Table_a] a
INNER JOIN [DatabaseName].[Table_b] b ON a.id = b.id
INNER JOIN [DatabaseName].[Table_c] c ON b.id=c.id
INNER JOIN [DatabaseName].[Table_d] d on c.id=d.id
INNER JOIN [DatabaseName].[Table_e] e on d.id=e.id where a.con=5 and
b.con=6
Here, at place of a.* and in where condition, you can show column(filed) which you like and according condition in where condition. You can insert more table and database as per your choice. But mind that you need to mention database name and alias if you work in different database.
Just tried the following from the Example DataBase given in W3School. Worked Fine for me.
SELECT Orders.OrderID, Customers.CustomerName, Orders.OrderDate, Products.ProductName, Products.ProductID
FROM Orders
INNER JOIN Products
INNER JOIN Customers
ON Orders.CustomerID=Customers.CustomerID;
Join used to combine rows from two or more tables, based on a related column between them. This example from Adventure works:
SELECT a.[EmailAddress],b.[FirstName],b.[LastName],c.[PhoneNumber],d.[Name]
FROM [Person].[EmailAddress] a
INNER JOIN [Person].[Person] b
ON a.BusinessEntityID = b.BusinessEntityID
INNER JOIN [Person].[PersonPhone] c
ON b.BusinessEntityID = c.BusinessEntityID
INNER JOIN [Person].[PhoneNumberType] d
ON c.phoneNumberTypeID = d.phoneNumberTypeID

coldfusion left join not returning expected results

I am working on a left join query and i am not getting back my expected results. I have searched over and over and have not been able to find what is causing this issue. When i execute the following code it returns the results of a LEFT INNER JOIN(only records from the left that match an item in the right). I have tried to specify LEFT OUTER with no success. Am i missing something simple?
var records = ORMExecuteQuery("SELECT new map(inst.id AS installationId,
inst AS installation,
uaa.id AS id,
uaa.permission AS permission,
uaa.app AS app)
FROM Installation AS inst,
LEFT JOIN inst.userApplicationAccesses AS uaa
WITH uaa.user.userId = ?", [Arguments.userId]);
ok your left join is wierd. You are left joining to a column, not a table, and that column is part of your first table (you are even referncing the as name).
You outer join should look something like:
Select *
From Table_A as A
Left Outer Join Table_B as B on B.ID = A.ID
and yes, both Table_A and Table_B can be the same table, but that table should be listed out twice.

Weird SQL Server view definition

I've "inherited" a well over 10-year old app, and it does show its age at times. I've stumbled across a really weird view definition today - I just can't seem to make sense of it! Can you help me? This was originally on SQL Server 7.0 and has since been migrated to SQL Server 2005 - but obviously it's never been refactored / redone....
This is the view definition - based on a bunch of tables and another view:
CREATE VIEW dbo.MyOddView
AS
SELECT
t1.MVOID, t1.SomeOtherColumn,
t2.Number ,
t3.OID, t3.FKOID,
t4.AcctNo,
t5.ShortDesc, t5.ZipCode, t5.City,
t6.BankAcctNo
FROM
dbo.viewFirst vf
INNER JOIN
dbo.Table1 t1 ON vf.MVOID = t1.MVOID AND vf.ValidFrom = t1.ValidFrom
LEFT OUTER JOIN
dbo.Table2 t2
RIGHT OUTER JOIN
dbo.Table3 t3 ON t2.OID = t3.FKOID
LEFT OUTER JOIN
dbo.Table4 t4 ON t3.ZVOID = t4.OID
LEFT OUTER JOIN
dbo.Table5 t5
INNER JOIN
dbo.Table4 t6 ON t5.OID = t6.BCOID
ON t4.ZVOID = t5.OID
ON t2.AddressOID = t4.OID
GO
What I don't get are the two JOIN's (for Table2 t2 and Table5 t5) which have no JOIN condition listed next to them, and the two extra ON conditions at the end of the view definition - I can't seem to rip this apart and put it back together in "proper" ANSI JOIN syntax so that my row count is the same...... (my original view gets me something over 12'000 rows, and a first attempt at refactoring this returned a single row......)
Any ideas? What the heck is this? Seems like totally invalid SQL to me - but it appears to be doing its job (and has been for the past several years....) Any thoughts? Pointers?
SELECT ...
FROM dbo.viewFirst vf
INNER JOIN dbo.Table1 t1
ON vf.MVOID = t1.MVOID
AND vf.ValidFrom = t1.ValidFrom
LEFT OUTER JOIN dbo.Table2 t2
RIGHT OUTER JOIN dbo.Table3 t3
ON t2.OID = t3.FKOID
LEFT OUTER JOIN dbo.Table4 t4
ON t3.ZVOID = t4.OID
LEFT OUTER JOIN dbo.Table5 t5
INNER JOIN dbo.Table4 t6
ON t5.OID = t6.BCOID
ON t4.ZVOID = t5.OID
ON t2.AddressOID = t4.OID
This syntax is covered in chapter 7 of Inside SQL Server 2008 T-SQL Querying or see this article by Itzik Ben Gan and the follow up letter by Lubor Kollar
Having the ON clause for t2.AddressOID = t4.OID last for example means that the JOIN of t2 logically happens last. i.e the other joins are logically processed first then the LEFT JOIN happens against the result of those Joins.

Joins in SQL for retriving data from two tables

There are two tables A and B. You are retreiving data from both tables where all rows from B table and only matching rows from A table should be displayed. Which of the following types of joins will you apply between A and B tables?
- Inner join
- Left outer join
- Right outer join
- Self join
Use left outer hoin or right outer join.
For example, the following satisfy your requirement.
select * from tableB
Left outer join tableA
on tableB.ID= tableA.ID
Or
select * from tableA
Right outer join tableB
on tableA.ID= tableB.ID
Better way to understand:
Easy, I would go with (B).
SELECT * FROM B x
LEFT JOIN A y
on x.someColName = y.someColname
EDIT: can also use Right join
SELECT * FROM A x
RIGHT OUTER JOIN B y
on x.someColName = y.someColname
This looks like homework, but it's dead simple enough that I'll just say that you're asking for B LEFT JOIN A.
Join Left
http://www.w3schools.com/Sql/sql_join_left.asp