Created
September 21, 2015 20:20
-
-
Save dcs619/2e959996b895d336a8e6 to your computer and use it in GitHub Desktop.
Perform an optional join on NULL parameter
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
| DECLARE @Today datetime = GETDATE() | |
| DECLARE @CampusId int | |
| SELECT @CampusId = 2 | |
| DECLARE @CampusGuid uniqueidentifier | |
| SELECT @CampusGuid = '6246DDEF-8AC9-4C25-B011-430CEE590F0F' | |
| SELECT COUNT(DISTINCT PersonAliasId) | |
| FROM Attendance A | |
| INNER JOIN ( | |
| -- Return a value (or return null) | |
| SELECT ( | |
| SELECT ID | |
| FROM Campus | |
| WHERE GUID = @CampusGuid | |
| ) AS ID | |
| ) CampusFilter | |
| ON A.CampusId = ISNULL(@CampusId, A.CampusId) | |
| WHERE DidAttend = 1 | |
| -- Filter by Date Range | |
| AND StartDateTime > DATEADD(month, -6, @Today) | |
| AND StartDateTime < @Today |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment