简体   繁体   中英

Oracle SQL Migrate data from date range / period to months

I have some problems in migrating data from one table to another. Im using a Oracle 11 database. In my problem, one table states some values which are valid in a specific date range (start date and end date). The other table, to which the data should be migrated to, should state these values per month (MMYYY). So for a specific time frame (for example 01/01/2010 to 31/12/2010), the data should be stated per month (01/2010, 02/2010, ..., 31/2010). I was trying to create a suitable sql insert statement for this case but i was not successful in it.


Example:

Table 1 has the following attributes: ID (PK), START_DATE, END_DATE, VALUE

Table 2 has the following attributes: ID (PK), MONTH, YEAR, VALUE


Table 1:

  • ID = 1, START_DATE = 01/01/2010, END_DATE = 31/12/2015, VALUE = 5
  • ID = 2, START_DATE = 01/01/2012, END_DATE = 31/12/2013, VALUE = 10
  • ID = 3, START_DATE = 01/01/2018, END_DATE = 31/12/2020, VALUE = 15

These entries should look like the following in the new table:

Table 2:

  • ID = 1, MONTH = 01, YEAR = 2010, VALUE = 5
  • ID = 1, MONTH = 02, YEAR = 2010, VALUE = 5
  • ID = 1, MONTH = 03, YEAR = 2010, VALUE = 5
  • ...
  • ID = 1, MONTH = 12, YEAR = 2015, VALUE = 5
  • ID = 2, MONTH = 01, YEAR = 2012, VALUE = 10
  • ID = 2, MONTH = 02, YEAR = 2012, VALUE = 10
  • ID = 2, MONTH = 03, YEAR = 2012, VALUE = 10
  • ...
  • ID = 2, MONTH = 12, YEAR = 2013, VALUE = 10
  • ID = 3, MONTH = 01, YEAR = 2018, VALUE = 15
  • ID = 3, MONTH = 02, YEAR = 2018, VALUE = 15
  • ID = 3, MONTH = 03, YEAR = 2018, VALUE = 15
  • ...
  • ID = 3, MONTH = 12, YEAR = 2020, VALUE = 15

Is there a best practice way to do this? Or does anyone have an idea? Thanks for any advice!

使用TO_DATE('mm / yyyy')转换字段,您可以在这里找到更多信息: https : //www.techonthenet.com/oracle/functions/to_date.php

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM