Advertisement
Guest User

Untitled

a guest
Feb 5th, 2018
536
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.37 KB | None | 0 0
  1. CAN EVERYONE READ THIS?
  2.  
  3. bob bob@yahoo.com
  4. ('bob','bob@yahoo.com')
  5.  
  6.  
  7. relational databases
  8.  
  9. often support SQL - structured query language
  10.  
  11. relational algebra -
  12. select (filtering)
  13. project (select columns)
  14. join ( the join)
  15. union
  16. intersection
  17.  
  18. SQL implements relational algebra.
  19.  
  20. select f1,f2,f3, sqrt(f4+f1) a1 -- projection
  21. from whatever
  22. where f3='something' -- select operation, filtering
  23.  
  24. select f1,f2 from whatever
  25. union
  26. select f1,f2 from somethingelse
  27.  
  28. -- intersection (not really)
  29. select f1,f2 from whatever
  30. except
  31. select f1,f2 from somethingelse
  32.  
  33. ----------------------------------------
  34. DDL: 2 statements to care about:
  35.  
  36. create table ITEM (
  37. itemid bigint,
  38. name varchar(50)
  39. );
  40. drop table ITEM;
  41.  
  42. 4 sql statements to care about: crud operations
  43.  
  44. insert
  45. select
  46. update
  47. delete
  48.  
  49. insert into ITEM(itemid,name) values (123,'thing');
  50.  
  51. select * from ITEM;
  52.  
  53. select itemid from ITEM;
  54.  
  55. select * from item where name like 't%';
  56.  
  57. update ITEM set name='thung' where itemid=123;
  58.  
  59. delete ITEM where thingid=123;
  60.  
  61.  
  62.  
  63. ----------------------------------------
  64. data costs $$$, so avoid losing it.
  65. in other words, avoid using "delete"
  66.  
  67. replace delete with some form of update
  68. (update usually implies add another event record).
  69.  
  70. ----------------------------------------
  71. when we do an update,
  72.  
  73. update ITEM set name='thung' where itemid=123;
  74.  
  75. avoid using update, because it loses old value.
  76.  
  77. e.g. add another event record to do the "update"
  78.  
  79. -----------------------------------------
  80. --- bank account journal.
  81.  
  82. journal(tdate,tid,accntid,amount);
  83.  
  84.  
  85. -- add $100 to account 123
  86. insert into journal(tid,accntid,amount)
  87. values(1,123,100);
  88.  
  89.  
  90. -- lets say we need to modify balance to be $200;
  91.  
  92. --option 1 (terrible option)
  93. update journal set amount=200
  94. where tid=1 and accntid=123;
  95.  
  96. --the good option
  97.  
  98. insert into journal(tid,accntid,amount)
  99. values(2,123,100);
  100.  
  101.  
  102. --journal(tdate,tid,accntid,amount);
  103.  
  104. --- what's the balance on 2018-02-05?
  105. --- for account 123
  106.  
  107.  
  108. select sum(amount) bal
  109. from journal
  110. where tdate<='2018-02-05' and accntid=123
  111.  
  112.  
  113. --- calculate daily closing balances for all accounts
  114. --- (only for days that have transactions).
  115.  
  116. with daytots as (
  117. select tdate,accntid,sum(amount) amount
  118. from journal
  119. group by tdate,accntid
  120. )
  121. select tdate,accntid,
  122. sum(amount) over (partition by accntid
  123. order by tdate) bal
  124. from daytots
  125.  
  126.  
  127. --- create table of daily closing balances
  128. --- (same as above, except in a create table).
  129.  
  130. create table daily_closing_bal as
  131. with daytots as (
  132. select tdate,accntid,sum(amount) amount
  133. from journal
  134. group by tdate,accntid
  135. )
  136. select tdate,accntid,
  137. sum(amount) over (partition by accntid
  138. order by tdate) bal
  139. from daytots
  140. ;
  141.  
  142. -- using daily_closing_bal, calculate daily
  143. -- percentage gain/loss, if not zero or infinity.
  144.  
  145. create table daily_closing_prcnt as
  146. with prevbal as (
  147. select tdate,accntid,bal,
  148. lag(bal) over (partition by accntid
  149. order by tdate) lag_bal
  150. from daily_closing_bal
  151. )
  152. select tdate,accntid,
  153. (bal-lag_bal)/lag_bal as prcnt
  154. from prevbal
  155. where lag_bal>0;
  156.  
  157.  
  158. --- we want to know the gain/loss between
  159. --- 2017-01-01 and 2018-01-01
  160.  
  161. select accntid,
  162. -- product( 1 + prcnt ) -- product doesn't exist
  163. expr( sum( log(1+prcnt) ) ) gainloss
  164. from daily_closing_prcnt
  165. where tdate>='2017-01-01' and tdate<'2018-01-01'
  166. group by accntid
  167.  
  168.  
  169. ---------------------------------------------------
  170.  
  171. customer(cid,name,email)
  172. account(aid,cid,...)
  173.  
  174. -- each customer may have 0..N accounts.
  175.  
  176. ---what's the customer name of account 235
  177.  
  178. (123,'bob','bob@yahoo.com') ---customer
  179.  
  180. (235,123) --account
  181.  
  182. --join (inner join)
  183.  
  184. select b.name
  185. from account a
  186. inner join customer b
  187. on a.cid=b.cid
  188. where a.aid=235
  189.  
  190. --- inner join conditions can be put into "where"
  191. -- e.g.: bad, don't do this:
  192.  
  193. select b.name
  194. from account a, customer b
  195. where a.cid=b.cid and
  196. a.aid=235
  197.  
  198.  
  199. ------------------------------------
  200. customer(cid,name,email)
  201. account(aid,cid,...)
  202.  
  203. -- find all customers who don't have an account.
  204.  
  205. (123,'bob','bob@yahoo.com') ---customer
  206.  
  207. (null,null) --account
  208.  
  209. -- customer a left outer join account b
  210. --- customer is on the LEFT
  211. --- account is on the RIGHT
  212.  
  213. select a.cid
  214. from customer a
  215. left outer join account b
  216. on a.cid=b.cid
  217. where b.cid is null
  218.  
  219.  
  220. ----------------------------------------
  221. --- find customers who have more than 10 accounts.
  222.  
  223. select a.cid
  224. from customer a
  225. inner join account b
  226. on a.cid=b.cid
  227. group by a.cid
  228. having count(*) > 10
  229.  
  230. --- find customers who have <=2 accounts.
  231. ---?
  232. select a.cid
  233. from customer a
  234. left outer join account b
  235. on a.cid=b.cid
  236. group by a.cid
  237. having count(b.cid) <= 2
  238.  
  239. --- what's the avg number of accounts per customer?
  240.  
  241. with custaccntcnt as (
  242. select a.cid,count(b.cid) cnt
  243. from customer a
  244. left outer join account b
  245. on a.cid=b.cid
  246. group by a.cid
  247. )
  248. select avg(cnt)
  249. from custaccntcnt
  250.  
  251.  
  252.  
  253. ------------------------------------
  254. customer(cid,name,email)
  255. account(aid,cid,...)
  256. journal(tid,aid,tim,amount)
  257.  
  258. --- current balance of aid=123
  259. select sum(amount) bal
  260. from journal
  261. where aid=123
  262.  
  263. --- find balance for all accounts of cid=235?
  264. select sum(amount) bal
  265. from account a
  266. inner join journal b
  267. on a.aid=b.aid
  268. where a.cid=235
  269.  
  270. --- find balance of customer with email bob@yahoo.com
  271.  
  272. select sum(amount) bal
  273. from customer a
  274. inner join account b
  275. on a.cid=b.cid
  276. inner join journal c
  277. on b.aid=c.aid
  278. where
  279. a.email = 'bob@yahoo.com'
  280.  
  281. ------------------------------------
  282. -- balances by email host?
  283.  
  284. 'bob@yahoo.com'
  285. position('@' in 'bob@yahoo.com') will return 4
  286. (or zero, if not found).
  287.  
  288. -- chop away the user from email.
  289. --- 'bob@yahoo.com' into 'yahoo.com'
  290.  
  291. with custdomain as (
  292. select a.*,
  293. substr(email,position('@' in email),1000) edomain
  294. from customer a
  295. )
  296. select edomain,sum(amount) bal
  297. from custdomain a
  298. inner join account b
  299. on a.cid=b.cid
  300. inner join journal c
  301. on b.aid=c.aid
  302. group by edomain
  303. order by 2 desc
  304.  
  305.  
  306. ----------------------------------------------
  307. -- everything is an event.
  308. --- something happens during some state...
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement