Sunteți pe pagina 1din 7

Define a constraint to enforce in case of new records concerning the

TransactionFees table so that individual fees are smaller than transaction amount
for any given transaction. In addition, starting with 2013, individual fees for Cash
withdrawal (ATM)-RON transactions cannot exceed 25 RON.
create trigger [dbo].[S2_Task2_prof] on [dbo].[TransactionFees]
for insert,update
as
begin
if exists(select *
from inserted i inner join
[dbo].[AccountTransactions] t on t.[TransactionId]=i.[TransactionId]
where ([FeeAmount]>25 and
[TransactionType]='Cash withdrawal (ATM)- RON'
and year([TransactionDate])>=2013)
or ([Amount]<[FeeAmount])
)
begin
rollback
raiserror('individual fees are smaller than transaction amount for any given transaction.
In addition, starting with 2013, individual fees for Cash withdrawal (ATM)-RON transactions
cannot exceed 25 RON.',
15,2)
end
end

Define a constraint to enforce in case of new records concerning the


AccountTransactions table so that transaction dates must be subsequent to the
opening date for any given account. In addition, Funds transfer transactions are
not allowed in case of Student deposit type of accounts.
create trigger [dbo].[S1_Task2_prof] on [dbo].[AccountTransactions]
for insert,update
as
begin
if exists(select * from inserted i inner join
[dbo].[BankAccounts] a on i.[AccountId]=a.[AccountId]
where ([OpeningDate]>[TransactionDate]) or
(lower([TransactionType])='funds transfer'
and lower([AccountType])='student deposit')
)
begin
rollback
raiserror('transaction dates must be subsequent to the opening date for any given
account.
In addition, Funds transfer transactions are not allowed in case of Student deposit type
of accounts.',
16,1)
end
end

Define a view to display the total transaction amount and total fee amount per
transaction type, both computed by considering transactions in the past 9 months
in case of accounts opened more than 3 years back by bank customers in Bucureti
and Sibiu. Results should be sorted in descending order by the total transaction
amount.
create view [dbo].[S2_Task1_prof]
as
select top 100 percent [TransactionType],sum([Amount]) as TotalAmount,
sum([FeeAmount]) as TotalFeeAmount
from [dbo].[AccountTransactions] a inner join [dbo].[TransactionFees] tf
on a.TransactionId=tf.TransactionId
inner join BankAccounts ba on ba.[AccountId]=a.[AccountId]
inner join [dbo].[AccountOwners] ao on ao.[OwnerPIN]=ba.[OwnerPIN]
where lower([Locality]) in ('bucuresti', 'sibiu')
and datedif(month,[TransactionDate],getdate()) between 0 and 9
and datedif(year,[OpeningDate],getdate())>=3
group by [TransactionType]
order by 2 desc

Define a stored procedure with 2 output parameters corresponding to indicators


required for statistics and analysis purposes: 1) the total transacted amount and 2)
the average initial deposit amount (in case of newly opened accounts only). Both
indicators are relative to a certain account type and a semester which must be
specified at execution time.
CREATE procedure [dbo].[S2_Task3_prof] @pAccountType varchar(50),
@pSemester tinyint,@pYear smallint,
@pTotalAmount money out,
@pAverageInitialDeposit money out
as
begin
set @pAverageInitialDeposit = (select avg([InitialDeposit])
from [dbo].[BankAccounts]
where [AccountType]=@pAccountType and year([OpeningDate])=@pYear
and month([OpeningDate])<=(case @pSemester when 1 then 6
when 2 then 12 end)
)
select @pTotalAmount=sum([Amount])
from [dbo].[AccountTransactions] t
inner join [dbo].[BankAccounts] a
on a.[AccountId]=t.[AccountId]
where [AccountType]=@pAccountType and year([OpeningDate])=@pYear
and month([OpeningDate])<=(case @pSemester when 1 then 6
when 2 then 12 end)
end

Define a stored procedure with 2 output parameters which return the total number
of transactions and total fee amount for a certain account and time period, both
specified at execution time.

CREATE procedure [dbo].[S1_Task3_prof] @pAccountId int,@pStartDate date,@pEndDate date,


@pNoTransactions smallint out,@pTotalFee money out
as
begin
select @pTotalFee=sum([FeeAmount])
from [dbo].[TransactionFees] f inner join [dbo].[AccountTransactions] t
on f.[TransactionId]=f.TransactionId
where [TransactionDate] between @pStartDate and @pEndDate
and [AccountId]=@pAccountId
select @pNoTransactions=count(*)
from [dbo].[AccountTransactions]
where [TransactionDate] between @pStartDate and @pEndDate
and [AccountId]=@pAccountId
end

Define a function which returns the list of transactions and the corresponding (total)
transaction fee amount for the particular type of account with the least transactions
in a month specified as a parameter. In order to illustrate how this function can be
put to use, you are required to create a stored procedure which calls the function
and provides additional information on the accounts involved in transactions (id,
IBAN, owners name).
Table variable
create function [dbo].[S1_Task4_prof] (@pYear smallint, @pMonth tinyint)
returns @t table([TransactionId] int,TotalFee money)
as
begin
with tmp as(
select [AccountType], dense_rank() over(order by count(*)) as Ranking
from [dbo].[BankAccounts] a inner join [dbo].[AccountTransactions] t
on a.[AccountId]=t.[AccountId]
where datepart(month,[TransactionDate])=@pMonth and
datepart(year,[TransactionDate])=@pYear
group by [AccountType]
)
insert into @t
select t.[TransactionId], sum([FeeAmount])
from tmp inner join [dbo].[BankAccounts] a on
tmp.AccountType=a.AccountType
inner join [dbo].[AccountTransactions] t
on t.[AccountId]=a.[AccountId] inner join [dbo].[TransactionFees] f
on f.TransactionId=t.TransactionId
where Ranking=1 and datepart(month,[TransactionDate])=@pMonth and
datepart(year,[TransactionDate])=@pYear

group by t.[TransactionId]
return
end

which calls the function and provides additional information on the accounts
involved in transactions (id, IBAN, Owners name).
create procedure [dbo].[S1_Task4_prof_Part2] @pYear smallint, @pMonth tinyint
as
begin
select f.*,[AccountIBAN],a.[AccountId],[LastName],[FirstName]
from S1_Task4_prof(@pYear, @pMonth) f inner join
[dbo].[AccountTransactions] t on t.[TransactionId]=f.[TransactionId]
inner join [dbo].[BankAccounts] a on a.[AccountId]=t.[AccountId]
inner join [dbo].[AccountOwners] o on o.OwnerPIN=a.[OwnerPIN]
end

exec S1_Task4_prof_Part2 2012,3


subject 5

Define a stored procedure to insert new records into the TransactionFees table.
When the fee in question is not available in the database it must be inserted into
the corresponding table (Fees) prior to using it in the context of account
transactions. Regardless of the reasons, if this operation fails, then both insert
actions must be cancelled.

create procedure S1_Task5_Hodo @pTransactionId int, @pFeeID int, @pFeeAmount money,


@pFeeName nvarchar (50)
as
begin
begin try
begin transaction
if not exists (select * from [dbo].[Fees] where [FeeId]=@pFeeID)
insert into [dbo].[Fees] values (@pFeeID,@pFeeName)
-- insert into [dbo].[Fees] ([FeeId],[FeeName]) select
@pFeeID,@pFeeName
insert into [dbo].[TransactionFees] ([TransactionId],[FeeId],[FeeAmount])
select @pTransactionId, @pFeeID, @pFeeAmount
commit
select 'Transaction completed successfully'

end

end try
begin catch
if @@trancount>0 rollback
select error_message(), error_number()
end catch

exec S1_Task5_Hodo 2,2,40, Test Fee

The Other subject (set)


1. Define a view to display the list of transactions in the first and third quarters of
the past year; the resulting record set must provide the transaction id, type,
amount, total amount of corresponding fees, as well as information on the related
account (Id, IBAN, owners name and PIN). Selected transactions should be sorted in
descending order by the total fee amount.

create view vS1_Task1_Hodo


as
with tmp1
as (
select [TransactionType], sum([Amount]) as TotalTranAmount
from [dbo].[AccountTransactions] t inner join [dbo].[BankAccounts] a on a.
[AccountId]=t.AccountId
inner join [dbo].[AccountOwners] o on o.OwnerPIN=a.[OwnerPIN]
where datedif(month,[TransactionDate],getdate())<9
and datedif(year,[OpeningDate],getdate())>3
and lower([Locality]) in ('bucuresti','sibiu')
group by [TransactionType]
),
tmp2 as (
select [TransactionType], sum([FeeAmount]) as TotalFee
from [dbo].[AccountTransactions] t inner join [dbo].[TransactionFees] f on
t.TransactionId=f.TransactionId
inner join [dbo].[BankAccounts] a on a.[AccountId]=t.AccountId
inner join [dbo].[AccountOwners] o on o.OwnerPIN=a.[OwnerPIN]
where datedif(month,[TransactionDate],getdate())<9
and datedif(year,[OpeningDate],getdate())>3
and lower([Locality]) in ('bucuresti','sibiu')
group by [TransactionType]
)
SELECT top 100 percent tmp1.[TransactionType], TotalTranAmount, TotalFee
FROM tmp1 inner join tmp2 on tmp1.TransactionType=tmp2.TransactionType
order by 2 desc

subject 4
Define a function which returns the list of transactions for the most frequently
performed transaction type during the past N months (N is to be specified at
execution time); the highest fee as well as the lowest should be displayed for each
of the selected transactions. In order to illustrate how this function can be put to
use, you are required to create a stored procedure which calls the function and
provides additional information on the accounts involved in transactions (id, IBAN,
owners name).

CREATE function [dbo].[S2_Task4_prof] (@pNoMonths tinyint)


returns @t table(TransactionId int,MaxFee money,MinFee money)
as
begin
with tmp as (select [TransactionType],
dense_rank() over(order by count(*) desc) as Ranking
from [dbo].[AccountTransactions]
where datedif(month,[TransactionDate],getdate())<=@pNoMonths
group by [TransactionType]
)
insert into @t
select f.[TransactionId],max([FeeAmount]),min([FeeAmount])
from tmp inner join [dbo].[AccountTransactions] t on
tmp.[TransactionType]=t.[TransactionType] inner join
[dbo].[TransactionFees] f on f.[TransactionId]=t.[TransactionId]
where datedif(month,[TransactionDate],getdate())<=@pNoMonths
and Ranking=1
group by f.[TransactionId]
return
end
create procedure [dbo].[S2_Task4_prof_Part2] @pNoMonths tinyint
as
begin
select a.[AccountId],[AccountIBAN],[LastName],[FirstName]
from S2_Task4_prof(@pNoMonths) f
inner join [dbo].[AccountTransactions] t on f.[TransactionId]=t.[TransactionId]
inner join [dbo].[BankAccounts] a on t.[AccountId]=a.[AccountId]
inner join [dbo].[AccountOwners] o on o.[OwnerPIN]=a.[OwnerPIN]
end

S-ar putea să vă placă și