Created
November 23, 2021 15:47
-
-
Save stevesohcot/24f1f15b9f934453050e4444daa82da0 to your computer and use it in GitHub Desktop.
SQL - New Hires (one query)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| -- New Hires | |
| DECLARE @minDate DATE = (SELECT MIN(ReportDate) FROM dbo.Rosters) | |
| SET @minDate = CAST( DATEADD(month, DATEDIFF(month, 0, @minDate), 0) AS DATE) | |
| --SELECT @minDate | |
| SELECT | |
| tblPrevious.FirstOfPrevMonth, tblPrevious.EmplID, tblPrevious.Name | |
| FROM ( | |
| SELECT tblPrevious.EmplID, tblPrevious.Name, tblPrevious.LevelNumber, | |
| DATEADD( month, -1, CAST( DATEADD(month, DATEDIFF(month, 0, tblPrevious.ReportDate), 0) AS DATE) ) AS FirstOfPrevMonth | |
| FROM dbo.Rosters tblPrevious | |
| ) AS tblPrevious | |
| LEFT JOIN ( | |
| SELECT tblCurrent.EmplID, tblCurrent.Name, tblCurrent.LevelNumber, | |
| CAST( DATEADD(month, DATEDIFF(month, 0, tblCurrent.ReportDate), 0) AS DATE) AS FirstOfMonth | |
| FROM dbo.Rosters tblCurrent | |
| ) tblCurrent ON | |
| tblCurrent.EmplID = tblPrevious.EmplID | |
| AND tblPrevious.FirstOfPrevMonth = tblCurrent.FirstOfMonth | |
| WHERE | |
| tblCurrent.EmplID IS NULL | |
| AND tblPrevious.FirstOfPrevMonth >= @minDate -- *all* entries will be there in the minimum month, so exclude that |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment