-- Database by Doug
-- Douglas Kline
-- 1/30/2019
-- the SwitchOffset function
-- how to use switchoffset
-- function available beginning SQL 2008
-- see "Time Zones and DATETIMEOFFSET" video
-- an update to the previous video
-- thanks to a viewer who pointed this function out to me
-- you know who you are!
SELECT GETDATE() AS [now, somewhere]
-- my time zone is EST -05:00
-- the server is in the Azure US east data center (EST)
-- note that the GETDATE() returns the time GMT, i.e. -00:00
-- the returned time is 5 hours in the future (based on EST)
-- also note that GETDATE() does not contain the time zone,
-- it returns a datetime, which does not contain time zone information
SELECT SQL_VARIANT_PROPERTY(GETDATE(), 'BaseType')
-- you can the server datetime with time zone like this:
SELECT SYSDATETIMEOFFSET(),
SQL_VARIANT_PROPERTY(SYSDATETIMEOFFSET(), 'BaseType')
-- this result proves that Azure SQL returns UTC 00:00
-- my current database server happens to be
-- in the eastern time zone of the US, which is -05:00 UTC
-- so what is the actual time, in EST?
-- observe the difference between the following values
SELECT GETDATE() AS [Azure datetime GMT],
CAST (GETDATE() AS DATETIMEOFFSET) AS [converted Azure datetime GMT],
TODATETIMEOFFSET(GETDATE(), '-05:00') AS [todatetimeoffset result EST], -- but note no hour change
SYSDATETIMEOFFSET() AS [sysdatetimeoffset EST],
SWITCHOFFSET(SYSDATETIMEOFFSET(),'-05:00') AS [switchoffset] -- this is the right one
-- note that GETDATE() is not as accurate
-- for a couple of reasons
-- fewer decimal points
-- but also
-- datetimes' one-thousandths place is always 0, 3, or 7
-- from doc'n "Rounded to increments of .000, .003, or .007 seconds"
-- so, before SWITCHOFFSET existed, ...
SELECT SWITCHOFFSET(SYSDATETIMEOFFSET(),'-05:00') AS [EST the easy way],
TODATETIMEOFFSET(DATEADD(HOUR, -5, SYSDATETIMEOFFSET()), '-05:00') AS [EST the hard way]
-- so, thinking of a DATETIMEOFFSET data type as a complex object
-- with many different parts: year, month, day, hour, time zone, etc.
-- it looks like SWITCHOFFSET changes two things: time zone and hour
-- but let's say that my source datetimeoffset
-- is near a time part boundary,
-- for example, the end of the year
DECLARE @NewYearsEveEST AS DATETIMEOFFSET
DECLARE @NewYearsEveGMT AS DATETIMEOFFSET
SET @NewYearsEveEST = DATETIMEOFFSETFROMPARTS(2019,12,31,23,50,0,0,-5,0,7)
SET @NewYearsEveGMT = SWITCHOFFSET(@NewYearsEveEST,'+00:00')
SELECT @NewYearsEveEST AS [NYEveEST],
@NewYearsEveGMT AS [NYEveGMT]
-- note that the year, month, day, hour, and time zone changed
-- in summary
-- SWITCHOFFSET is really helpful to have
-- simpler code, likely more reliable
-- use SYSDATETIMEOFFSET to get max precision w/Offset
-- Database by Doug
-- Douglas Kline
-- 1/30/2019
-- the SwitchOffset function
Wednesday, January 30, 2019
The SwitchOffset Function
Monday, November 12, 2018
Understanding Relational Division in SQL
Here is the script to create the JobSkills database, with data, used in this example.
-- Database by Doug
-- Douglas Kline
-- 11/10/2018
-- Understanding Relational Division - a (relatively) simple example
USE JobSkills
-- show the diagram of the data model
-- explain
-- here are our applicants (note - 4 of them)
SELECT *
FROM Applicant
-- here are our skills (note - 4 of them)
SELECT *
FROM Skill
-- now what if every applicant had every skill
-- that would mean that there would be 16 (4 x 4)
-- records in the ApplicantSkill table
-- it would look something like this:
SELECT *
FROM Applicant
CROSS JOIN Skill
ORDER BY Applicant.applicantID,
Skill.skillID
-- if you haven't seen this before,
-- this is a cross join
-- a cross join of two tables creates all possible combinations
-- of the records in the two tables
-- with no condition - no requirement on lining up FKs and PKs
-- this is known as the *cartesian product* of all records in both tables
-- and you might recall that *product* is another way of saying
-- 'the result of multiplication'
-- in other words, CROSS JOIN is kind of *relational multiplication*
-- sort of (Applicant records) x (Skill records)
-- think of this in math terms a * b = c
-- where a is Applicants
-- b is Skills
-- and c is the result
-- now if you remember your algebra,
-- if a * b = c
-- that means that c / b = a
-- in our context 4 records times 4 records equals 16
-- so 16 divided by 4 should equal 4
-- let's try to do that!
-- I'm going to use a common table expression (CTE),
-- so you can tell there's nothing up my sleeve!
-- note that this is exactly the CROSS JOIN from above
; WITH [c] (applicantID, lastName, skillID, skillName)
AS
(
SELECT *
FROM Applicant
CROSS JOIN Skill
)
SELECT applicantID,
lastName
FROM [c]
GROUP BY applicantID,
lastName
HAVING COUNT(skillID) = (SELECT COUNT(*) FROM Skill)
-- this could also be rephrased as a subquery like this:
SELECT applicantID,
lastName
FROM (
SELECT *
FROM Applicant
CROSS JOIN Skill
) [c]
GROUP BY applicantID,
lastName
HAVING COUNT(skillID) = (SELECT COUNT(*) FROM Skill)
-- the logic is that I'm showing all applicants
-- that have the same number of skills
-- as are in the skill table
-- so if CROSS JOIN is the "relational multiplication" operator
-- what is the "relational division" operator?
-- unfortunately, there is no relational division operator
-- that's why we are using HAVING and COUNT to get the logical equivalent
-- summary so far
-- 3 applicants CROSS JOIN 4 skills = 12 results, e.g., everyone has every skill
-- then 12 results divided by 4 skills = 3 applicants, the original applicants
-- but this is maybe an unrealistically simple example
-- not all applicants will have all skills
-- what we'll have in reality is a job that requires a certain set of skills
-- then we'll have applicants that each have a certain set of skills
-- and what we want is a set of applicants that whose skills *match* the requirements
-- here is the data model
-- let's start simply, with a single job that requires two skills
-- and a set of applicants that each have a set of skills
-- I've cooked some data for that...
-- examining jobskill...
SELECT Job.jobID,
Job.jobName,
Skill.SkillID,
Skill.SkillName
FROM Job
JOIN JobSkill ON Job.jobID = JobSkill.jobID
JOIN Skill ON JobSkill.skillID = Skill.skillID
WHERE Job.JobID = 10
ORDER BY Job.jobID,
Skill.SkillID
-- this says that the software developer job
-- requires the sql and javascript skills
-- now let's cook up some applicants with some skills
SELECT Applicant.applicantID,
Applicant.lastName,
Skill.skillID,
Skill.skillName
FROM Applicant
JOIN ApplicantSkill ON Applicant.applicantID = ApplicantSkill.applicantID
JOIN Skill ON ApplicantSkill.skillID = Skill.skillid
ORDER BY Applicant.applicantID,
Skill.skillID
-- so it looks like viable candidates are Jones and Brown
-- Jones has exactly the right skills and no more
-- Brown has the right skills, plus one
-- so to find a candidate that satisfies our job...
-- a * b = c
-- candidateSkills = jobskills
-- candidates * skills = jobskills
-- candidates = jobskills / skills
-- this analogy is not perfect, in that * is not really a cross join
-- and candidateskills and jobskills are actual records,
-- for which we are trying to find alignment via joins
--
-- we'll solve this with the COUNT method we used before
SELECT Applicant.applicantID,
Applicant.lastName,
COUNT(jobSkill.SkillID) AS [MatchingSkillCount]
FROM ApplicantSkill
JOIN JobSkill ON ApplicantSkill.skillID = JobSkill.SkillID
JOIN Applicant ON Applicant.ApplicantID = ApplicantSkill.ApplicantID
WHERE JobSkill.JobID = 10
GROUP BY Applicant.applicantID,
Applicant.lastName
HAVING COUNT(jobSkill.SkillID) = (SELECT COUNT(*) FROM JobSkill WHERE JobSkill.jobID = 10)
ORDER BY Applicant.applicantID
-- note that the outer query limits the applicant skill count to only
-- skills that match exactly the skills for this job
-- and the subquery does the same
-- let's look at this in a more general sense:
SELECT Job.jobID,
Job.jobName,
Skill.SkillID,
Skill.SkillName
FROM Job
JOIN JobSkill ON Job.jobID = JobSkill.jobID
JOIN Skill ON JobSkill.skillID = Skill.skillID
ORDER BY Job.jobID,
Skill.SkillID
-- two jobs, each of which require two skills
-- disjoint skill set
-- same set of applicants
SELECT Applicant.applicantID,
Applicant.lastName,
Skill.skillID,
Skill.skillName
FROM Applicant
JOIN ApplicantSkill ON Applicant.applicantID = ApplicantSkill.applicantID
JOIN Skill ON ApplicantSkill.skillID = Skill.skillid
ORDER BY Applicant.applicantID,
Skill.skillID
-- and the number of matching applicant skills for each job
SELECT Applicant.applicantID,
Applicant.lastName,
Job.JobID,
Job.jobName,
COUNT(ApplicantSkill.SkillID) AS [MatchingJobSkillCount]
FROM ApplicantSkill
JOIN JobSkill ON ApplicantSkill.skillID = JobSkill.SkillID -- this is *key*
JOIN Job ON Job.jobID = JobSkill.JobID
JOIN Applicant ON Applicant.ApplicantID = ApplicantSkill.ApplicantID
GROUP BY Applicant.applicantID,
Applicant.lastName,
Job.JobID,
Job.jobName
ORDER BY Applicant.applicantID,
Job.JobID
-- now we'd like to limit this to the applicants with jobskill count
-- equal to the jobskill count of each job
-- the trick here is that the the HAVING subquery must be correlated
SELECT Applicant.applicantID,
Applicant.lastName,
Job.JobID,
Job.jobName,
COUNT(ApplicantSkill.SkillID) AS [MatchingJobSkillCount]
FROM ApplicantSkill
JOIN JobSkill ON ApplicantSkill.skillID = JobSkill.SkillID -- this is *key*
JOIN Job ON Job.jobID = JobSkill.JobID
JOIN Applicant ON Applicant.ApplicantID = ApplicantSkill.ApplicantID
GROUP BY Applicant.applicantID,
Applicant.lastName,
Job.JobID,
Job.jobName
HAVING COUNT(ApplicantSkill.SkillID) = (SELECT COUNT(js.skillID)
FROM JobSkill js
WHERE js.jobID = job.jobID)
ORDER BY Applicant.applicantID,
Job.JobID
-- perhaps this is clearer as a CTE:
; WITH [JobWithSkillCount] (jobID, skillCount)
AS
(
SELECT jobID,
COUNT(skillID)
FROM JobSkill
GROUP BY jobID
)
SELECT Applicant.applicantID,
Applicant.lastName,
Job.JobID,
Job.jobName,
COUNT(ApplicantSkill.SkillID) AS [MatchingJobSkillCount]
FROM ApplicantSkill
JOIN JobSkill ON ApplicantSkill.skillID = JobSkill.SkillID -- this is *key*
JOIN Job ON Job.jobID = JobSkill.JobID
JOIN Applicant ON Applicant.ApplicantID = ApplicantSkill.ApplicantID
JOIN [JobWithSkillCount] ON [JobWithSkillCount].jobID = job.jobID
GROUP BY Applicant.applicantID,
Applicant.lastName,
Job.JobID,
Job.jobName,
JobWithSkillCount.skillCount
HAVING COUNT(ApplicantSkill.SkillID) = JobWithSkillCount.skillCount
ORDER BY Applicant.applicantID,
Job.JobID
-- hopefully, you find this a helpful example for understanding relational division
-- A couple last comments
-- I've presented this from the employer point of view:
-- applicants who match job requirements
-- but it is exactly equivalent form an applicant point of view:
-- jobs that match skillsets
-- I'm not showing the "remainder" of the division
-- for example, Brown actually has *extra* skills!
-- this example shows applicants with the exact matching skillsets
-- and some may have extra skills
-- this is not necessarily the *most efficient* way to
-- do relational division
-- my goal was to create an example for understanding relational division
-- an performant solution would of course depend on many things:
-- the data volume, its statistical distribution, available indexes,
-- the schema, etc.
-- I hope that you found this helpful!
-- Database by Doug
-- Douglas Kline
-- 11/10/2018
-- Understanding Relational Division - a (relatively) simple example
JobSkills database schema and data for Understanding Relational Division
The script below creates a database called JobSkills with the following schema:

The script also creates records in each table that are consistent with the following demonstration:
Understanding Relational Division in SQL

The script also creates records in each table that are consistent with the following demonstration:
Understanding Relational Division in SQL
USE [JobSkills] GO /****** Object: Table [dbo].[Applicant] Script Date: 11/12/2018 9:10:48 AM ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO CREATE TABLE [dbo].[Applicant]( [applicantID] [int] NOT NULL, [lastName] [varchar](50) NOT NULL, CONSTRAINT [PK_Applicant] PRIMARY KEY CLUSTERED ( [applicantID] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO /****** Object: Table [dbo].[ApplicantSkill] Script Date: 11/12/2018 9:10:49 AM ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO CREATE TABLE [dbo].[ApplicantSkill]( [employeeSkillID] [int] NOT NULL, [applicantID] [int] NOT NULL, [skillID] [int] NOT NULL, CONSTRAINT [PK_ApplicantSkill] PRIMARY KEY CLUSTERED ( [employeeSkillID] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO /****** Object: Table [dbo].[Job] Script Date: 11/12/2018 9:10:49 AM ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO CREATE TABLE [dbo].[Job]( [jobID] [int] NOT NULL, [jobName] [varchar](50) NOT NULL, CONSTRAINT [PK_Job] PRIMARY KEY CLUSTERED ( [jobID] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO /****** Object: Table [dbo].[JobSkill] Script Date: 11/12/2018 9:10:49 AM ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO CREATE TABLE [dbo].[JobSkill]( [jobSkillID] [int] NOT NULL, [jobID] [int] NOT NULL, [skillID] [int] NOT NULL, CONSTRAINT [PK_JobSkill] PRIMARY KEY CLUSTERED ( [jobSkillID] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO /****** Object: Table [dbo].[Skill] Script Date: 11/12/2018 9:10:49 AM ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO CREATE TABLE [dbo].[Skill]( [skillID] [int] NOT NULL, [skillName] [varchar](50) NOT NULL, CONSTRAINT [PK_Skill] PRIMARY KEY CLUSTERED ( [skillID] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO INSERT [dbo].[Applicant] ([applicantID], [lastName]) VALUES (100, N'Kline') GO INSERT [dbo].[Applicant] ([applicantID], [lastName]) VALUES (101, N'Smith') GO INSERT [dbo].[Applicant] ([applicantID], [lastName]) VALUES (102, N'Jones') GO INSERT [dbo].[Applicant] ([applicantID], [lastName]) VALUES (103, N'Brown') GO INSERT [dbo].[ApplicantSkill] ([employeeSkillID], [applicantID], [skillID]) VALUES (1000, 100, 1) GO INSERT [dbo].[ApplicantSkill] ([employeeSkillID], [applicantID], [skillID]) VALUES (1001, 100, 2) GO INSERT [dbo].[ApplicantSkill] ([employeeSkillID], [applicantID], [skillID]) VALUES (1002, 101, 3) GO INSERT [dbo].[ApplicantSkill] ([employeeSkillID], [applicantID], [skillID]) VALUES (1003, 102, 1) GO INSERT [dbo].[ApplicantSkill] ([employeeSkillID], [applicantID], [skillID]) VALUES (1004, 102, 4) GO INSERT [dbo].[ApplicantSkill] ([employeeSkillID], [applicantID], [skillID]) VALUES (1005, 103, 1) GO INSERT [dbo].[ApplicantSkill] ([employeeSkillID], [applicantID], [skillID]) VALUES (1006, 103, 4) GO INSERT [dbo].[ApplicantSkill] ([employeeSkillID], [applicantID], [skillID]) VALUES (1007, 103, 3) GO INSERT [dbo].[ApplicantSkill] ([employeeSkillID], [applicantID], [skillID]) VALUES (1008, 103, 2) GO INSERT [dbo].[Job] ([jobID], [jobName]) VALUES (10, N'Software Developer') GO INSERT [dbo].[Job] ([jobID], [jobName]) VALUES (11, N'Business Analyst') GO INSERT [dbo].[Job] ([jobID], [jobName]) VALUES (12, N'Data Analyst') GO INSERT [dbo].[JobSkill] ([jobSkillID], [jobID], [skillID]) VALUES (100, 10, 1) GO INSERT [dbo].[JobSkill] ([jobSkillID], [jobID], [skillID]) VALUES (101, 10, 4) GO INSERT [dbo].[JobSkill] ([jobSkillID], [jobID], [skillID]) VALUES (102, 11, 2) GO INSERT [dbo].[JobSkill] ([jobSkillID], [jobID], [skillID]) VALUES (103, 11, 3) GO INSERT [dbo].[Skill] ([skillID], [skillName]) VALUES (1, N'SQL') GO INSERT [dbo].[Skill] ([skillID], [skillName]) VALUES (2, N'Public Speaking') GO INSERT [dbo].[Skill] ([skillID], [skillName]) VALUES (3, N'Project Management') GO INSERT [dbo].[Skill] ([skillID], [skillName]) VALUES (4, N'Javascript') GO ALTER TABLE [dbo].[ApplicantSkill] WITH CHECK ADD CONSTRAINT [FK_ApplicantSkill_Applicant] FOREIGN KEY([applicantID]) REFERENCES [dbo].[Applicant] ([applicantID]) GO ALTER TABLE [dbo].[ApplicantSkill] CHECK CONSTRAINT [FK_ApplicantSkill_Applicant] GO ALTER TABLE [dbo].[ApplicantSkill] WITH CHECK ADD CONSTRAINT [FK_ApplicantSkill_Skill] FOREIGN KEY([skillID]) REFERENCES [dbo].[Skill] ([skillID]) GO ALTER TABLE [dbo].[ApplicantSkill] CHECK CONSTRAINT [FK_ApplicantSkill_Skill] GO ALTER TABLE [dbo].[JobSkill] WITH CHECK ADD CONSTRAINT [FK_JobSkill_Job] FOREIGN KEY([jobID]) REFERENCES [dbo].[Job] ([jobID]) GO ALTER TABLE [dbo].[JobSkill] CHECK CONSTRAINT [FK_JobSkill_Job] GO ALTER TABLE [dbo].[JobSkill] WITH CHECK ADD CONSTRAINT [FK_JobSkill_Skill] FOREIGN KEY([skillID]) REFERENCES [dbo].[Skill] ([skillID]) GO ALTER TABLE [dbo].[JobSkill] CHECK CONSTRAINT [FK_JobSkill_Skill] GO
Saturday, October 20, 2018
Finding Unmatched Records in SQL
-- Database by Doug
-- Douglas Kline
-- 10/19/2018
--
-- Finding unmatched records
-- LEFT JOIN... WHERE IS NULL
-- suppose you want to find records in one table
-- that do not match records in another table
-- some practical examples:
-- products that are not in a category
-- (in Products table, but not in Categories)
-- Categories that have no products
-- (in Categories table, but not in Products table)
-- two main cases,
-- first case: look for the NULL
-- products that are not in a category
SELECT ProductID
FROM Products
WHERE CategoryID IS NULL
-- this is fairly simple, because
-- everything we need is in a single table
-- we don't need to inspect another table
-- this is when the Foreign Key is
-- in the table of interest
-- Categories that have no products
-- this is tougher
-- since which Categories to display
-- depends on what's in the Products table
-- here's the pattern, then we'll build up
-- to why it works
SELECT Categories.CategoryName
FROM Categories
LEFT JOIN Products ON Categories.CategoryID = Products.CategoryID
WHERE Products.ProductID IS NULL
-- the pattern is
-- <table of interest> LEFT JOIN <unmatched table>
-- WHERE <unmatched table>.<primary key> IS NULL
-- consider this, normal join
SELECT Categories.CategoryName,
Categories.CategoryID,
Products.CategoryID,
Products.ProductID,
Products.ProductName
FROM Categories
JOIN Products ON Categories.CategoryID = Products.CategoryID
ORDER BY Categories.CategoryName
-- note 77 records
-- now the LEFT JOIN
SELECT Categories.CategoryName,
Categories.CategoryID,
Products.CategoryID,
Products.ProductID,
Products.ProductName
FROM Categories
LEFT JOIN Products ON Categories.CategoryID = Products.CategoryID
ORDER BY Categories.CategoryName
-- now 78 records
-- and note the Office Supplies category with no Product
-- the record has been replaced with a *NULL record*
-- the LEFT JOIN includes Categories without matching Products
-- Categories LEFT JOIN Products -- Categories is on the LEFT
-- how do I know that it's really a *NULL record*
-- and not just NULLs in the fields? which might be ok
-- because Products.ProductID is NULL, which can't happen for a PK
-- so, let's limit to just that record
SELECT Categories.CategoryName,
Categories.CategoryID,
Products.CategoryID,
Products.ProductID,
Products.ProductName
FROM Categories
LEFT JOIN Products ON Categories.CategoryID = Products.CategoryID
WHERE Products.ProductID IS NULL
ORDER BY Categories.CategoryName
-- and paring it back to the simple form
SELECT Categories.CategoryName
FROM Categories
LEFT JOIN Products ON Categories.CategoryID = Products.CategoryID
WHERE Products.ProductID IS NULL
ORDER BY Categories.CategoryName
-- Database by Doug
-- Douglas Kline
-- 10/19/2018
--
-- Finding unmatched records
-- LEFT JOIN... WHERE IS NULL
Subscribe to:
Posts (Atom)