Skip to content

Commit bfd395a

Browse files
authored
Update BudgetSummary.sql
Fixing conditions to match Totals By Fund
1 parent 67983b8 commit bfd395a

1 file changed

Lines changed: 71 additions & 69 deletions

File tree

Finance Reports/BudgetSummary.sql

Lines changed: 71 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ DECLARE @BudgetJson nvarchar(MAX);
1212
SET @Year = CAST(@p1 AS INT);
1313

1414

15-
IF @Year = 1
15+
IF @Year = 1
1616
SET @Today = getdate();
1717
ELSE IF @Year > 1000
1818
SET @Today = DATEFROMPARTS(@Year, 12, 31);
@@ -30,89 +30,91 @@ SET @StartDateMo = DATEFROMPARTS(@Year, @Month, 01)
3030
SET @PctOfYear = (DATEPART(dayofyear, @Today)) * 1.0 / (DATEPART(dayofyear, DATEFROMPARTS(@Year, 12, 31)));
3131
SET @BudgetJson = (SELECT TOP 1 Body FROM Content WHERE Name = 'Budgets.json');
3232

33-
SELECT c.FundId,
34-
SUM(c.contributionAmount) AS giving,
35-
DATEPART(MONTH, c.ContributionDate) as Month,
36-
DATEPART(YEAR, c.ContributionDate) as Year
33+
SELECT c.FundId,
34+
SUM(c.contributionAmount) AS giving,
35+
DATEPART(MONTH, c.ContributionDate) as Month,
36+
DATEPART(YEAR, c.ContributionDate) as Year
3737
INTO #contribs
38-
FROM Contribution c
39-
WHERE (c.ContributionDate >= @startDate AND c.ContributionDate <= @Today) OR
40-
(c.ContributionDate >= @startDatePY AND c.ContributionDate <= @TodayPY)
38+
FROM Contribution c
39+
WHERE ((c.ContributionDate >= @startDate AND c.ContributionDate <= @Today) OR
40+
(c.ContributionDate >= @startDatePY AND c.ContributionDate <= @TodayPY)) AND
41+
c.ContributionTypeId <> 6 -- exclude returned checks
42+
AND c.ContributionStatusId <> 2 -- exclude unapproved(?)
4143
GROUP BY c.FundId, DATEPART(MONTH, c.ContributionDate), DATEPART(YEAR, c.ContributionDate);
4244

4345
SELECT SUM(c.giving) as Giving,
44-
fsf.FundSetId,
45-
c.month as Month,
46-
c.year as Year,
47-
COALESCE(fs.description, cf.FundName) as Fund,
48-
SUM(CAST(CAST(JSON_VALUE(@BudgetJson, CONCAT('$."', @Year, '"."', cf.FundId, '"')) AS MONEY) AS DECIMAL(18,2))) AS Budget
46+
fsf.FundSetId,
47+
c.month as Month,
48+
c.year as Year,
49+
COALESCE(fs.description, cf.FundName) as Fund,
50+
SUM(CAST(CAST(JSON_VALUE(@BudgetJson, CONCAT('$."', @Year, '"."', cf.FundId, '"')) AS MONEY) AS DECIMAL(18,2))) AS Budget
4951
INTO #grouped
5052
FROM #contribs c
51-
LEFT JOIN ContributionFund cf ON c.FundId = cf.FundId
52-
LEFT JOIN FundSetFunds fsf ON c.FundId = fsf.FundId
53-
LEFT JOIN FundSets fs ON fsf.FundSetId = fs.FundSetId
53+
LEFT JOIN ContributionFund cf ON c.FundId = cf.FundId
54+
LEFT JOIN FundSetFunds fsf ON c.FundId = fsf.FundId
55+
LEFT JOIN FundSets fs ON fsf.FundSetId = fs.FundSetId
5456
GROUP BY c.Month, c.year, fsf.FundSetId, COALESCE(fs.description, cf.FundName);
5557

5658
SELECT *
57-
INTO #summed
59+
INTO #summed
5860
FROM (
59-
SELECT
60-
SUM(g.Giving) as Giving,
61-
g.Fund,
62-
g.Budget,
63-
g.Year
64-
FROM #grouped g
65-
WHERE (@year <= 2023 AND g.FundSetId IN (2, 5, 6))
66-
OR (@year >= 2024 AND g.FundSetId IN (1))
67-
GROUP BY g.Fund, g.Budget, g.Year
61+
SELECT
62+
SUM(g.Giving) as Giving,
63+
g.Fund,
64+
g.Budget,
65+
g.Year
66+
FROM #grouped g
67+
WHERE (@year <= 2023 AND g.FundSetId IN (2, 5, 6))
68+
OR (@year >= 2024 AND g.FundSetId IN (1))
69+
GROUP BY g.Fund, g.Budget, g.Year
6870

69-
UNION
70-
71-
SELECT
72-
SUM(g.Giving) as Giving,
73-
CONCAT(g.Fund, ' (', @MonthName, ')') As Fund,
74-
g.Budget,
75-
g.Year
76-
FROM #grouped g
77-
WHERE g.month = @Month
78-
AND (
79-
(@year <= 2023 AND g.FundSetId IN (4))
80-
OR (@year >= 2024 AND g.FundSetId IN (4))
81-
)
82-
GROUP BY g.Fund, g.Budget, g.year
83-
) as summed;
71+
UNION
72+
73+
SELECT
74+
SUM(g.Giving) as Giving,
75+
CONCAT(g.Fund, ' (', @MonthName, ')') As Fund,
76+
g.Budget,
77+
g.Year
78+
FROM #grouped g
79+
WHERE g.month = @Month
80+
AND (
81+
(@year <= 2023 AND g.FundSetId IN (4))
82+
OR (@year >= 2024 AND g.FundSetId IN (4))
83+
)
84+
GROUP BY g.Fund, g.Budget, g.year
85+
) as summed;
8486

8587
SELECT c.Fund,
86-
c.Giving AS 'Giving YTD',
87-
c.Budget AS 'Budget FY',
88-
c.Budget * @PctOfYear AS 'Budget YTD',
89-
c.Giving - c.Budget * @PctOfYear AS 'YTD Difference',
90-
(c.Giving - c.Budget * @PctOfYear) * 100 / (c.Budget * @PctOfYear) AS 'YTD Pct',
91-
c.Giving - c.Budget AS 'FY Difference',
92-
(c.Giving - c.Budget) * 100 / c.Budget AS 'FY Pct',
93-
p.Giving AS 'Giving PYTD',
94-
c.Giving - p.Giving AS 'PY Difference',
95-
(c.Giving - p.Giving) * 100 / p.Giving AS 'PY Pct',
96-
CAST(@Today AS DATE) AS 'As Of',
97-
@year as 'Year'
88+
c.Giving AS 'Giving YTD',
89+
c.Budget AS 'Budget FY',
90+
c.Budget * @PctOfYear AS 'Budget YTD',
91+
c.Giving - c.Budget * @PctOfYear AS 'YTD Difference',
92+
(c.Giving - c.Budget * @PctOfYear) * 100 / (c.Budget * @PctOfYear) AS 'YTD Pct',
93+
c.Giving - c.Budget AS 'FY Difference',
94+
(c.Giving - c.Budget) * 100 / c.Budget AS 'FY Pct',
95+
p.Giving AS 'Giving PYTD',
96+
c.Giving - p.Giving AS 'PY Difference',
97+
(c.Giving - p.Giving) * 100 / p.Giving AS 'PY Pct',
98+
CAST(@Today AS DATE) AS 'As Of',
99+
@year as 'Year'
98100
FROM #summed c
99-
LEFT JOIN #summed p ON c.Fund = p.Fund AND c.Year = p.Year + 1
101+
LEFT JOIN #summed p ON c.Fund = p.Fund AND c.Year = p.Year + 1
100102
WHERE c.Budget > 0 AND c.year = @year
101-
UNION
103+
UNION
102104
SELECT c.Fund,
103-
c.Giving AS 'Giving YTD',
104-
null AS 'Budget FY',
105-
null AS 'Budget YTD',
106-
null AS 'YTD Difference',
107-
null AS 'YTD Pct',
108-
null AS 'FY Difference',
109-
null AS 'FY Pct',
110-
p.Giving AS 'Giving PYTD',
111-
c.Giving - p.Giving AS 'PY Difference',
112-
(c.Giving - p.Giving) * 100 / p.Giving AS 'PY Pct',
113-
CAST(@Today AS DATE) AS 'As Of',
114-
@year as 'Year'
105+
c.Giving AS 'Giving YTD',
106+
null AS 'Budget FY',
107+
null AS 'Budget YTD',
108+
null AS 'YTD Difference',
109+
null AS 'YTD Pct',
110+
null AS 'FY Difference',
111+
null AS 'FY Pct',
112+
p.Giving AS 'Giving PYTD',
113+
c.Giving - p.Giving AS 'PY Difference',
114+
(c.Giving - p.Giving) * 100 / p.Giving AS 'PY Pct',
115+
CAST(@Today AS DATE) AS 'As Of',
116+
@year as 'Year'
115117
FROM #summed c
116-
LEFT JOIN #summed p ON c.Fund = p.Fund AND c.Year = p.Year + 1
118+
LEFT JOIN #summed p ON c.Fund = p.Fund AND c.Year = p.Year + 1
117119
WHERE c.Budget IS NULL AND c.year = @year
118-
ORDER BY c.Giving DESC
120+
ORDER BY c.Giving DESC

0 commit comments

Comments
 (0)