-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqueries.sql
More file actions
46 lines (44 loc) · 872 Bytes
/
queries.sql
File metadata and controls
46 lines (44 loc) · 872 Bytes
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
-- View for income and expense reports
CREATE VIEW IncomeExpenseReport AS
SELECT
u.UserID,
u.FirstName,
u.LastName,
a.AccountName,
t.CategoryID,
t.Amount,
t.Date,
t.Description
FROM
Transactions t
JOIN
Users u ON t.UserID = u.UserID
JOIN
Accounts a ON t.AccountID = a.AccountID
ORDER BY
t.Date;
-- Query to get the balance of all accounts for a user
SELECT
u.UserID,
u.FirstName,
u.LastName,
SUM(a.Balance) AS TotalBalance
FROM
Users u
JOIN
Accounts a ON u.UserID = a.UserID
GROUP BY
u.UserID, u.FirstName, u.LastName;
-- Query to get transactions for a specific account
SELECT
t.TransactionID,
t.Amount,
t.Date,
t.Description,
c.CategoryName
FROM
Transactions t
JOIN
Categories c ON t.CategoryID = c.CategoryID
WHERE
t.AccountID = 1;