I am dealing with some queries for my assignment and any help would be greatly appreciated.
- List branches together with the number of employees and assigned to them customers, total loan amount, total account balance, assets for branches residing in the given city.
- List customers who made account and loan operations in the given time period
- List employees and the number of served by each of them customers in the given time period
I guess a simple example would be enough for me to solve rest.
Here’s what I’ve tried so far for the first one:
ALTER PROCEDURE [dbo].[SelecBranchesByCity]
(@City varchar(50))
AS
select
Br.Name as BranchName,
COUNT(emps.ID) as NumberOfEmployee,
SUM(emps.NumberOfCustomers) as TotalCustomers,
SUM(lo.Amount) as TotalAmountOfLoan,
SUM(acc.Balance) as TotalBalance,
Br.Assets as Assets
from Branches Br
left outer join Employees emps on emps.[BranchName] = Br.Name
left outer join Loans lo on lo.[BranchName] = Br.Name
left outer join Accounts acc on acc.[BranchName] = Br.Name
where
Br.[Address] like '%'+@City+'%'
GROUP BY
Br.ID,
Br.Name,
Br.Assets
Here is the schema !

Schema is frightening. Many-to-many on customers-loans? Many-to-many on customers-accounts? Why?? Employees has a
branchnamecolumn instead of an FK relationship tobranches.loan_operationshas FK toemployees!? I don’t mean to stray from the topic or sound flippant, but there are so many anti-patterns here I don’t even know where to begin. But I will try to help with the specific question anyway.Q1:
I’ll note that you have an
amountcolumn in bothloansandloan_operations. It’s hard to know what the difference is between these two – it’s entirely possible thatloansshouldn’t have this column at all, and instead it should be summed from the column inloan_operations.Q2:
Q3: