Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7522311
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T02:29:12+00:00 2026-05-30T02:29:12+00:00

I have the following configuration: a table called source (bid, valid_from, valid_to, qty) and

  • 0

I have the following configuration: a table called source (bid, valid_from, valid_to, qty) and destination (bid, jan, feb, mar, apr, …., dec).
I would like to insert the data from source into origin, (spliting the qty equally to the valid_from-valid_to months and the remaining rest to the last month in the timeframe) like this : if the record in source is

00001     01.02.2001   31.06.2001     132

this would be translated into destination :

00001  0 26 26 26 26 28 0 0 0 0 0 0

How could I do this?
Thank you!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T02:29:13+00:00Added an answer on May 30, 2026 at 2:29 am

    I’m assuming valid_from/valid_to never spans multiple years; i.e., TO_DATE(valid_from,'YYYY') always = TO_DATE(valid_to,'YYYY').

    SQL> CREATE TABLE source (
      2      bid         VARCHAR2(5)
      3  ,   valid_from  DATE
      4  ,   valid_to    DATE
      5  ,   qty         NUMBER
      6  );
    
    Table created.
    
    SQL> INSERT INTO source VALUES ('00001',TO_DATE('20010201','YYYYMMDD'),TO_DATE('20010630','YYYYMMDD'),132);
    
    1 row created.
    
    SQL> INSERT INTO source VALUES ('00002',TO_DATE('20020301','YYYYMMDD'),TO_DATE('20021231','YYYYMMDD'),59);
    
    1 row created.
    
    SQL> CREATE TABLE destination (
      2      bid         VARCHAR2(5)
      3  ,   jan         NUMBER
      4  ,   feb         NUMBER
      5  ,   mar         NUMBER
      6  ,   apr         NUMBER
      7  ,   may         NUMBER
      8  ,   jun         NUMBER
      9  ,   jul         NUMBER
     10  ,   aug         NUMBER
     11  ,   sep         NUMBER
     12  ,   oct         NUMBER
     13  ,   nov         NUMBER
     14  ,   dec         NUMBER
     15  );
    
    Table created.
    
    SQL> COLUMN jan FORMAT 999
    SQL> COLUMN feb FORMAT 999
    SQL> COLUMN mar FORMAT 999
    SQL> COLUMN apr FORMAT 999
    SQL> COLUMN may FORMAT 999
    SQL> COLUMN jun FORMAT 999
    SQL> COLUMN jul FORMAT 999
    SQL> COLUMN aug FORMAT 999
    SQL> COLUMN sep FORMAT 999
    SQL> COLUMN oct FORMAT 999
    SQL> COLUMN nov FORMAT 999
    SQL> COLUMN dec FORMAT 999
    SQL> INSERT INTO destination
      2  SELECT bid
      3  ,      NVL(MAX(DECODE(r,01,split_qty)),0)  jan
      4  ,      NVL(MAX(DECODE(r,02,split_qty)),0)  feb
      5  ,      NVL(MAX(DECODE(r,03,split_qty)),0)  mar
      6  ,      NVL(MAX(DECODE(r,04,split_qty)),0)  apr
      7  ,      NVL(MAX(DECODE(r,05,split_qty)),0)  may
      8  ,      NVL(MAX(DECODE(r,06,split_qty)),0)  jun
      9  ,      NVL(MAX(DECODE(r,07,split_qty)),0)  jul
     10  ,      NVL(MAX(DECODE(r,08,split_qty)),0)  aug
     11  ,      NVL(MAX(DECODE(r,09,split_qty)),0)  sep
     12  ,      NVL(MAX(DECODE(r,10,split_qty)),0)  oct
     13  ,      NVL(MAX(DECODE(r,11,split_qty)),0)  nov
     14  ,      NVL(MAX(DECODE(r,12,split_qty)),0)  dec
     15  FROM
     16  (
     17  SELECT x.bid
     18  ,      x.month_abbr
     19  ,      x.r
     20  ,      x.rn
     21  ,      x.total_months
     22  ,      x.qty
     23  ,      FLOOR(x.qty / x.total_months)
     24         + DECODE(x.rn
     25                  ,      x.total_months, MOD(x.qty, x.total_months)
     26                  ,      0)                           split_qty
     27  FROM  (SELECT     s.bid
     28         ,          months.r
     29         ,          ROW_NUMBER()
     30                    OVER (PARTITION BY s.bid
     31                          ORDER BY     months.r)            rn
     32         ,          COUNT(*)
     33                    OVER (PARTITION BY s.bid)               total_months
     34         ,          s.qty
     35         FROM      (SELECT     ROWNUM   r
     36                    FROM       DUAL
     37                    CONNECT BY LEVEL <= 12)  months
     38         ,          source                   s
     39         WHERE      TO_CHAR(s.valid_from,'YYYY') = TO_CHAR(s.valid_to,'YYYY')
     40         AND        months.r BETWEEN TO_NUMBER(TO_CHAR(s.valid_from,'MM'))
     41                                 AND TO_NUMBER(TO_CHAR(s.valid_to,'MM'))) x
     42  )
     43  GROUP BY bid
     44  ;
    
    2 rows created.
    
    SQL> SELECT *
      2  FROM   destination
      3  ;
    
    BID    JAN  FEB  MAR  APR  MAY  JUN  JUL  AUG  SEP  OCT  NOV  DEC
    ----- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ----
    00001    0   26   26   26   26   28    0    0    0    0    0    0
    00002    0    0    5    5    5    5    5    5    5    5    5   14
    
    SQL>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following line of code called very often: var configValue = System.Configuration.ConfigurationManager.AppSettings[ConfigValueKey];
I'm creating a model called Configuration and I have the following code and I
I have the following configuration: Visual Studio Team System 2008 SQL Server Developer Edition
I have the following configuration file for NHibernate : <?xml version=1.0 encoding=utf-8 ?> <hibernate-configuration
I have the following log4j.xml configuration: <log4j:configuration> <appender name = CONSOLE class = org.apache.log4j.ConsoleAppender>
I have a WCF service with the following configuration: <system.serviceModel> <behaviors> <serviceBehaviors> <behavior name=MetadataEnabled>
I have the following for my log4j DSL configuration in Grails 1.2: log4j =
I have the following text: started: Project: ProjectA, Configuration: Release Any CPU ------ I
I have the following requirement: In C#2.0, Given a device configuration in xml format
Say I have the following web.config: <?xml version=1.0 encoding=utf-8?> <configuration> <system.web> <authentication mode=Windows></authentication> </system.web>

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.