Dynamic JOIN condition on a Table - sql

I want to avoid string concatenation to create dynamic SQL query in SQL 2008.
How can I achieve the below functionality, if at all, efficiently ?
A table stores the various conditions a user had chosen to search DB by using one or more values to search tables from. For e.g. he can give SSN or DOB or both SSN and DOB.
So if he gives multiple values as input, then the query needs to find records matching ALL the criteria. This needs me to create queries from the below shown two tables
I always need to return personid, which may be from the person table, or the view.
Search conditions given by user will be stored in the 'conditions' table below
conditions
-------------
id,custid,dob,ssn,base
person
--------
id,firstname,ssn,dob
view
--------
id,personid,base
So, IF DOB is to be searched
SELECT p.personid from conditon c
INNER JOIN person p
ON p.dob=c.dob
WHERE c.condition=1
IF SSN and base is to be searched
SELECT v.personid from conditon c
INNER JOIN view v
ON c.base=v.base
INNER JOIN person p
ON p.ssn=c.ssn
WHERE c.condition=1
IF SSN,DOB and base is to be searched
SELECT v.personid from conditon c
INNER JOIN view v
ON c.base=v.base
INNER JOIN person p
ON p.ssn=c.ssn
AND
p.dob=c.dob
WHERE c.condition=1

Use the pattern below with IsNull() or Coalesce() function
Where ColumnName = IsNull(#InParameter, ColumnName)
or tyhe same pattern in a Join Condition
Join Table a
On [Other stuff]
And ColumnName = IsNull(#InParameter, ColumnName)
And then, if the parameter is supplied, the ql will filter or join based on the parameter value, if it not supplied or is null, the query will ignore it (ColumnValue = ColumnVale is always true (except for nullable columns)
EDITED: to add another predicate to Join Condition. because, in the case where you did not specifiy a parameter value, the join condition would always be true, and, unless some other predicate condition is in the join, you would effectively get a cartesian product...

Related

Semi-join vs Subqueries

What is the difference between semi-joins and a subquery? I am currently taking a course on this on DataCamp and i'm having a hard time making a distinction between the two.
Thanks in advance.
A join or a semi join is required whenever you want to combine two or more entities records based on some common conditional attributes.
Unlike, Subquery is required whenever you want to have a lookup or a reference on same table or other tables
In short, when your requirement is to get additional reference columns added to existing tables attributes then go for join else when you want to have a lookup on records from the same table or other tables but keeping the same existing columns as o/p go for subquery
Also, In case of semi join it can act/used as a subquery because most of the times we dont actually join the right table instead we mantain a check via subquery to limit records in the existing hence semijoin but just that it isnt a subquery by itself
I don't really think of a subquery and a semi-join as anything similar. A subquery is nothing more interesting than a query that is used inside another query:
select * -- this is often called the "outer" query
from (
select columnA -- this is the subquery inside the parentheses
from mytable
where columnB = 'Y'
)
A semi-join is a concept based on join. Of course, joining tables will combine both tables and return the combined rows based on the join criteria. From there you select the columns you want from either table based on further where criteria (and of course whatever else you want to do). The concept of a semi-join is when you want to return rows from the first table only, but you need the 2nd table to decide which rows to return. Example: you want to return the people in a class:
select p.FirstName, p.LastName, p.DOB
from people p
inner join classes c on c.pID = p.pID
where c.ClassName = 'SQL 101'
group by p.pID
This accomplishes the concept of a semi-join. We are only returning columns from the first table (people). The use of the group by is necessary for the concept of a semi-join because a true join can return duplicate rows from the first table (depending on the join criteria). The above example is not often referred to as a semi-join, and is not the most typical way to accomplish it. The following query is a more common method of accomplishing a semi-join:
select FirstName, LastName, DOB
from people
where pID in (select pID
from class
where ClassName = 'SQL 101'
)
There is no formal join here. But we're using the 2nd table to determine which rows from the first table to return. It's a lot like saying if we did join the 2nd table to the first table, what rows from the first table would match?
For performance, exists is typically preferred:
select FirstName, LastName, DOB
from people p
where exists (select pID
from class c
where c.pID = p.pID
and c.ClassName = 'SQL 101'
)
In my opinion, this is the most direct way to understand the semi-join. There is still no formal join, but you can see the idea of a join hinted at by the usage of directly matching the first table's pID column to the 2nd table's pID column.
Final note. The last 2 queries above each use a subquery to accomplish the concept of a semi-join.

How to use ISNULL() function along with join

While using the ISNULL function along with aliases of column name that I have given to form a left join, I get an error.
The error is:
I am unable to understand what am I doing wrong. The data is stored in customers table:
The sql code that I am using is:
select ISNULL(c2.name,'N/A') as referredby , c1.name as name
from customers as c1
left outer join customers as c2 on c1.referredby = c2.id
order by referredby;
Don't use isnull()! The standard version is coalesce():
select coalesce(cref.name, 'N/A') as referredby_name, c.name as name
from customers c left outer join
customers cref
on c.referredby = cref.id
order by referredby_name;
I made a two other changes. First, the table aliases better describe the role of the tables. I also changed the column alias to referredby_name, so it doesn't match a column in a table. Neither of these are required; with them, I think the query reads more easily.

Why we should specify column name in where clause at inner join when have same column name?

I have the query
select gltree.*,tsacc.confirm,tsacc.acc_no,commacc.* from tsacc
inner join commacc on tsacc.acc_no = commacc.acc_no and tsacc.glcode = commacc.glcode
inner join gltree on tsacc.glcode = gltree.glcode
where gltree.glcode = 12738
in this query two specified tables have 'glcode' column name , so
why I should specify table name in where clause eg. gltree.glcode and can't use only glcode without table name ? As we have just one glcode in executed query ?
You actually have threes columns with that name (tsacc.glcode, commacc.glcode and gltree.glcode), so you need to tell the database which one you mean.
The list of columns in the select list is evaluated as the last step when processing the statement. So when the DB processes the where clause it does not "know" which of them you are actually using (you could use all of them).
Plus: with an inner join it does indeed not matter, but if you were using an outer join it would make a big difference which of those three columns is used in the where clause.

Returning Null values with COUNT

With this query, I get a result that is two short of the table because they are not included in count, and I would like get the NULL values in the result. To do this, I am pretty sure I need to use a subquery of some kind, but I am not sure how, since the attribute in question is an aggregate.
SELECT Equipment.SerialNo , Name, COUNT(Assignment.SerialNo)
FROM Equipment
INNER JOIN Assignment
ON Assignment.SerialNo = Equipment.SerialNo
GROUP BY Equipment.SerialNo, Name
You need to change your inner join to a left outer join and count something other than Assignment.SerialNo if you want all rows counted -- use count(*) or another property that will not be null, as count(column) does not include nulls for that column. If you don't want null Assignment.SerialNo values included in the count, then continue aggregating on that column.
An inner join by definition will skip values where Assignment.SerialNo is NULL-- an outer join will include them.

Left Join With Where Clause

I need to retrieve all default settings from the settings table but also grab the character setting if exists for x character.
But this query is only retrieving those settings where character is = 1, not the default settings if the user havent setted anyone.
SELECT `settings`.*, `character_settings`.`value`
FROM (`settings`)
LEFT JOIN `character_settings`
ON `character_settings`.`setting_id` = `settings`.`id`
WHERE `character_settings`.`character_id` = '1'
So i should need something like this:
array(
'0' => array('somekey' => 'keyname', 'value' => 'thevalue'),
'1' => array('somekey2' => 'keyname2'),
'2' => array('somekey3' => 'keyname3')
)
Where key 1 and 2 are the default values when key 0 contains the default value with the character value.
The where clause is filtering away rows where the left join doesn't succeed. Move it to the join:
SELECT `settings`.*, `character_settings`.`value`
FROM `settings`
LEFT JOIN
`character_settings`
ON `character_settings`.`setting_id` = `settings`.`id`
AND `character_settings`.`character_id` = '1'
When making OUTER JOINs (ANSI-89 or ANSI-92), filtration location matters because criteria specified in the ON clause is applied before the JOIN is made. Criteria against an OUTER JOINed table provided in the WHERE clause is applied after the JOIN is made. This can produce very different result sets. In comparison, it doesn't matter for INNER JOINs if the criteria is provided in the ON or WHERE clauses -- the result will be the same.
SELECT s.*,
cs.`value`
FROM SETTINGS s
LEFT JOIN CHARACTER_SETTINGS cs ON cs.setting_id = s.id
AND cs.character_id = 1
If I understand your question correctly you want records from the settings database if they don't have a join accross to the character_settings table or if that joined record has character_id = 1.
You should therefore do
SELECT `settings`.*, `character_settings`.`value`
FROM (`settings`)
LEFT OUTER JOIN `character_settings`
ON `character_settings`.`setting_id` = `settings`.`id`
WHERE `character_settings`.`character_id` = '1' OR
`character_settings`.character_id is NULL
You might find it easier to understand by using a simple subquery
SELECT `settings`.*, (
SELECT `value` FROM `character_settings`
WHERE `character_settings`.`setting_id` = `settings`.`id`
AND `character_settings`.`character_id` = '1') AS cv_value
FROM `settings`
The subquery is allowed to return null, so you don't have to worry about JOIN/WHERE in the main query.
Sometimes, this works faster in MySQL, but compare it against the LEFT JOIN form to see what works best for you.
SELECT s.*, c.value
FROM settings s
LEFT JOIN character_settings c ON c.setting_id = s.id AND c.character_id = '1'
For this problem, as for many others involving non-trivial left joins such as left-joining on inner-joined tables, I find it convenient and somewhat more readable to split the query with a with clause. In your example,
with settings_for_char as (
select setting_id, value from character_settings where character_id = 1
)
select
settings.*,
settings_for_char.value
from
settings
left join settings_for_char on settings_for_char.setting_id = settings.id;
The way I finally understand the top answer is realising (following the Order Of Execution of the SQL query ) that the WHERE clause is applied to the joined table thereby filtering out rows that do not satisfy the WHERE condition from the joined (or output) table. However, moving the WHERE condition to the ON clause applies it to the individual tables prior to joining. This enables the left join to retain rows from the left table even though some column entries of those rows (entries from the right tables) do not satisfy the WHERE condition.
The result is correct based on the SQL statement. Left join returns all values from the right table, and only matching values from the left table.
ID and NAME columns are from the right side table, so are returned.
Score is from the left table, and 30 is returned, as this value relates to Name "Flow". The other Names are NULL as they do not relate to Name "Flow".
The below would return the result you were expecting:
SELECT a.*, b.Score
FROM #Table1 a
LEFT JOIN #Table2 b
ON a.ID = b.T1_ID
WHERE 1=1
AND a.Name = 'Flow'
The SQL applies a filter on the right hand table.