I have multiple tables in my data set. I need two columns from one table to be pivoted against one column in a different table.
Code:
SELECT DISTINCT
table1.name,
table6.count,
table6.car_name
From Master_Table
Inner Join table1
On Master_Table.ID_SEQNO = table1.ID_SEQNO
Inner Join table2
On table1.table1_ID_SEQNO = table2.table1_ID_SEQNO
Inner Join table3
On table2.ID_SEQNO = table3.ID_SEQNO
Inner Join table4
On table3.ID_SEQNO = table4.ID_SEQNO
Left Outer Join table5
On table4.CANDIDATE_ID_SEQNO = table5.CANDIDATE_ID_SEQNO
Left Outer Join table6
On table5.ID_SEQNO = table6.ID_SEQNO
ORDER BY table1.name
Output I need:
What I have now :
Master_Table : Has names , Date, Time of ownership etc
table1 has info on each name , adress phone number email etc
table2 has info like cars insurance etc so on... I have used id_seqno to join statements without which the code doesn't work.
Each table has a id_seqno with which I map it to every other table... I am modifying an existing code so pretty new to this.
I have 4 tables.
1 with just sitenames.
3 tables, which contains sitenames and the ammount of hits on them for different user types.
I need to make a report on the number of hits on thoose sites for each user type like this
Site Userype1hits Userype2hits Userype3hits
so in the select part I neeed to crosscheck with a table called noanswer
like
select *
from table
where site in (select site from noanswer)
So from what I understand I need to use join, and in this case right join?
How do I do with join in this query?
You would use left join:
select sn.*, t1.cnt, t2.cnt, t3.cnt
from sitenames sn left join
table1 t1
on t1.name = sn.name left join
table2 t2
on t2.name = sn.name left join
table3 t3
on t3.name = sn.name;
Your question is vague on the field names.
A left join keeps all rows in the first table and matches rows in the subsequent tables. It is much more commonly used than right join, probably because it is easier to read the logic thinking "I'll keep all of these rows". A series of right joins actually keeps all rows in the last table, so you have to wait to see which rows stay.
I did like this, googled some further as I did not get it to work even with the answer here, joins are totaly new to me. The internalusers, external and so on is just fake to post what I did here as I cant use the real names.
select s.Site,s2.Total_hits as 'Internalusers',s3.Total_Hits as 'ExternalUsers',
s4.Total_hits as 'Comapany2users' from Database.Table as S
left outer join HITAnalyze.dbo.Internal as s2 on s2.site = s.Site
left outer join HITAnalyze.dbo.External1 as s3 on s3.site = s.Site
left outer join HITAnalyze.dbo.Company2 as s4 on s4.site = s.Site
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)
I have 3 tables of accounts that all contain the same fields. Table1 contains all accounts while Table2 and Table3 contain subsets of the accounts. I'm trying to select records in Table1 that do no exist in Table2 or Table3.
Let's say the table layout is like this and is the same for all 3 tables:
|AcctNum|Name|State|
I know how to do this if it was just Table1 and Table2, using a left join and Is Null, but the 3rd table is throwing me. Is this possible to do in one query? Can you combine left joins? I should point out I'm using Access 2010.
Yes you can combine left joins and with the odd syntax Access uses the query should look like this:
SELECT T1.AcctNum
FROM (Table1 AS T1 LEFT JOIN Table2 AS T2 ON T1.AcctNum = T2.AcctNum)
LEFT JOIN Table3 AS T3 ON T1.AcctNum = T3.AcctNum
WHERE (((T2.AcctNum) Is Null) AND ((T3.AcctNum) Is Null));
You can use Access to create a view called TableCombined that is a union of both Table2 and Table3.
At that point, you can use your left join and Is Null query and join TableCombined to Table1.
Hope this helps!
You can also do a NOT EXISTS statement which makes sense logically for what you are trying to achieve.
For example:
SELECT ACCTNUM
FROM TABLE1
WHERE NOT EXISTS (SELECT TABLE2.ACCTNUM FROM TABLE2 INNER JOIN TABLE3 WHERE TABLE2.ACCTNUM IS NULL AND TABLE3.ACCTNUM IS NULL)
I am new to sql and I am trying to join 4 tables together but just cant get the hang of it.
I am trying to do this with an inner join but I always get an syntax error with access.
SELECT *
from kdst,aufpos
inner join( artst inner join vert on kdst.vertreter = vert.vertnr)
on aufpos.artnr = artst.artnr;
This is my code but it does not work. I dont know what to do anymore, I hope someone can help me.
Build using the query design window
Then switch to sql view
Select *
From table1 t1
Inner join table2 t2 on t1.id = t2.fkid
Inner join table3 t3 on t1.id = t3.fkid
...
This is if you want to join multiple tables to the same parent table (table1). Fkid is the column of the foreign key field that refers to the primary key of the parent table.