Nested Loops...looping through one month of data at a time year by year

Hi all,
I'm trying to create an insert statement that loops through a table that has 10 years of data (2001 to 2010) month by month to minimize impact on server and commits more frequently to avoid filling up the redo logs and rollback tablespaces. The table is large, has about 40 millions records per year. Lets say the structure of the table is the following:
Customer_ID number(9),
Order_Item_1 number(6),
Order_Item_2 number(6),
Order_Item_3 number(6),
Order_date date
The table is in flat format but I want to normalize it so that it looks like the following:
Customer_ID Order_Seq Order_Item Order_date
999999999 1 555555 01-jan-2001
999999999 2 666666 01-jan-2001
999999999 3 444444 01-jan-2001
888888888 1 555555 03-jan-2001
888888888 2 666666 03-jan-2001
But because I want to loop through month by month....I need to set it up so that it loops through month by month, year by year (Using the Order Date Field) and Order_item by Order_item. Something like:
so my insert statements would be something like if I hardcoded instead of put the insert statement into a loop:
insert into orders_normalized
(Customer_id,Order_seq,Order_item,Order_date) select customer_id,1,Order_item,Order_date where Order_item_1 is not null and to_char(order_date,'yyyy') = '2001' and to_char(order_date,'mm')='01';
insert into orders_normalized
(Customer_id,Order_seq,Order_item,Order_date) select customer_id,2,Order_item,Order_date where Order_item_2 is not null and to_char(order_date,'yyyy') = '2001' and to_char(order_date,'mm')='01';
insert into orders_normalized
(Customer_id,Order_seq,Order_item,Order_date) select customer_id,3,Order_item,Order_date where Order_item_3 is not null and to_char(order_date,'yyyy') = '2001' and to_char(order_date,'mm')='01';
insert into orders_normalized
(Customer_id,Order_seq,Order_item,Order_date) select customer_id,1,Order_item,Order_date where Order_item_1 is not null and to_char(order_date,'yyyy') = '2001' and to_char(order_date,'mm')='02';
insert into orders_normalized
(Customer_id,Order_seq,Order_item,Order_date) select customer_id,2,Order_item,Order_date where Order_item_2 is not null and to_char(order_date,'yyyy') = '2001' and to_char(order_date,'mm')='02';
insert into orders_normalized
(Customer_id,Order_seq,Order_item,Order_date) select customer_id,3,Order_item,Order_date where Order_item_3 is not null and to_char(order_date,'yyyy') = '2001' and to_char(order_date,'mm')='03';
Hope this makes sense.
Thanks

Does the sequence of items in an order really matter? In other words, do we really need to preserve that an item was in position 2 versus position 1? I bet that the sequence or position of each item in an order is not meaningful. They were probably numbered 1, 2, and 3 just to make them uniquely named columns so there would be three slots to hold up to 3 items in the denormalized table.
You only have about 400 million rows to insert, so it could feasibly be done in a single transaction (depending on your database environment).
You can always do a create table as select (CTAS) to help with undo / redo issues and get better performance. You could run it in parallel, and spit it out to a new table partitioned by month. Single DDL statement running in parallel making your new table--sounds good to me.
How about something like this:
CREATE TABLE ORDERS_NORMALIZED
(CUSTOMER_ID, ORDER_ITEM, ORDER_DATE)
PARTITION BY RANGE(ORDER_DATE)
PARTITION p200901 VALUES LESS THAN (TO_DATE('200902','YYYYMM')),
PARTITION p200902 VALUES LESS THAN (TO_DATE('200903','YYYYMM')),
PARTITION p201012 VALUES LESS THAN (TO_DATE('201101','YYYYMM'))
as SELECT CUSTOMER_ID, ORDER_ITEM_1, ORDER_DATE
   FROM OTHER_TABLE
   WHERE ORDER_ITEM_1 IS NOT NULL
   UNION ALL
   SELECT CUSTOMER_ID, ORDER_ITEM_2, ORDER_DATE
   FROM OTHER_TABLE
   WHERE ORDER_ITEM_2 IS NOT NULL
   UNION ALL
   SELECT CUSTOMER_ID, ORDER_ITEM_3, ORDER_DATE
   FROM OTHER_TABLE
   WHERE ORDER_ITEM_3 IS NOT NULL.....................
Out of curiosity, why not normalize it further? You could have used two tables instead of one.
One (ORDER) with:
ORDER_ID
CUSTOMER_ID
DATE
Order_id would be a new surrogate key / primary key.
Another table (ORDER_ITEM) with:
ORDER_ID
ORDER_ITEM
It would be a table that links ORDERS to ITEMS. You get the idea.

Similar Messages

  • Hi, my free trial license has expired, and I am wondering if it's possible to only pay for one month for after effects without locking for a year with monthly fee. I only need this for a project that is ending next week.

    Hi, my free trial license has expired, and I am wondering if it's possible to only pay for one month for after effects without locking for a year with monthly fee. I only need this for a project that is ending next week.

    Creative Cloud Plans
    https://creative.adobe.com/#plans

  • Send 102 bit data through the LPT port??I have to send 102 bit to my interface board using the LPT port, through one of the data line and be able to read data from the input line, I don't know how to realize such a task.Thanks

    Let me describe you the program I have to write:
    I have to send 102 bit serialy using one of the data line of the LPT port to a device and be able to read back data sent from a device register throug one one of the input port pin for instance pin 10 of a DB25, and synchronize the transmission by the PC clock(for write and read). If fact I am using 5 output control(D0...D4) signal from the LPT port, RST, TXD, CLK, CE,TEST, one input data RXD (pin10)
    So the program should work in normal mode (write data, read data), and test mode (use the write p
    rocedure). Since I am quite new in Labview I am little lost and I need some support or exemple that use a way to send more than 32 bit via one data line(in my case 102bit= 8 bit COMMAD(MSB)+88 bit SPI DATA+6 bits CRC(LSB)) and be able to read them back, place them in the register and be able either to monitor or modify them.
    I know that there are plenty of exemple but if I can gain time by being helped it would be great.
    Can you please advise?

    Hi Beni,
    find attached a SPI.vi - minor changed from one of
    my typical SPI's (LabVIEW 7.1). With some simple changes it will fit to your application.
    The only thing you need to do - prepare
    the bits for the input-array.
    If you need some more comments - find one of my email
    adresses in documentation of this vi.
    Regards
    Werner
    Attachments:
    102bit_SPI.zip ‏90 KB

  • How do I make one while loop that runs 1 piece of data at a time?

    I have a question about a while loop.  Currently, I have 4 or 5 dynamic data lines that I would like to run through a while loop that will tell me if the data is within a certain range or not.  However, the ranges are different for each piece of data, and I dont know how to do each one separately.  Do I have to make 5 different while loops? Thanks!

    pg22aw wrote:
    I have a question about a while loop.  Currently, I have 4 or 5 dynamic data lines that I would like to run through a while loop that will tell me if the data is within a certain range or not.  However, the ranges are different for each piece of data, and I dont know how to do each one separately.  Do I have to make 5 different while loops? Thanks!
    Do you want to compare the value with a defined value and also the ranges for the 5 data is different?. What are you doing in the while loop. If the data is in an array of dynamic data you can seperate the amplitude value and check the whole array of value with the ranges that is also build same as the data theh you will get the output as a boolean array.
    The best solution is the one you find it by yourself

  • To create a Universe Level filter for calculating one month's data

    Post Author: roy999
    CA Forum: Deployment
    Hi Everbody, I am creating a Monthly report using Deski XI R2. My requirement is :At
    the report prompt the user will enter only the Start Date (which will
    be the first day of previous month) and the End Date will be
    automatically taken as the last day of the previous month. Is there any way to implement this, if yes please resond ASAP... ThanksRoy

    Post Author: amr_foci
    CA Forum: Deployment
    if you use oracle connection u can make it easy and use the oracle's functions like
    CALENDAR.MY_DATE BETWEEN @Prompt('ENTER DATE','D',,MONO,FREE) AND LAST_DAY(CALENDAR.MY_DATE)
    if u dont use oracle as a target database connection u can either look for the similar function in ur DBMS for "LAST_DAY" ,, i think its available for any DBMS
    good luck

  • How to write one row of data at a time to "Write to File"

    I am trying to write 10 parameters to the LV "Write to File". This is for just one row at a time. This happens evertime I get a failure in my test routine. I am not quite sure how to accomplish this task. If I get another failure I write one row again. I testing 4 DUTS at a time so I write this row of data to each file. I am sure it is very simple.
    Thanks
    Philip

    Assuming your 10 parameters are an numeric array with 10 elements,  use "write to spreadsheet" file with append set to true. (... and if they are scalars, built the array first ).
    LabVIEW Champion . Do more with less code and in less time .

  • Can I communicate with two different FP 1000 modules through one labview program at the same time

    I am wondering could you communicate with two seperate FP 1000 modules with one labview program, using a tab control, with page 1 of the tab communicating with one FP 1000 through com port 1 and page 2 of the tab control communicating with another FP 1000 module through com port 4, using different iak files for both. Can I do it through using those wireless modems supplied by National Instruments. Sorry I dont have a name for them.

    Noely,
    Within a single IAK file, you can have multiple FieldPoint modules on separate COM ports. There is no need to use separate IAKs for the program you are describing. Actually, older versions of NI-FieldPoint do not support having multiple IAKs in use simultaneously (I am not sure whether it is currently supported).
    As for the Radio Modems, they are called SRM-6000s and can be used simultaneouslyon different serial ports. Although, using the SRM-6000 in a single master multi slave arrangement, you could use a single serial port to talk to both FP-1000s (3 SRMs total). You will need to configure the FP-1000s to have separate addresses (DIP Switch setting).
    Regards,
    Aaron

  • Month calculations: DATE() vs EDATE()

    In a recent topic (Challenge to get a date correctly), I mentioned using the DATE function to calculate a date that is (for example) one month later than a given one. Specifically, if cell A1 contains the given date, then I suggested using this formula for a date one month later:
    =DATE(YEAR(A1),MONTH(A1)+1,DAY(A1))
    Yvan Koenig suggested instead using the simpler EDATE formula, here equivalent to:
    EDATE(A1,1)
    There is, as it turns out, more than simplicity in favor of Yvan's approach. While one might expect the two formulas always to produce the same results, they do not! This is easily seen by constructing the following three column table with a column header:
    1. In cell A2, enter the last day of the first month of this year (January 31, 2008 in the U.S. system, for example). In the cell below it, enter the last day of the second month of this year (Feburary 29, 2008, for example).
    2. Next, select both cells & use the circular fill handle, drawing down to fill the column with a year or so of last-day-of month values.
    3. Select cell B1 (a header cell) & enter in the formula bar & press return:
    =DATE(YEAR(A),MONTH(A)+1,DAY(A))
    4. Likewise, in cell C1 enter:
    =EDATE(A,1)
    Note that the B & C column values are sometimes the same, sometimes not. The same results occur if the "1" in both formulas is replaced with another number of months.
    What seems to be happening is the 'MONTH(A)+n' expression uses the number of days in the month of the "A" cell value as the basis for the 'n months later' calculation, which is not the convention usually used for such things as billing cycles. The EDATE formula follows the normal convention, & is thus the preferred choice for almost all uses imaginable ... unless perhaps you are the one being billed.
    This also applies to 'YEAR(A) + n' calculations.
    So, it would seem that any calculation involving the DATE(year, month, day) form with an arithmetic operator in the year or month element should be used only with great care, if at all.
    BTW, the comments about the month unit of calendar time having "no real purpose today" in http://www.cl.cam.ac.uk/~mgk25/iso-time.html may be of interest.

    I'm aware of the blue warning triangle appearing in these "overflow" situations; however, even when it does not, the results may be different -- for example, with a starting date of January 31st, adding one month in my long formula produces a month argument that is in bounds but (for 2008) yields a date 2 days later than with the EDATE method.
    I don't view this so much a bug as a consequence of the vague nature of the "n months later" or of the "month offset" concept. As the cited scholarly article points out, the concept of the months of the year are of somewhat obscure mystic origins, & there lengths were arbitrarily set, often for reasons more political than practical. Between the 1st & 28th day of any month, the concept is unambiguous -- it is the same numbered day in the other month, but for the 29th through 31st day of the month it is not, depending on the starting month.
    From what little I have been able to discover from casual research, the EDATE results are the standard more by custom than by any well-defined rule: what we mean by the offset references the first, last, or some near-the-end-of-the-month day number, whichever seems the most suitable at the time.
    I do not have Excel on my Macs, but since the functions are similar, I would be interested in learning how that application behaves with this table.

  • Can I sign up for a month of data with Verizon without auto renewal and how long can I go without using my sim card before it is deactivated?

    When I checked out Verizon's data plans online, the only option I saw that did not involve automatic renewals was the option to pay $5 for a day of 300 MB. The other options (e.g., $20/month for 1GB) all appeared to involve automatic renewals every month unless I go in and cancel the plan. However, during a chat, a Verizon representative told me that auto renewal of the prepaid plans is an option that can be avoided, it just isn't listed in the online information. Has anyone found a way to only sign up for one month of data without having to cancel the next payment?
    Also, when I looked online at a site that compared data plans, it stated that one needed to use the Verizon network every 4 months or else the sim card would be permanently deactivated. But in a couple of online discussions, the limit was listed as being 3 months. Verizon told me 6 months. Does anyone know which is currently correct?
    Finally, I have heard that if the sim card does become deactivated, Verizon tends to insist on making one buy a new one with a regular data plan and not the prepaid options.Has anyone had any experience with this?

    GeekBoy.from.Illinois thank you for replying. I tried the Verizon support forum, but before I can ask a question there I need to register, and the first step in registering is entering my mobile phone number. Since I don't have a Verizon phone, I can't register and, therefore, can't ask any questions. I did look through previous questions and answers, but it only confirmed that Verizon employees do not give out consistently correct information. I guess I will try talking or chatting with two of three more of them and hope that whatever the majority answers are will be the correct ones.

  • Missing LO changes and creations since one month

    Hi
    We have some issues with our LO data source and now the issue has been resolved. We need to delete the requests since one month
    and reload. We have the following date selections available in the info pakcage Createdon and changed on. Can we load all the
    changes and new creation during that time with this selctions without getting duplicates ?
    Please advice

    Hi,
    created on load part is fine as you said and you should do that but
    then load the data changed between those two dates
    this part will create issue as it will send records to cube which are already present in the cube since they can have created on dates in the past.
    created on load part is fine as you said and you should do that.
    I mean....same record can have created on and changed values
    created on is static and will never change...so a record created in 01.01.1900 will not come in your selection based on created on but it is still present in the cube.
    now if the same record got changed in the last one month..i.e. the time when the delta was not working...if you give the changed on selection then it will bring the same record again...and may be if your load goes for two days then for the third time as well if it gets changed on last loading day...
    some thing like you loaded for the selection
    first you loaded created on 01.01.1900-31.09.2008
    then changed on 01.10.2008- 02.12.2008
    Now there is one record which as created on date as 01.01.1900...and changed on date as 02.10.2008...and if the old record is not deleted from the cube then it will get doubled. after load based on changed on
    also if the load keep on running for two days and is the same record got changed on 03.12.2008 then it will come thrice.
    So in any case you will have to delete the history from the cube to load the data correctly.
    Thanks
    Ajeet

  • One Month Sales

    HI Friends,
    In my Report I want to see the one month Sales Data like as,
    How much sales is happened on Monday in one coloum,
    How much sales is happened on Tuesday in one coloum and like Wednesday, Thursday, Friday and Saturday.
    Plz give me u kind reply.
    Regards,
    Rams.

    Hi Ram,
        I will give you the scenario. You should convert it into ABAP code.
    You have a variable where the user enters the date.
    Well you will have in columns values for MONDAY, TUESDAY........
    For all these columns you need to create variables which accept multiple values with no user input allowed.
    In the you have to read from the calender what all dates fall under what days of the month the user enters and return the value to the variables used in the columns.
    It will certainly be a good and workable code. It will workout. Its good if you are familiar with ABAP.
    Hope this helps you......

  • Help writing a excel macro to do a copy of 4 cells and paste transpose. I need to loop the copy and paste through 6900 rows of data.

      I need help writing a excelmacro to do a copy of 4 cells and paste transpose.  I need to loop the copy and paste through 6900 rows of data.  I started the macro with two rounds of copying & paster transposed but I need help getting it
    to loop through all rows.  Here is what macro looks like now.
        Range("I2:I5").Select
        Application.CutCopyMode = False
        Selection.Copy
        Range("J2").Select
        Selection.PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:= _
            False, Transpose:=True
        Range("I6:I9").Select
        Application.CutCopyMode = False
        Selection.Copy
        Range("J6").Select
        Selection.PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:= _
            False, Transpose:=True
    End Sub

    Thanks Jim for the solution above.
    Hi Brogents,
    Thanks for posting in our forum. Please note that this forum focuses on questions and feedback for Microsoft Office client. For any
    VBA/Macro related issues, I would suggest you to post in the forum of
    Excel for Developers, where you can get more experienced responses:
    https://social.msdn.microsoft.com/Forums/office/en-US/home?forum=exceldev
    The reason why we recommend posting appropriately is you will get the most qualified pool of respondents, and other partners who read the forums regularly can either share their knowledge or learn from your interaction with us. Thank you for your understanding.
    Regards,
    Ethan Hua
    TechNet Community Support
    It's recommended to download and install
    Configuration Analyzer Tool (OffCAT), which is developed by Microsoft Support teams. Once the tool is installed, you can run it at any time to scan for hundreds of known issues in Office
    programs.

  • Nested for loops

    Sorry to bother you all, but I have a small problem with my java homework.
    I have to make a nested for loop to produce the following output:
    00
    10 11
    20 21 22
    30 31 32 33
    (only goes from 0 to 3)
    Now, all I have managed to do are two simple loops, one with increment 10 and the other with increment just ++.
    Could you help me?

    Think through this logically. You need two loops. You need this output:
    00
    10 11
    20 21 22
    30 31 32 33
    Each number above has two digits (two digits, two loops...not a coincidence). So you need one loop to control the first digit, and another loop to control the second digit. Look at the values for the first digit: 0,1,2,3. Those are your first loop values. Now, each time the first loop has a value, your second loop goes through all of its values. Here's the values you need:
    First iteration:
    loop index 1: 0
    loop index 2: 0
    Second iteration:
    loop index 1: 1
    loop index 2: 0,1
    Third Iteration:
    loop index 1: 2
    loop index 2: 0,1,2
    Fourth Iteration:
    loop index 1: 3
    loop index 2: 0,1,2,3
    Notice the pattern for loop 2? Look at the starting value for each iteration, and notice the ending value. That gives you your loop constraints.

  • Trouble with nested foreach loop

    I got 3 nested foreach loop but the 2nd and the third one outputs no data.
    $vss = "vSwitch3"
    $vmhost_array = @("host1,host2,host3")
    $vss_vlan_array = @("vlan50")
    $dvs_vlan_array = @("n1kv_vms_vlan50")
    foreach ($vmhost in $vmhost_array) {
    foreach ($vss_vlan in $vss_vlan_array) {
    foreach ($dvs_vlan in $dvs_vlan_array) {
    Get-VMHost $vmhost | Get-VirtualSwitch -Name $vss | Get-VirtualPortGroup -Name $vss_vlan | Get-VM | Get-NetworkAdapter | Set-NetworkAdapter -Portgroup $dvs_vlan -Confirm:$false
    I get runtime error at Get-VirtualSwitch -Name $vss 
    $vss says vlan50 not found. but its there. anything wrong with this nested foreach?

    hi
    I am actually getting an error on 
    Get-VirtualPortGroup -Name $($vss_vlan_array[$i])
    Its trying to give me the whole array instead of the 1st item in the array. so get-virtualportgroup will fail.
    I checked it with $vss_vlan_array[0] and gives me 
    "vlan50","vlan49"
    how do one item at a time instead of the whole array?

  • I am looping and  through an array comparing two bigdecimals:

    I am looping and through an array comparing two bigdecimals:
    I get the max for valueB only if I have multiple items. not when I have one item why would that be:
    val=0;
    if (array.compareTo(valueB) > 0) {                                                                                                                                                                                                                                                                                                                                                                                                                                               

    Hi. You were wise to trace the value of n. Your problem seems
    to be that the tests succeed (almost always) in the very first
    iteration because they truly don't match (where n = 0) and
    execution breaks out. You need to adjust the logic. As one
    approach, while sticking with your code and not changing it too
    much (such as using more efficient int vs Number):
    at the very top, define a counter var ct:int = 0;
    before the testing loop, define a flag, such as var
    flag:boolean = false;
    you could then invert the logic in the loop and test for ==
    rather than for !=
    if you find a match, do--> flag = true;
    and break
    at the end of testing, use the flag to decide whether to
    store the new value
    if (!flag) {
    // add to array
    ct++;
    also, use a while loop for controlling the whole thing-->
    while(ct < max) { }
    to know when you're done
    You can also check out the Array functions indexOf() and
    some(), to make things more efficient and faster/easier
    when you're all done, you can also look into using if (a != b
    && c != d)
    for efficiency, instead of nesting them separately
    good luck :)

Maybe you are looking for