Advertisement
nate__the_dogg

ibk_sql

Feb 24th, 2022
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
T-SQL 3.15 KB | None | 0 0
  1. /*
  2. 1. Count of unique accounts that make at least one payment on any given day
  3. */
  4. select payment_date, count(distinct account_id) as account_count
  5. from payment
  6. group by payment_date
  7. order by payment_date desc
  8.  
  9.  
  10.  
  11. /*
  12. 2: Count of unique accounts that make at least one payment in a month
  13. */
  14.  
  15. select concat(a.mnth,'/' ,a.yr) as payment_month, count(distinct a.account_id) as account_count
  16. from (
  17.   select account_id, MONTH(payment_date) as mnth, YEAR(payment_date) as yr
  18.   from payment
  19. ) a
  20. group by a.yr, a.mnth
  21. order by a.yr desc, a.mnth DESC
  22.  
  23.  
  24. /*
  25. 3: How many new accounts made a payment in the last month?
  26. */
  27.  
  28. with last_month_payments(acct_id, rnk) as (
  29.   select account_id,
  30.   row_number() over (partition by account_id order by payment_date DESC) as rnk -- row_number(): to know the first time a payment was made
  31.   from payment
  32.   where payment_date BETWEEN DATEADD(DAY, 1, EOMONTH(GETDATE(), -2)) AND EOMONTH(GETDATE(), -1)
  33. ),
  34.  
  35. number_of_payments(acct_id, max_rnk) as (
  36.   select a.acct_id, max(a.rnk) as max_rnk  
  37.   from last_month_payments a
  38.   group by a.acct_id
  39. )
  40. select count(*) as new_accounts
  41. from number_of_payments n
  42. where n.max_rnk = 1
  43.  
  44.  
  45.  
  46. /*
  47. 4: What is the average count of payments per account?
  48. */
  49. select a.account_id, avg(a.cnt) as avg_count
  50. from (
  51.   select payment_date, account_id, count(*) as cnt
  52.   from payment
  53.   group by payment_date, account_id
  54. ) a
  55. group by a.account_id
  56.  
  57.  
  58.  
  59. /*
  60. 5: What is the failed payment rate this month?
  61. calculated as follows:
  62. failed rate = (count of failed payments this month)/(count of all payments this month)
  63. */
  64.  
  65. WITH all_payments_this_month(status, status_count) as (
  66.   select Status, count(*) as cnt
  67.   from payment
  68.   where payment_date >= DATEADD(DAY, 1, EOMONTH(GETDATE(), -1))
  69.   group by status
  70. )
  71. select round(cast((select status_count from all_payments_this_month where status = 'failed') as float)/cast(sum(status_count) as float), 3) as failed_payment_rate
  72. from all_payments_this_month
  73.  
  74.  
  75.  
  76. /*
  77. 6: What is the number of GBP payments that have no decimal place?
  78. Assumption: amount is float.  
  79. */
  80.  
  81. select count(*) as cnt
  82. from payment
  83. where currency = 'GBP'
  84. AND cast(amount as decimal(20, 2)) % 1 = 0  
  85.  
  86. /*
  87. If amount is varchar
  88. */
  89. select count(*) as cnt
  90. from payment
  91. where currency = 'GBP'
  92. AND amount NOT LIKE '%.%'
  93.  
  94.  
  95.  
  96. /*
  97. 7. How will you calculate the daily amount GBP Equiv. per day for the last three months?
  98. */
  99. select
  100.   p.payment_date,
  101.   p.account_id,
  102.   p.currency as original_currency,
  103.   p.amount as original_amount,
  104.   p.amount * h.rate as amount_in_gbp
  105. from payment p JOIN historic_rates h on p.payment_date = h.date AND p.currency = h.from_ccy
  106. where p.currency <> 'GBP'AND payment_date >= DATEADD(month, -3, GETDATE())
  107. ORDER BY p.payment_date
  108.  
  109.  
  110.  
  111. /* 8: What is the average payment amount GBP Equiv. per account year to date? */
  112. select
  113.     p.payment_date,
  114.     p.account_id,
  115.     AVG(p.amount * h.rate) OVER (partition by YEAR(p.payment_date), p.account_id order by p.payment_date) as amount_ytd
  116.    
  117. from payment p join historic_rates h on p.payment_date = h.date and p.currency = h.from_ccy
  118. where p.currency <> 'GBP'
  119. ORDER BY p.payment_date, p.account_id
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement