Skip to content

Instantly share code, notes, and snippets.

@shaneis
Last active October 9, 2017 12:47
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Embed
What would you like to do?
Checking the difference between isnull and coalesce
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