Advertisement
cdsatrian

Exchange Rate 20170317

Mar 17th, 2017
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MySQL 3.87 KB | None | 0 0
  1. CREATE TABLE IF NOT EXISTS exchange_rate (
  2. currency_code varchar(5) NOT NULL,
  3. effective_date date NOT NULL,
  4. exchange_rate decimal(5,0) NOT NULL
  5. );
  6.  
  7. INSERT INTO exchange_rate (currency_code, effective_date, exchange_rate) VALUES
  8. ('USD', '2016-01-01', '13000'),
  9. ('USD', '2016-02-01', '13500'),
  10. ('USD', '2016-03-10', '13200'),
  11. ('SGD', '2016-01-01', '9500'),
  12. ('SGD', '2016-02-11', '9300'),
  13. ('SGD', '2016-03-20', '9250');
  14.  
  15. SELECT * FROM exchange_rate;
  16.  
  17. +---------------+----------------+---------------+
  18. | currency_code | effective_date | exchange_rate |
  19. +---------------+----------------+---------------+
  20. | USD           | 2016-01-01     |         13000 |
  21. | USD           | 2016-02-01     |         13500 |
  22. | USD           | 2016-03-10     |         13200 |
  23. | SGD           | 2016-01-01     |          9500 |
  24. | SGD           | 2016-02-11     |          9300 |
  25. | SGD           | 2016-03-20     |          9250 |
  26. +---------------+----------------+---------------+
  27.  
  28. SELECT
  29.     a.currency_code,
  30.     a.effective_date,
  31.     a.exchange_rate
  32. FROM
  33.     exchange_rate AS a
  34. ORDER BY
  35.     a.currency_code DESC,
  36.     a.effective_date ASC;
  37.  
  38. +---------------+----------------+---------------+
  39. | currency_code | effective_date | exchange_rate |
  40. +---------------+----------------+---------------+
  41. | USD           | 2016-01-01     |         13000 |
  42. | USD           | 2016-02-01     |         13500 |
  43. | USD           | 2016-03-10     |         13200 |
  44. | SGD           | 2016-01-01     |          9500 |
  45. | SGD           | 2016-02-11     |          9300 |
  46. | SGD           | 2016-03-20     |          9250 |
  47. +---------------+----------------+---------------+ 
  48.  
  49. SELECT
  50.     a.currency_code,
  51.     a.effective_date,
  52.     (
  53.         SELECT
  54.             DATE_SUB(MIN(b.effective_date), INTERVAL 1 DAY)
  55.         FROM exchange_rate AS b
  56.         WHERE
  57.             b.effective_date>a.effective_date AND a.currency_code=b.currency_code
  58.     ) AS effective_end_date,
  59.     a.exchange_rate
  60. FROM
  61.     exchange_rate AS a
  62. ORDER BY
  63.     a.currency_code DESC,
  64.     a.effective_date ASC;
  65.  
  66. +---------------+----------------+--------------------+---------------+
  67. | currency_code | effective_date | effective_end_date | exchange_rate |
  68. +---------------+----------------+--------------------+---------------+
  69. | USD           | 2016-01-01     | 2016-01-31         |         13000 |
  70. | USD           | 2016-02-01     | 2016-03-09         |         13500 |
  71. | USD           | 2016-03-10     | NULL               |         13200 |
  72. | SGD           | 2016-01-01     | 2016-02-10         |          9500 |
  73. | SGD           | 2016-02-11     | 2016-03-19         |          9300 |
  74. | SGD           | 2016-03-20     | NULL               |          9250 |
  75. +---------------+----------------+--------------------+---------------+
  76.  
  77. SELECT
  78.     a.currency_code,
  79.     DATE_FORMAT(a.effective_date,'%d-%b-%Y') AS effective_date,
  80.     DATE_FORMAT(
  81.         COALESCE(
  82.             (
  83.             SELECT
  84.                 DATE_SUB(MIN(b.effective_date), INTERVAL 1 DAY)
  85.             FROM exchange_rate AS b
  86.             WHERE
  87.                 b.effective_date>a.effective_date AND a.currency_code=b.currency_code
  88.             )
  89.             ,'2099-12-31'
  90.         )
  91.         ,'%d-%b-%Y'
  92.     ) AS effective_end_date,
  93.     a.exchange_rate
  94. FROM
  95.     exchange_rate AS a
  96. ORDER BY
  97.     a.currency_code DESC,
  98.     a.effective_date ASC;
  99.    
  100. +---------------+----------------+--------------------+---------------+
  101. | currency_code | effective_date | effective_end_date | exchange_rate |
  102. +---------------+----------------+--------------------+---------------+
  103. | USD           | 01-Jan-2016    | 31-Jan-2016        |         13000 |
  104. | USD           | 01-Feb-2016    | 09-Mar-2016        |         13500 |
  105. | USD           | 10-Mar-2016    | 31-Dec-2099        |         13200 |
  106. | SGD           | 01-Jan-2016    | 10-Feb-2016        |          9500 |
  107. | SGD           | 11-Feb-2016    | 19-Mar-2016        |          9300 |
  108. | SGD           | 20-Mar-2016    | 31-Dec-2099        |          9250 |
  109. +---------------+----------------+--------------------+---------------+
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement