Sentric WorkforceQuery Builder
Working with Table Joins
Sentric, Inc.
Josh DavisSentric Product Support Supervisor
My Credentials:Over 17 years experience in HRIS
Questions?Hang on until the end, we’ll have 15 minutes of Q&A
Or email me at [email protected]
Webinar How ToI will not be able to hear you during the webinar until we get to the question and answer portion, but you have some tools to tell me if you need some help.
By your attendee status are some status icons to help me know if you need me to speak up or speak more softly. You can also put out the life saver to tell me if you need some assistance.
To view the presentation in full screen click the expansion icon in the upper right corner.
This Webinar presents advanced materialThe presentation you are attending presents advanced material about query development. The concepts and information presented are intended for experienced report designers and experienced Query Builder designers.
If you find the material is hard to understand I recommend that you keep working with Query Builder and asking questions.
This webinar is being recorded and can be reviewed by going to the Sentric website.
Agenda
Joins and data
Inner join versus Outer join
Taking joins to end of the line
Joins and Criteria Filters
Adding a filter to a Join
Joins and Data
Joins and Data
Joins and Data
Important background1. SQL Server supports Relational Databases2. Relational databases are made up of
numerous tables that are related or joined to one another by key fields
Joins and Data
Like a Dot to Dot puzzle – the tables like the dots of the puzzle do not make much sense without the connections.
Connecting the dots incorrectly produces an incorrect picture!
Joins and Data
Inner Joins Versus Outer Joins
Inner Joins Versus Outer Joins
Inner Joins Versus Outer Joins
Missing Data Poll
?? ?
Inner Joins Versus Outer Joins
Common Joins1. Inner Join2. Outer Join
1. Left Outer Join2. Right Outer Join
Inner Joins Versus Outer Joins
Inner Join1. Only data with key value in each table return a record
Table AKey Field ID First Name Last Name
1 John Smith
2 Fred Jones
3 Susan Brand
4 Ellen Sanders
Table B
Key Field ID Dependent First Name
Dependent Last Name
1 Julie Smith
1 Brock Smith
4 Ethan Sanders
Results of Inner Join StatementKey Field ID First Name Last Name Dependent
First Name Dependent Last Name
1 John Smith Julie Smith
1 John Smith Brock Smith
4 Ellen Sanders Ethan Sanders
Select [Table A].[First Name], [Table A].[Last Name],[Table B].[Dependent First Name],[Table B].[Dependent Last Name] From [Table A]INNER JOIN[Table B]ON [Table A].[Key Field ID] = [Table B].[Key Field ID]
Inner Joins Versus Outer Joins
Left Outer Join1. All data in Left table returned with matching data from right tableTable AKey Field ID First Name Last Name
1 John Smith
2 Fred Jones
3 Susan Brand
4 Ellen Sanders
Table B
Key Field ID Dependent First Name
Dependent Last Name
1 Julie Smith
1 Brock Smith
4 Ethan Sanders
Results of Left Outer Join StatementKey Field ID
First Name Last Name Dependent First Name
Dependent Last Name
1 John Smith Julie Smith
1 John Smith Brock Smith
2 Fred Jones
3 Susan Brand
4 Ellen Sanders Ethan Sanders
Select [Table A].[First Name], [Table A].[Last Name],[Table B].[Dependent First Name],[Table B].[Dependent Last Name] From [Table A]LEFT OUTER JOIN[Table B]ON [Table A].[Key Field ID] = [Table B].[Key Field ID]
Inner Joins Versus Outer Joins
Right Outer Join1. All data in Right table returned with matching data from left tableTable AKey Field ID First Name Last Name
1 John Smith
2 Fred Jones
3 Susan Brand
4 Ellen Sanders
Table B
Key Field ID Dependent First Name
Dependent Last Name
1 Julie Smith
1 Brock Smith
4 Ethan Sanders
Results of Right Outer Join StatementKey Field ID
First Name Last Name Dependent First Name
Dependent Last Name
1 John Smith Julie Smith
1 John Smith Brock Smith
4 Ellen Sanders Ethan Sanders
Select [Table A].[First Name], [Table A].[Last Name],[Table B].[Dependent First Name],[Table B].[Dependent Last Name] From [Table A]RIGHT OUTER JOIN[Table B]ON [Table A].[Key Field ID] = [Table B].[Key Field ID]
Inner Joins Versus Outer Joins
Example from Sentric Workforce Query Builder1. Show me all active employees and their emergency contacts if
they have any in the system.
Inner Joins Versus Outer Joins
Example from Sentric Workforce Query Builder1. Notice that all the records have Emergency Contact data and
there are 101 records
Inner Joins Versus Outer Joins
Example from Sentric Workforce Query Builder1. Change the record to a Left Outer Join to see all active employees
whether or not they have an Emergency Contact.
Inner Joins Versus Outer Joins
Example from Sentric Workforce Query Builder1. Now looking at the results there are 165 records and active employees
with no emergency contact show blank fields from that table
Inner Joins Versus Outer Joins
Taking Joins to the End of the Line
Inner Joins Versus Outer Joins
Outer Joins Poll
?? ?
Taking Joins to the End of the Line
Taking Joins to the End of the Line
Using Outer Joins can sometimes get tricky1. Imagine this scenario
• Trying to create a query showing all active employees and their emergency contacts if they have any with the state name the contact lives in
• You make the outer join from tPerson to tPersonEmergency run the query and it all looks good
• You then link in the state code table add the description of the state, but when you run the query the data is not right – all the people with no emergency contacts have been removed
• This occurs because the new join is a INNER JOIN and since there is no record in tPersonEmergency the connection to the StateProv Code table removes the records because with an inner join the value must exist on both ends of the join
Taking Joins to the End of the Line
When using Outer Joins all the joins to the end of the string need to be outer joins too
Joins and Criteria Filters
Inner Joins Versus Outer Joins
SQL Tab Poll
?? ?
Joins and Criteria Filters
Adding a filter to the table on the Right side of a Left Outer Join will invalidate the Outer Join
When you add a filter it only returns the records where the filter is true
Joins and Criteria Filters
To see everyone who does not have an Emergency Contact who lives in Pennsylvania, if you set a filter to say StateProvCode <> ‘PA’ – that would invalidate the outer join and employees without a StateProvCode value would no longer show up
To avoid this problem change the criteria to also return null values
Joins and Criteria Filters
The Criteria panel is not configured to allow a NULL value filter as you can see below
Joins and Criteria Filters
A NULL value criteria statement needs to be added on the SQL tab
In a SQL statement the WHERE clause defines the criteria applied to the entire SQL Select Statement
Joins and Criteria Filters
The Basics of a SQL Select Statement
Joins and Criteria Filters
To add a NULL criteria1. Find the value that is in the code currently that can
also be null2. Note that in Query Builder filter Statements are
enclosed in Parenthesis – and a set is also added from the beginning of the where clause to the end of each portion
Joins and Criteria Filters
To add a NULL criteria3. Add an ‘OR’ for a null value to the criteria for the
outer joined table
Joins and Criteria Filters
To add a NULL criteria4. By adding the OR IS NULL criteria the left outer join
becomes valid again
Joins and Criteria Filters
Adding a Filter to a Join
Adding a Filter to a Join
Sometimes adding NULL does not work1. Here is a scenario – you want to see all your active
employees and see who is enrolled in the medical plan and who is not
2. If you set up a filter to say the benefit plan is Medical or it null this does not work because an employee may not be in medical but they may have life insurance, vision coverage or dental
3. NULL only works where either there is a record or there is not one
Adding a Filter to a Join
Use Null Filter
Does a record exist in the table for this person or not
Emergency contacts or any dependent
In WHERE Clause
Adding a Filter to a Join
Examples of a query where you would use Join Filter• Show ALL active employees – show their Voluntary
Employee Life coverage amount if they are enrolled• Show ALL active employees – show the name of their
spouse if they are married
Joins and Criteria Filters
Query to get Employee and Spouse – Step 1
Joins and Criteria Filters
Query to get Employee and Spouse – Step 1
Joins and Criteria Filters
Query to get Employee and Spouse – Step 1
Joins and Criteria Filters
Query to get Employee and Spouse – Step 2 change inner joins to Left outer joins
SELECT TOP 99999999 [tPerson].[PersonGUID] AS [Person GUID], [tPerson].[FullName] AS [Full Name], [Dependent].[FullName] AS [Dependent Full Name], [tRelationship].[RelationshipDescription] AS [Relationship Description], [tRelationship].[SpousalEquivalencyFlag] AS [Spousal Equivalency Flag]FROM [tPerson] INNER JOIN [tPersonStatusHist] ON [tPerson].[PersonGUID] = [tPersonStatusHist].[PersonGUID] LEFT OUTER JOIN [tPersonDependent] ON [tPerson].[PersonGUID] = [tPersonDependent].[PersonGUID] INNER JOIN [tStatus] ON [tPersonStatusHist].[StatusCode] = [tStatus].[StatusCode] LEFT OUTER JOIN [tPerson] AS [Dependent] ON [tPersonDependent].[DependentPersonGUID] = [Dependent].[PersonGUID] LEFT OUTER JOIN [tRelationship] ON [tPersonDependent].[RelationshipCode] = [tRelationship].[RelationshipCode]WHERE (([tPersonStatusHist].PersonStatusCurrentFlag = '1') AND ([tStatus].ActiveFlag = '1'))
Joins and Criteria Filters
Query to get Employee and Spouse – Step 2
Joins and Criteria Filters
Query to get Employee and Spouse – • If you make the MISTAKE of adding the
filter in the filter pane…
Joins and Criteria Filters
Query to get Employee and Spouse – • Your results will only show employees with
spouses – NOT THE DESIRED RESULT!
Joins and Criteria Filters
Query to get Employee and Spouse – • If you try and add the IS NULL Filter to fix this you may be
fooled into thinking the data is correct
Results that show all Active with All Dependents
Results with spouse or NULL filter
Joins and Criteria Filters
Query to get Employee and Spouse – Step 3• To complete this query correctly, changes will need to be
made in the SQL tab• NOTE: Once changes are made in the SQL tab the query will
no longer be updatable in the design tab• If a filter is added to the LEFT OUTER JOIN the system will
return all the records from the right side table and only the matching filtered records from the table on the left side
LEFT OUTER JOIN [tPersonDependent] ON [tPerson].[PersonGUID] = [tPersonDependent].[PersonGUID] AND [tPersonDependent].[RelationshipCode] IN ('SP', 'HU', 'WF')
Joins and Criteria Filters
Query to get Employee and Spouse – Step 3
Joins and Criteria Filters
Query to get Employee and Spouse – Step 3
The more you understand the joins the clearer the picture becomes
The more you understand the joins the clearer the picture becomes
The more you understand the joins the clearer the picture becomes
The more you understand the joins the clearer the picture becomes
The more you understand the joins the clearer the picture becomes
The more you understand the joins the clearer the picture becomes
The more you understand the joins the clearer the picture becomes
Absences
Test Your Knowledge
?? ?
Questions
If you would like to ask a question please enter your question in the Chat box
Thank You for attending!
Please take the survey following the Webinar