Checking the difference between isnull and coalesce
This file contains 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 @Date date = DATEADD(DAY, -1, GETDATE()); | |
SELECT SpentDate, | |
SUM(SpentAmount) AS Total | |
FROM dbo.RunningTotals | |
WHERE SpentDate >= ISNULL(@Date, GETDATE()) | |
GROUP BY | |
GROUPING SETS( | |
SpentDate, -- Per date, | |
() -- To include the overall total as well. | |
); | |
GO | |
DECLARE @Date date = NULL; | |
SELECT SpentDate, | |
SUM(SpentAmount) AS Total | |
FROM dbo.RunningTotals | |
WHERE SpentDate >= ISNULL(@Date, GETDATE()) | |
GROUP BY | |
GROUPING SETS( | |
SpentDate, | |
() | |
); | |
GO | |
DECLARE @Date date = DATEADD(DAY, -1, GETDATE()); | |
SELECT SpentDate, | |
SUM(SpentAmount) AS Total | |
FROM dbo.RunningTotals | |
WHERE SpentDate >= COALESCE(@Date, GETDATE()) | |
GROUP BY | |
GROUPING SETS( | |
SpentDate, | |
() | |
); | |
GO | |
DECLARE @Date date = NULL; | |
SELECT SpentDate, | |
SUM(SpentAmount) AS Total | |
FROM dbo.RunningTotals | |
WHERE SpentDate >= COALESCE(@Date, GETDATE()) | |
GROUP BY | |
GROUPING SETS( | |
SpentDate, | |
() | |
); | |
GO |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment