cdsatrian

range tanggal

Feb 28th, 2017
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MySQL 2.80 KB | None | 0 0
  1. DROP TABLE IF EXISTS a;
  2. CREATE TABLE IF NOT EXISTS a(
  3.     id INT AUTO_INCREMENT PRIMARY KEY,
  4.     trans_date DATE,
  5.     currency CHAR(3)
  6. );
  7.  
  8. INSERT INTO a(trans_date,currency)
  9. VALUES
  10. ('2015-02-01','USD'),
  11. ('2015-04-04','USD'),
  12. ('2017-04-04','USD'),
  13. ('2015-01-01','JPY'),
  14. ('2015-04-04','JPY'),
  15. ('2017-04-04','JPY');
  16.  
  17. SELECT * FROM a;
  18. +----+------------+----------+
  19. | id | trans_date | currency |
  20. +----+------------+----------+
  21. |  1 | 2015-02-01 | USD      |
  22. |  2 | 2015-04-04 | USD      |
  23. |  3 | 2017-04-04 | USD      |
  24. |  4 | 2015-01-01 | JPY      |
  25. |  5 | 2015-04-04 | JPY      |
  26. |  6 | 2017-04-04 | JPY      |
  27. +----+------------+----------+
  28.  
  29. DROP TABLE IF EXISTS b;
  30. CREATE TABLE IF NOT EXISTS b(
  31.     currency CHAR(3),
  32.     last_update DATE,
  33.     price FLOAT
  34. );
  35.  
  36. INSERT INTO b(currency,last_update,price)
  37. VALUES
  38. ('USD','2015-01-01',13200.00),
  39. ('USD','2015-03-01',13160.00),
  40. ('USD','2017-04-04',13340.00),
  41. ('JPY','2015-01-01',110.00),
  42. ('JPY','2015-03-01',108.00),
  43. ('JPY','2017-04-04',112.00);
  44.  
  45. SELECT * FROM b;
  46. +----------+-------------+-------+
  47. | currency | last_update | price |
  48. +----------+-------------+-------+
  49. | USD      | 2015-01-01  | 13200 |
  50. | USD      | 2015-03-01  | 13160 |
  51. | USD      | 2017-04-04  | 13340 |
  52. | JPY      | 2015-01-01  |   110 |
  53. | JPY      | 2015-03-01  |   108 |
  54. | JPY      | 2017-04-04  |   112 |
  55. +----------+-------------+-------+
  56.  
  57. SELECT a.id,a.trans_date,MAX(b.last_update) AS last_update, a.currency
  58. FROM a
  59. JOIN b ON (a.trans_date>=b.last_update AND a.currency=b.currency)
  60. GROUP BY a.id;
  61. +----+------------+-------------+----------+
  62. | id | trans_date | last_update | currency |
  63. +----+------------+-------------+----------+
  64. |  1 | 2015-02-01 | 2015-01-01  | USD      |
  65. |  2 | 2015-04-04 | 2015-03-01  | USD      |
  66. |  3 | 2017-04-04 | 2017-04-04  | USD      |
  67. |  4 | 2015-01-01 | 2015-01-01  | JPY      |
  68. |  5 | 2015-04-04 | 2015-03-01  | JPY      |
  69. |  6 | 2017-04-04 | 2017-04-04  | JPY      |
  70. +----+------------+-------------+----------+
  71.  
  72. SELECT a.id,a.trans_date,b.last_update, a.currency, b.price
  73. FROM a
  74. JOIN
  75. (
  76. SELECT a.id,MAX(b.last_update) AS last_update,b.currency
  77. FROM a
  78. JOIN b ON (a.trans_date>=b.last_update AND a.currency=b.currency)
  79. GROUP BY a.id
  80. ) c USING(id)
  81. JOIN b ON (b.last_update=c.last_update AND b.currency=c.currency);
  82.  
  83. +----+------------+-------------+----------+-------+
  84. | id | trans_date | last_update | currency | price |
  85. +----+------------+-------------+----------+-------+
  86. |  1 | 2015-02-01 | 2015-01-01  | USD      | 13200 |
  87. |  2 | 2015-04-04 | 2015-03-01  | USD      | 13160 |
  88. |  3 | 2017-04-04 | 2017-04-04  | USD      | 13340 |
  89. |  4 | 2015-01-01 | 2015-01-01  | JPY      |   110 |
  90. |  5 | 2015-04-04 | 2015-03-01  | JPY      |   108 |
  91. |  6 | 2017-04-04 | 2017-04-04  | JPY      |   112 |
  92. +----+------------+-------------+----------+-------+
Add Comment
Please, Sign In to add comment