Transcript
Page 1: SentricWorkforce Query Builder: Joins and Tables

Sentric WorkforceQuery Builder

Working with Table Joins

Sentric, Inc.

Page 2: SentricWorkforce Query Builder: Joins and Tables

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]

Page 3: SentricWorkforce Query Builder: Joins and Tables

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.

Page 4: SentricWorkforce Query Builder: Joins and Tables

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.

Page 5: SentricWorkforce Query Builder: Joins and Tables

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

Page 6: SentricWorkforce Query Builder: Joins and Tables

Joins and Data

Page 7: SentricWorkforce Query Builder: Joins and Tables

Joins and Data

Page 8: SentricWorkforce Query Builder: Joins and Tables

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

Page 9: SentricWorkforce Query Builder: Joins and Tables

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!

Page 10: SentricWorkforce Query Builder: Joins and Tables

Joins and Data

Page 11: SentricWorkforce Query Builder: Joins and Tables

Inner Joins Versus Outer Joins

Page 12: SentricWorkforce Query Builder: Joins and Tables

Inner Joins Versus Outer Joins

Page 13: SentricWorkforce Query Builder: Joins and Tables

Inner Joins Versus Outer Joins

Missing Data Poll

?? ?

Page 14: SentricWorkforce Query Builder: Joins and Tables

Inner Joins Versus Outer Joins

Common Joins1. Inner Join2. Outer Join

1. Left Outer Join2. Right Outer Join

Page 15: SentricWorkforce Query Builder: Joins and Tables

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]

Page 16: SentricWorkforce Query Builder: Joins and Tables

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]

Page 17: SentricWorkforce Query Builder: Joins and Tables

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]

Page 18: SentricWorkforce Query Builder: Joins and Tables

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.

Page 19: SentricWorkforce Query Builder: Joins and Tables

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

Page 20: SentricWorkforce Query Builder: Joins and Tables

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.

Page 21: SentricWorkforce Query Builder: Joins and Tables

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

Page 22: SentricWorkforce Query Builder: Joins and Tables

Inner Joins Versus Outer Joins

Page 23: SentricWorkforce Query Builder: Joins and Tables

Taking Joins to the End of the Line

Page 24: SentricWorkforce Query Builder: Joins and Tables

Inner Joins Versus Outer Joins

Outer Joins Poll

?? ?

Page 25: SentricWorkforce Query Builder: Joins and Tables

Taking Joins to the End of the Line

Page 26: SentricWorkforce Query Builder: Joins and Tables

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

Page 27: SentricWorkforce Query Builder: Joins and Tables

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

Page 28: SentricWorkforce Query Builder: Joins and Tables

Joins and Criteria Filters

Page 29: SentricWorkforce Query Builder: Joins and Tables

Inner Joins Versus Outer Joins

SQL Tab Poll

?? ?

Page 30: SentricWorkforce Query Builder: Joins and Tables

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

Page 31: SentricWorkforce Query Builder: Joins and Tables

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

Page 32: SentricWorkforce Query Builder: Joins and Tables

Joins and Criteria Filters

The Criteria panel is not configured to allow a NULL value filter as you can see below

Page 33: SentricWorkforce Query Builder: Joins and Tables

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

Page 34: SentricWorkforce Query Builder: Joins and Tables

Joins and Criteria Filters

The Basics of a SQL Select Statement

Page 35: SentricWorkforce Query Builder: Joins and Tables

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

Page 36: SentricWorkforce Query Builder: Joins and Tables

Joins and Criteria Filters

To add a NULL criteria3. Add an ‘OR’ for a null value to the criteria for the

outer joined table

Page 37: SentricWorkforce Query Builder: Joins and Tables

Joins and Criteria Filters

To add a NULL criteria4. By adding the OR IS NULL criteria the left outer join

becomes valid again

Page 38: SentricWorkforce Query Builder: Joins and Tables

Joins and Criteria Filters

Page 39: SentricWorkforce Query Builder: Joins and Tables

Adding a Filter to a Join

Page 40: SentricWorkforce Query Builder: Joins and Tables

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

Page 41: SentricWorkforce Query Builder: Joins and Tables

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

Page 42: SentricWorkforce Query Builder: Joins and Tables

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

Page 43: SentricWorkforce Query Builder: Joins and Tables

Joins and Criteria Filters

Query to get Employee and Spouse – Step 1

Page 44: SentricWorkforce Query Builder: Joins and Tables

Joins and Criteria Filters

Query to get Employee and Spouse – Step 1

Page 45: SentricWorkforce Query Builder: Joins and Tables

Joins and Criteria Filters

Query to get Employee and Spouse – Step 1

Page 46: SentricWorkforce Query Builder: Joins and Tables

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'))

Page 47: SentricWorkforce Query Builder: Joins and Tables

Joins and Criteria Filters

Query to get Employee and Spouse – Step 2

Page 48: SentricWorkforce Query Builder: Joins and Tables

Joins and Criteria Filters

Query to get Employee and Spouse – • If you make the MISTAKE of adding the

filter in the filter pane…

Page 49: SentricWorkforce Query Builder: Joins and Tables

Joins and Criteria Filters

Query to get Employee and Spouse – • Your results will only show employees with

spouses – NOT THE DESIRED RESULT!

Page 50: SentricWorkforce Query Builder: Joins and Tables

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

Page 51: SentricWorkforce Query Builder: Joins and Tables

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')

Page 52: SentricWorkforce Query Builder: Joins and Tables

Joins and Criteria Filters

Query to get Employee and Spouse – Step 3

Page 53: SentricWorkforce Query Builder: Joins and Tables

Joins and Criteria Filters

Query to get Employee and Spouse – Step 3

Page 54: SentricWorkforce Query Builder: Joins and Tables

The more you understand the joins the clearer the picture becomes

Page 55: SentricWorkforce Query Builder: Joins and Tables

The more you understand the joins the clearer the picture becomes

Page 56: SentricWorkforce Query Builder: Joins and Tables

The more you understand the joins the clearer the picture becomes

Page 57: SentricWorkforce Query Builder: Joins and Tables

The more you understand the joins the clearer the picture becomes

Page 58: SentricWorkforce Query Builder: Joins and Tables

The more you understand the joins the clearer the picture becomes

Page 59: SentricWorkforce Query Builder: Joins and Tables

The more you understand the joins the clearer the picture becomes

Page 60: SentricWorkforce Query Builder: Joins and Tables

The more you understand the joins the clearer the picture becomes

Page 61: SentricWorkforce Query Builder: Joins and Tables

Absences

Test Your Knowledge

?? ?

Page 62: SentricWorkforce Query Builder: Joins and Tables

Questions

If you would like to ask a question please enter your question in the Chat box

Page 63: SentricWorkforce Query Builder: Joins and Tables

Thank You for attending!

Please take the survey following the Webinar


Top Related