在我的Sql数据库中,我有表
1 Account_Customer -帐户ID -顾客ID -Account_CustomerID
2个帐户 -帐户ID - 剩余部分
3位客户 -顾客ID -名称 -性别 -age
4贷款 -LoanID -量 -BranchName
5 Loan_Customer -Loan_CustomerID -LoanID -CustomerID
我想写一个存储过程 列出客户及其账户数量,总账户余额,贷款数量和贷款总额,居住在特定城市,给定性别和年龄,以及在特定分支机构拥有账户和/或贷款。
我可以在我的程序中完成帐户数和总帐户余额,但我需要一个存储过程来完成我的工作。
非常感谢任何帮助。
答案 0 :(得分:1)
好吧,让我们给这个打击(尽管我仍然认为我们错过了几件)
CREATE PROCEDURE SelectCustomerDetailsBySex
@Sex <your data type here>
AS
BEGIN
SELECT cus.CustomerID,
cus.Name,
COUNT(acc.AccountID) AS AccountCount,
SUM(acc.Balance) AS AccountBalance,
COUNT(loa.LoanID) AS LoanCount,
SUM(loa.Amount) AS LoanTotal
FROM Customers cus
LEFT OUTER JOIN Account_Customer ac ON cus.CustomerID = ac.CustomerID
LEFT OUTER JOIN Accounts acc ON ac.AccountID = acc.AccountID
LEFT OUTER JOIN Loan_Customer lc ON cus.CustomerID = lc.CustomerID
LEFT OUTER JOIN Loans loa ON lc.LoanID = loa.LoanID
WHERE cus.Sex = @Sex
GROUP BY cus.CustomerID,
cus.Name;
END
这会作为一个例子,或者你希望我做另一个吗?