-
Notifications
You must be signed in to change notification settings - Fork 0
/
SQLQuery_Budget.sql
206 lines (193 loc) · 8.37 KB
/
SQLQuery_Budget.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
/*
--Creates the Main Budget Table for January
Create table Budget_01
(
TransactionID int primary key identity (1,1),
CategoryID int,
PlannedSpending smallmoney,
);
--Foreign Keys
ALTER TABLE Budget_01
ADD FOREIGN KEY (CategoryID) REFERENCES Category(CategoryID);
--Drop table budget_01
--Insert Data (In Progress)
INSERT Budget_01 ( PlannedSpending, CategoryID)
VALUES ( 50.00, 1);
INSERT Budget_01 ( PlannedSpending, CategoryID)
VALUES ( 25.00, 2);
INSERT Budget_01 ( PlannedSpending, CategoryID)
VALUES ( 1.00, 3);
INSERT Budget_01 ( PlannedSpending, CategoryID)
VALUES ( 100.00, 4);
INSERT Budget_01 ( PlannedSpending,CategoryID)
VALUES ( 50.00, 5);
INSERT Budget_01 ( PlannedSpending, CategoryID)
VALUES ( 120.00, 6);
INSERT Budget_01 ( PlannedSpending, CategoryID)
VALUES ( 50.00, 7);
*/
--ActualSpending Per Category by MONTH=Current + Amount Saved
USE Budget_2019;
SELECT C.CategoryID
, CategoryName
, PlannedSpending --BUDGET
, SUM(
CASE
WHEN C.CategoryID=1 and Month(Time) = Month(GetDate())-1 THEN Price
WHEN C.CategoryID=2 and Month(Time) = Month(GetDate())-1 THEN Price
WHEN C.CategoryID=3 and Month(Time) = Month(GetDate())-1 THEN Price
WHEN C.CategoryID=4 and Month(Time) = Month(GetDate())-1 THEN Price
WHEN C.CategoryID=5 and Month(Time) = Month(GetDate())-1 THEN Price
WHEN C.CategoryID=6 and Month(Time) = Month(GetDate())-1 THEN Price
WHEN C.CategoryID=7 and Month(Time) = Month(GetDate())-1 THEN Price
Else 0
END ) as ' Budget Last Month'
, SUM(
CASE
WHEN Month(Time)= Month(GetDate()) THEN Price
WHEN Month(Time)= Month(GetDate()) THEN Price
WHEN Month(Time)= Month(GetDate()) THEN Price
WHEN Month(Time)= Month(GetDate()) THEN Price
WHEN Month(Time)= Month(GetDate()) THEN Price
WHEN Month(Time)= Month(GetDate()) THEN Price
WHEN Month(Time)= Month(GetDate()) THEN Price
Else 0
END ) as 'Current Month'
, CAST( SUM(
CASE
WHEN C.CategoryID=1 and Month(Time) = Month(GetDate()) THEN Price
WHEN C.CategoryID=2 and Month(Time) = Month(GetDate()) THEN Price
WHEN C.CategoryID=3 and Month(Time) = Month(GetDate()) THEN Price
WHEN C.CategoryID=4 and Month(Time) = Month(GetDate()) THEN Price
WHEN C.CategoryID=5 and Month(Time) = Month(GetDate()) THEN Price
WHEN C.CategoryID=6 and Month(Time) = Month(GetDate()) THEN Price
WHEN C.CategoryID=7 and Month(Time) = Month(GetDate()) THEN Price
Else 0
END)
-SUM(
CASE
WHEN C.CategoryID=1 and Month(Time) = Month(GetDate())-1 THEN Price
WHEN C.CategoryID=2 and Month(Time) = Month(GetDate())-1 THEN Price
WHEN C.CategoryID=3 and Month(Time) = Month(GetDate())-1 THEN Price
WHEN C.CategoryID=4 and Month(Time) = Month(GetDate())-1 THEN Price
WHEN C.CategoryID=5 and Month(Time) = Month(GetDate())-1 THEN Price
WHEN C.CategoryID=6 and Month(Time) = Month(GetDate())-1 THEN Price
WHEN C.CategoryID=7 and Month(Time) = Month(GetDate())-1 THEN Price
Else 0
END ) as NVARCHAR(10)) as 'Difference'
--SUMMARY
, 'You spent $' +
--ACTUAL SPENDING
CAST(
SUM(
CASE
WHEN C.CategoryID=1 and Month(Time) = Month(GetDate()) THEN Price
WHEN C.CategoryID=2 and Month(Time) = Month(GetDate()) THEN Price
WHEN C.CategoryID=3 and Month(Time) = Month(GetDate()) THEN Price
WHEN C.CategoryID=4 and Month(Time) = Month(GetDate()) THEN Price
WHEN C.CategoryID=5 and Month(Time) = Month(GetDate()) THEN Price
WHEN C.CategoryID=6 and Month(Time) = Month(GetDate()) THEN Price
WHEN C.CategoryID=7 and Month(Time) = Month(GetDate()) THEN Price
Else 0
END )
as NVARCHAR(50)) + ' on ' + CategoryName + ', which is $'
--DIFFERENCE BETWEEN CURRENT AND PREV MONTH
+ CAST(
CASE
WHEN
SUM(
CASE
WHEN C.CategoryID=1 and Month(Time) = Month(GetDate()) THEN Price
WHEN C.CategoryID=2 and Month(Time) = Month(GetDate()) THEN Price
WHEN C.CategoryID=3 and Month(Time) = Month(GetDate()) THEN Price
WHEN C.CategoryID=4 and Month(Time) = Month(GetDate()) THEN Price
WHEN C.CategoryID=5 and Month(Time) = Month(GetDate()) THEN Price
WHEN C.CategoryID=6 and Month(Time) = Month(GetDate()) THEN Price
WHEN C.CategoryID=7 and Month(Time) = Month(GetDate()) THEN Price
Else 0
END)
-SUM(
CASE
WHEN C.CategoryID=1 and Month(Time) = Month(GetDate())-1 THEN Price
WHEN C.CategoryID=2 and Month(Time) = Month(GetDate())-1 THEN Price
WHEN C.CategoryID=3 and Month(Time) = Month(GetDate())-1 THEN Price
WHEN C.CategoryID=4 and Month(Time) = Month(GetDate())-1 THEN Price
WHEN C.CategoryID=5 and Month(Time) = Month(GetDate())-1 THEN Price
WHEN C.CategoryID=6 and Month(Time) = Month(GetDate())-1 THEN Price
WHEN C.CategoryID=7 and Month(Time) = Month(GetDate())-1 THEN Price
Else 0
END ) > 0 --IF MONEY SPENT CURRENT MONTH IS GREATER THAN MONEY SPENT PREVIOUS MONTH [USE WORDS 'MORE THAN']
THEN
CAST(
SUM(
CASE
WHEN C.CategoryID=1 and Month(Time) = Month(GetDate()) THEN Price
WHEN C.CategoryID=2 and Month(Time) = Month(GetDate()) THEN Price
WHEN C.CategoryID=3 and Month(Time) = Month(GetDate()) THEN Price
WHEN C.CategoryID=4 and Month(Time) = Month(GetDate()) THEN Price
WHEN C.CategoryID=5 and Month(Time) = Month(GetDate()) THEN Price
WHEN C.CategoryID=6 and Month(Time) = Month(GetDate()) THEN Price
WHEN C.CategoryID=7 and Month(Time) = Month(GetDate()) THEN Price
Else 0
END)
-SUM(
CASE
WHEN C.CategoryID=1 and Month(Time) = Month(GetDate())-1 THEN Price
WHEN C.CategoryID=2 and Month(Time) = Month(GetDate())-1 THEN Price
WHEN C.CategoryID=3 and Month(Time) = Month(GetDate())-1 THEN Price
WHEN C.CategoryID=4 and Month(Time) = Month(GetDate())-1 THEN Price
WHEN C.CategoryID=5 and Month(Time) = Month(GetDate())-1 THEN Price
WHEN C.CategoryID=6 and Month(Time) = Month(GetDate())-1 THEN Price
WHEN C.CategoryID=7 and Month(Time) = Month(GetDate())-1 THEN Price
Else 0
END)
as nvarchar(50)) + ' more than previous month '
ELSE
CAST(
(SUM(
CASE
WHEN C.CategoryID=1 and Month(Time) = Month(GetDate()) THEN Price
WHEN C.CategoryID=2 and Month(Time) = Month(GetDate()) THEN Price
WHEN C.CategoryID=3 and Month(Time) = Month(GetDate()) THEN Price
WHEN C.CategoryID=4 and Month(Time) = Month(GetDate()) THEN Price
WHEN C.CategoryID=5 and Month(Time) = Month(GetDate()) THEN Price
WHEN C.CategoryID=6 and Month(Time) = Month(GetDate()) THEN Price
WHEN C.CategoryID=7 and Month(Time) = Month(GetDate()) THEN Price
Else 0
END)
-SUM(
CASE
WHEN C.CategoryID=1 and Month(Time) = Month(GetDate())-1 THEN Price
WHEN C.CategoryID=2 and Month(Time) = Month(GetDate())-1 THEN Price
WHEN C.CategoryID=3 and Month(Time) = Month(GetDate())-1 THEN Price
WHEN C.CategoryID=4 and Month(Time) = Month(GetDate())-1 THEN Price
WHEN C.CategoryID=5 and Month(Time) = Month(GetDate())-1 THEN Price
WHEN C.CategoryID=6 and Month(Time) = Month(GetDate())-1 THEN Price
WHEN C.CategoryID=7 and Month(Time) = Month(GetDate())-1 THEN Price
Else 0
END))*-1
as NVARCHAR(50)) + ' less than previous month '
end
as nvarchar(50)) + ' at $' +
--PREVIOUS MONTH SPENDING
CAST(
SUM(
CASE
WHEN C.CategoryID=1 and Month(Time) = Month(GetDate())-1 THEN Price
WHEN C.CategoryID=2 and Month(Time) = Month(GetDate())-1 THEN Price
WHEN C.CategoryID=3 and Month(Time) = Month(GetDate())-1 THEN Price
WHEN C.CategoryID=4 and Month(Time) = Month(GetDate())-1 THEN Price
WHEN C.CategoryID=5 and Month(Time) = Month(GetDate())-1 THEN Price
WHEN C.CategoryID=6 and Month(Time) = Month(GetDate())-1 THEN Price
WHEN C.CategoryID=7 and Month(Time) = Month(GetDate())-1 THEN Price
Else 0
END
)
as NVARCHAR(50)) as Summary
From
Budget_01 as Budget
INNER JOIN Category C
ON Budget.CategoryID = C.CategoryID
INNER JOIN t_expense Expense
ON Budget.CategoryID = Expense.CategoryID
GROUP BY C.CategoryID, CategoryName, PlannedSpending