edited by
2,582 views
6 votes
6 votes

Consider a bank database with only one relation  

transaction (transno, acctno, date, amount)

The amount attribute value is positive for deposits and negative for withdrawals.

  1. Define an SQL view TP containing the information
    (acctno,T1.date,T2.amount) 
    for every pair of transaction T1,T2 and such that T1 and T2 are transaction on the same account and the date of T2 is $\leq $ the date of T1.
  2. Using only the above view TP, write a query to find for each account the minimum  balance it ever reached (not including the 0 balance when the account is created). Assume there is at most one transaction per day on each account and each account has at least one transaction since it was created. To simplify your query, break it up into 2 steps by defining an intermediate view V.
edited by

1 Answer

9 votes
9 votes

a.

Create view TP(T1.acctno, T1.date, T2.amount)
as (Select T1.acctno, T1.date, T2.amount
    from Transaction T1, Transaction T2
    where T1.acctno = T2.acctno
    and T2.date <= T1.date);

b.

i.

Create view V(acctno, date, balance)
as (select acctno, date, sum(amount)
    from TP
    group by acctno, date);

ii.

select acctno, min(balance) 
from V
group by acctno;

 

Related questions

45 votes
45 votes
4 answers
2
57 votes
57 votes
4 answers
3
Kathleen asked Sep 14, 2014
16,434 views
Given relations r(w, x) and s(y, z) the result ofselect distinct w, x from r, s is guaranteed to be same as r, provided.r has no duplicates and s is non-emptyr and s have...