coldfusion left join not returning expected results - orm

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.

Related

Filter on the column on which two tables are joined

Are next two queries going to return same result set?
SELECT * FROM tableA a
JOIN tableB b
ON a.id = b.id
WHERE a.id = '5'
--------------------------------
SELECT * FROM tableA a
JOIN tableb b
ON a.id = b.id
WHERE b.id = '5'
Also, will answer be different if LEFT JOIN is used instead of JOIN?
As written, they will return the same result.
The two will not necessarily return the same result with a left join.
Yes the result will be the same.
With a left join you will get every dataset of both table who got a ID.
With a join (Inner Join) you will get only the dataset's who a.id = b.id.
This site will explain you how to join https://www.w3schools.com/sql/sql_join.asp
Yes they will. A simple join works like an inner join by default. It checks for instances where the item you're joining on exist on both tables. Since you're joining on where a.id=b.id the results will be the same.
If you change the type of join to a left, the results will include all a.id's regardless of whether they are equal to 5.

Right join or which type of join?

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

SQL Different between Left join on... and Left Join on..where

I have two sql to join two table together:
select top 100 a.XXX
,a.YYY
,a.ZZZ
,b.GGG
,b.JJJ
from table_01 a
left join table_02 b
on a.XXX = b.GGG
and b.JJJ = "abc"
and a.YYY between '01/08/2009 13:18:00' and '12/08/2009 13:18:00'
select top 100 a.XXX
,a.YYY
,a.ZZZ
,b.GGG
,b.JJJ
from table_01 a
left join table_02 b
on a.XXX = b.GGG
where b.JJJ = "abc"
and a.YYY between '01/08/2009 13:18:00' and '12/08/2009 13:18:00'
The outcome of them is different but I don't understand the reason why.
I would be grateful if I can get some help here.
Whenever you are using LEFT JOIN, all the conditions about the content of the right table should be in the ON clause, otherwise you are effectively converting your LEFT JOIN to an INNER JOIN.
The reason for that is that when a LEFT JOIN is used, all the rows from the left table will be returned. If they are matched by the right table, the values of the matching row(s) will be returned as well, but if they are not matched with any row on the right table, then the right table will return a row of null values.
Since you can't compare anything to NULL (not even another NULL) (Read this answer to find out why), you are basically telling your database to return all rows that are matched in both tables.
However, when the condition is in the ON clause, Your database knows to treat it as a part of the join condition.

Having problems with SQL Joins

Table A
Table B
I tried to use LEFT OUTER JOIN but it seems not working..
I want the query to extract all data from Table A with 0 as average score if there is no data yet for the specified parameter. Meaning, in Figure 3, it should have shown ID 2 with 0 on s. Can anyone help me figure out the solution?
You have the table names switched in the join. To keep all of Table A then it needs to be the table listed on the left side of the left join. Also anything that you want to only affect the output of table B, and not filter the entire results, should be moved to the left join on clause. Should be:
SELECT a.id,
Avg(Isnull(b.score, 0)) AS s
FROM a
LEFT OUTER JOIN b
ON a.id = b.id
AND b.kind = 'X'
GROUP BY a.id

SQL trying to do a JOIN to include results from multiple Tables

I'm a complete novice teaching myself SQL by writing and modifying a few queries and reports at work.
I've got something of a handle on the various types of JOINs and I've used INNER JOIN a few times with decent success.
What I'm stuck on should be a simple task, but my Google-Fu must be weak. Here's what I'm trying to do.
Say I have 3 tables, Table_A, Table_B, and Table_C, and each table has a column called [Serial_Number].
What I'm wanting to select is 3 of the other columns if A.Serial_Number = B.Serial_Number OR C.Serial_Number.
I've tried doing:
SELECT
*
FROM
Table_A AS A
INNER JOIN Table_B AS B ON A.Serial_Number = B.Serial_Number
INNER JOIN Table_C AS C ON A.Serial_Number = C.Serial_Number
But this always yields 0 results as the nature of the data dictates that if A matches B, it will never match C and vice versa. I also tried a LEFT OUTER JOIN as the second clause, but this just includes NULLs from Table_C that have already matched on Table_B.
All the searches I have done relating to JOINs on multiple tables seem to be about using JOINS to further exclude records, where I'm actually wanting to INCLUDE more records.
Like I said, I'm sure this is really simple, just needing a nudge in right direction.
Thanks!
The use of two inner joins here is akin to saying
If A.Serial_Number = B.Serial_Number AND
A.Serial_Number = C.Serial_Number
Using left outer join on the second clause - by which i presume you mean second join - would perform a left join on a result set already filtered by A.Serial_Number = B.Serial_Number by the first inner join. Given that B.Serial_Number doesn't relate to C.Serial_Number you wouldn't expect the an equijoin to return any result from tablec.
What you want is a left outer join like you tried but for both tableb and tablec.
Select *
From tablea
Left join tableb on tableb.Serial_Number = tablea.Serial_Number
Left join tablec on tablec.Serial_Number = tablea.Serial_Number
This way regardless of whether tablea.Serial_Number is in tableb it will still be returned and thus available to be joined to tablec
Agreed. Your output for your inner joins is producing NULLs which is why it is resulting in 0. I would suggest modifying your INNER JOIN.