Advertisement
Guest User

Untitled

a guest
Mar 19th, 2019
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.04 KB | None | 0 0
  1.  
  2.  
  3. customer(custid,fname,lname,ssn)
  4. address(addid,custid,street,city,state,zip)
  5. phone(phoneid,custid,num,type)
  6. account(accntid,custid,type) --accntid=1 is 'cash'
  7. transctn(trid,tim,accntid,amnt)
  8.  
  9.  
  10. create sequence custseq;
  11. create sequence addressseq;
  12. create sequence phoneseq;
  13. create sequence accntseq;
  14. create sequence trseq;
  15.  
  16. --create a new customer
  17. insert into customer(custid,fname,lname,ssn)
  18. values(nextval('custseq'),'John','Doe','123-45-1234');
  19. insert into address(addid,custid,street,city,state,zip)
  20. values(nextval('addressseq'), currval('custseq'),
  21. '2020 Bedford Ave','Brooklyn','NY','11210');
  22. insert into phone(phoneid, custid,num,type)
  23. values(nextval('phoneseq'),currval('custseq'),
  24. '718-555-1234','H');
  25.  
  26. -- add checking account for customer 12345;
  27. insert into account(accntid,custid, type)
  28. values(nextval('accntid'), 12345, 'C');
  29.  
  30. -- account 12345 gets deposit $100.
  31. -- add money to customer's account
  32. insert into transctn(trid,tim,accntid,amnt)
  33. values(nextval('trseq'), now(), 12345, 100);
  34. -- take money out of 'cash' account.
  35. insert into transctn(trid,tim,accntid,amnt)
  36. values(currval('trseq'), now(), 1, -100);
  37.  
  38. nextval('trseq') = 1
  39. nextval('trseq') = 2
  40. nextval('trseq') = 3
  41. currval('trseq') = 3
  42. currval('trseq') = 3
  43. nextval('trseq') = 4
  44.  
  45.  
  46.  
  47. --find the balance for account 12345 ?
  48.  
  49. select sum(amnt)
  50. from transctn
  51. where accntid=12345;
  52.  
  53.  
  54. -- find balance for ALL accounts...
  55. select accntid,sum(amnt)
  56. from transctn
  57. group by accntid
  58.  
  59.  
  60. -- find closing balance for all accounts on 2018-12-31
  61.  
  62.  
  63. select accntid,sum(amnt)
  64. from transctn
  65. where tim<'2019-01-01'
  66. group by accntid
  67.  
  68.  
  69. -- find closing balance for customer 123 on 2018-12-31
  70.  
  71. select b.custid,sum(a.amnt) bal
  72. from transctn a
  73. inner join account b
  74. on a.accntid=b.accntid
  75. where b.custid=123 and a.tim<'2019-01-01'
  76. group by b.custid
  77.  
  78.  
  79. -- lets find corrupt transactions
  80. -- (those that don't sum to 0).
  81. select trid
  82. from transctn
  83. group by trid
  84. having sum(amnt) != 0
  85.  
  86.  
  87.  
  88. -- find suspicious customers
  89. --(customers with same ssn, but different names).
  90. -- find instances of same ssn, but different names.
  91.  
  92. select ssn
  93. from customer
  94. group by ssn
  95. having min(concat(fname,',',lname)) != max(concat(fname,',',lname))
  96.  
  97. -- find balance for each account for each day of 2018
  98.  
  99.  
  100. with dates as (
  101. SELECT cast( date_trunc('day', dd) as date) as dt
  102. FROM generate_series
  103. ( cast('2018-01-01' as timestamp)
  104. , cast('2008-12-31' as timestamp)
  105. , cast('1 day' as interval) ) dd
  106. ),
  107. accntdt as (
  108. select accntid,dt
  109. from account, dates
  110. ),
  111. tamntday as (
  112. select
  113. coalesce(a.accntid,b.accntid) accntid,
  114. coalesce(a.dt, cast(b.tim as date) ) dt,
  115. sum( coalesce(b.amnt,0) ) amnt
  116. from accntdt a
  117. full outer join transactn b
  118. on a.accntid=b.accntid and cast(b.tim as date)=a.dt
  119. where coalesce(b.tim,'2018-01-01')<'2019-01-01'
  120. group by coalesce(a.accntid,b.accntid),
  121. coalesce(a.dt, cast(b.tim as date) )
  122. ),
  123. bals as (
  124. select a.*,
  125. sum(amnt) over (partition by accntid order by dt) bal
  126. from tamntday a
  127. )
  128. select accntid,dt,bal
  129. from bals
  130. where dt>='2018-01-01' and dt<'2019-01-01';
  131.  
  132.  
  133.  
  134.  
  135. -- find outlier transactions
  136. -- transactions >= 2sd of 20 day moving average.
  137.  
  138.  
  139. with dttransactions as (
  140. select accntid,cast(tim as date) dt,
  141. avg( sum(amnt) ) over (partition by accntid order by cast(tim as date) rows between 20 preceding and current row) avg20,
  142. stddev( sum(amnt) ) over (partition by accntid order by cast(tim as date) rows between 20 preceding and current row) sd20
  143. from transactn
  144. group by accntid,cast(tim as date)
  145. )
  146. select a.accntid,a.tim,b.dt,b.avg20,b.sd20,a.amnt
  147. from transactn a
  148. inner join dttransactions b
  149. on a.accntid=b.accntid and cast(a.tim as date)=b.dt
  150. where a.amnt >= b.avg20+b.sd20*2.0
  151.  
  152.  
  153.  
  154.  
  155.  
  156.  
  157.  
  158.  
  159.  
  160.  
  161.  
  162.  
  163.  
  164. customer(custid,fname,lname,ssn)
  165. address(addid,custid,street,city,state,zip)
  166. phone(phoneid,custid,num,type)
  167. account(accntid,custid,type) --accntid=1 is 'cash'
  168. transctn(trid,tim,accntid,amnt)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement