Two cursors with interdependency: how to code ?

Hi,
I'm a newbe to pl/sql.
I've two tables: customers and purchases. A customer can have more than on purchase. I want to create invoices, for each customer one invoice for all purchases in this year. (Simplified for this situation).
I created a cursor for customers, c_cust (select cust_id from customers).
Now I want to create a cursor for purchases for each customer in the other cursor.
But how to program in the loop of c_cust the loop for purchases of the customer in the c_cust?
Any reaction will be appreciated.
Leo

The fastest and easiest way is using a single insert statement.
Something like this:
SQL> create table customers (id, name)
  2  as
  3  select 1, 'customer 1' from dual union all
  4  select 2, 'customer 2' from dual union all
  5  select 3, 'customer 3' from dual
  6  /
Tabel is aangemaakt.
SQL> create table purchases (id, customer_id, purchase_date, amount)
  2  as
  3  select 1, 1, date '2006-11-11', 10 from dual union all
  4  select 2, 1, date '2007-01-01', 20 from dual union all
  5  select 3, 1, date '2007-02-02', 30 from dual union all
  6  select 4, 1, date '2007-03-03', 40 from dual union all
  7  select 5, 3, date '2007-04-04', 50 from dual union all
  8  select 6, 3, date '2007-05-05', 60 from dual union all
  9  select 7, 3, date '2007-06-06', 70 from dual
10  /
Tabel is aangemaakt.
SQL> create table invoices
  2  ( customer_id number(6)
  3  , year date
  4  , total_amount number(9,2)
  5  )
  6  /
Tabel is aangemaakt.
SQL> insert into invoices (customer_id,year,total_amount)
  2  select customer_id
  3       , date '2007-01-01'
  4       , nvl(sum(p.amount),0)
  5    from customers c
  6       , purchases p
  7   where c.id = p.customer_id (+)
  8     and p.purchase_date(+) between date '2007-01-01' and date '2008-01-01' - interval '1' second
  9   group by customer_id
10  /
3 rijen zijn aangemaakt.
SQL> select * from invoices
  2  /
CUSTOMER_ID YEAR                TOTAL_AMOUNT
          1 01-01-2007 00:00:00           90
          3 01-01-2007 00:00:00          180
            01-01-2007 00:00:00            0
3 rijen zijn geselecteerd.You may skip the outer join if you don't want to generate an invoice if a customer doesn't have a purchase in a year.
Groet,
Rob.

Similar Messages

  • TWO problems with "Organizer" -- How to solve?

    I am running PSE-4 on a WIN-XT PC.
    I upgraded a little while back from PSE-3 and had, so far, only worked with the Editor. (BTW, it's nice to see that Adobe finally fixed some very nasty bugs that plagged PSE-3 Editor, like the "jumping image, etc., as well as an improved speed.)
    I needed (and still need) to convert 17 pix that are in a directory on their own into High-Quality "12" JPG files. Nothing should be simpler...
    Well, first thing I realized is that, apparently, I can NOT do that in PSE-4's Editor. I had to go to the "Organizer" to try that. (Or am I missing something here?) And that's how I bumped into two crazy problems...
    PROBLEM #1:
    From the 17 PSD files (all of them contain several layers), only 12 did show up as Thumnails in the central pane.
    Why is that?
    Where did the 5 missing ones get stuck?
    HOW can I get ALL 17 files to show up in the thumbnail pane so that I can select them for a "batch-conversion" to JPG?
    PROBLEM #2:
    While in "Organizer", all I see below each thumbnail is a date! So, if I have a shoot of, say, 100 pix on a same day, I end up with 100 pix having the same "name" -- useless and insane! (IF I wanted ALL the pix shot on a given date/day, I would only want these specific pix show up in the pane, EACH with its own specific name!)
    I checked the various "defaults" that may effect the results and they seem OK. However, given the incredible lack of intuitiveness (!)this "Organizer" module has, I may have missed something somewhere or plain not figured out a twist somewhere to make the files' actual NAME show up under ech thumbnail.
    Any suggestion(s) to make these file NAMES appear under each thumbnail?
    THANKS!
    Duke

    Barb,
    Problem #2:
    You guessed right -- BINGO... and a big THANKS!
    Increasing the size of the thumbnails did the trick.. the Names appeared below the Dates.
    (I still am shocked that Adobe would leave such a "shortcoming" in that program. From a programming point of view, there are at least two easy ways to go around the "small space" problem for the fields below thumbnails having a small size... I guess we all will have to wait a couple more releases of that program to see that quirk fixed.)
    Ward,
    Problem #1:
    No luck here. Increasing the thumbnails to the max (or anywhere inbetween) did make the 5 missing file appear.
    So, I made one more File>Get photos>From file folder. Same results.
    So, I looked at the list of "files/items that were NOT imported". Interstingly enaough, ALL 17 files were listed!
    Now, for ALL of them, the "Reason" given "why" these 17 files/items weren't imported is that, according to "Organizer", "Files already exist in the catalog". And that sent me thinking about some weird possibilities...
    Somehow, that catalog has a lot of files that, from what I could recognized and figure out, were "acquired" with the "Organizer" of PSE-3. What I see in the pane where the thumbnails are is a succession of directories with some of their pictures displayed as thumbnails. And now I am wondering:
    Could it be that "Organizer" is so conceived that it doesn't accept/put a same file TWICE in the "Catalog"?
    (I'll go -- slowly -- through the hundreds of pix in that Catalog to see IF I can spot these exact same five pix in some other directory that has already been acquired)
    IF the answer turns out to be "yes, a file can only be ONCE in a catalog, no matter in what directory", then I probably will be going back to IrfanView to shuffle and convert pictures on ma system. Its logic and straightforwardness for dealing with files are a model of common sense.
    Do you know IF "Organizer" has such a 'rule' that a file can only be once in a catalog?
    From what I can see so far, I'll batch-convert the 12 files that show up as thumbnails in "Organizer" and singly convert the 5 others in "Editor".
    Thanks for the feedback!
    Duke

  • Two partitions with OSX how to get only one back

    I installed xp on my macbook and did not know it was service pack 1. Hours later I have two partitions on my macbook both with OSX now. How can I get my macbook back to one partition with OSX on it?


    Enlarging a volume
    If you have multiple volumes on a disk and one is running out of space, you may be able to enlarge it
    without losing any of its data.
    To enlarge a volume, you must delete the volume that comes after it on the disk, and then move the
    volume’s end point into the freed space. You can’t enlarge the last volume on a disk. You can see the
    order of the volumes in Disk Utility by selecting the disk, clicking Partition, and looking at the Volume
    Scheme list.
    All data on the deleted volume will be erased. Be sure to back up your disk’s data before you begin.
    To enlarge a volume:
    From the list at the left, select the disk that contains the volume you want to enlarge, and then click
    Partition.
    In the Volume Scheme list, select the volume beneath the volume you want to enlarge, and then click
    Delete〔-〕.
    The volume is immediately erased, leaving the space it formerly occupied available.
    Resize the volume.
    You can drag the divider at the bottom of the volume in the Volume Scheme list, or you can select the
    volume, and then enter a value in the Size field.
    If the enlarged volume doesn’t use all the space from the deleted volume, you can create new volumes
    in the unused space.
    To create a new volume, click Add〔+〕, select the new volume, and choose its name, format, and size.
    Click Apply.
    Disk Utility enlarges the volume and creates any new volumes.
    Kj ♞

  • Two materials with same material code

    There are 2 components of a split AC, say Indoor unit and outdoor unit.
    Now we purchase the two materials with the same material code.
    Once an order is received, these two units are picked and sold. For easy identification, both the materials are procured under the same material code,
    However, this does not give any visibility on the actual stock status of each component. So we have decided to have two different material codes.
    When these are sold, we want to be able to use a BOM that will bind these two materials. However, there is no production activity involved.  This will help us in printing the Header material on all documents like say Excise Invoice (Split AC Model 4354 - 1 EA)
    Is there a possibility in SAP to attain this ?
    Amit

    Hi Amit,
    you are not going to create two material codes in case of split valuation.
    you can refer the following links for split valuation
    Re: spilt valuation with batch management?
    http://help.sap.com/saphelp_46c/helpdata/EN/1c/1a39516e36d1118b3f0060b03ca329/content.htm
    http://help.sap.com/saphelp_46c/helpdata/EN/1e/dc30b3efb511d1a7aa0000e8a5f6c8/content.htm
    http://www.sap-img.com/mm025.htm
    or for your requirement you can manage the sam ewith different batches one is for internal and other is for external.
    regards,
    Adwait Bachuwar

  • How JSP codes residing in js function works

    Hi,
    I would like to check with you how JSP codes residing in js function works.
    How can I get the 2nd select stmt executed based on the values retrieve from 1st select statement and the conditions pass in to this js function
    How can I get the select statement to execute only when a condition is met.
    The following is the codes. It is not executing the 2nd select statement. when I didnt put in the 2nd select stmt, i execute those codes in that portion
    script
    ====
    function test(obj, obj2) {
    <%
    sql_query = "SELECT a1, a2 , a3, a4, a5 "+
    " FROM table_a ";
    try {
    rset = db.execSQL(sql_query);
    catch(SQLException e){
    System.err.println ("Error in query " +e);
    %>
    var po_ln_fnd = false
    <% while (rset.next()) {
    j_a1 = rset.getString("a1");
    j_a2 = rset.getString("a2");
    j_a3 = rset.getString("a3");
    %>
    if ((eval(obj2.value)== '<%=j_a1%>')) {
    if ((obj.value == '<%=j_a2%>') ) {
    <% j_a4 = rset.getString("a4");
    j_a5 = rset.getString("a5");
    sql_query = "SELECT b2, b3, b4 "+
    " FROM table_b "+
    " WHERE b1 = '"+j_a3+"' ";
    try {
    rset = db.execSQL(sql_query);
    catch (SQLException e) {
    System.err.println("Error in query " +e);
    while (rset.next()) {
    String j_b2 = rset.getString("j_b2");
    String j_b3 = rset.getString("j_b3");
    String j_b4 = rset.getString("j_b4");
    %>
    <%}>
    }

    what is the other alternative to produce the same results: (retrieve the data based on the value entered on screen, upon onChange, it will chk whether the condition met, if met, will retrieve data from another table and display on the screen - all this is done without the submit button being pressed)

  • I have a plot with two cursors - how can I control the "visible" attribute of the second cursor?

    I have a waveform graph with multiple plots and two cursors. I can use the "cursor->visible" property to control cursor #1 but I cannot seem to find a way to control the visible attribute for cursor #2. Is there a way to do this?
    Thanks,
    RGA

    There's an "Active Cursor" property that allows you select which cursor the following actions operate on. Remember that property nodes "execute" from top to bottom...
    Mike...
    Certified Professional Instructor
    Certified LabVIEW Architect
    LabVIEW Champion
    "... after all, He's not a tame lion..."
    Be thinking ahead and mark your dance card for NI Week 2015 now: TS 6139 - Object Oriented First Steps

  • How can I get extract the data between two cursors on an XY graph

    How can I get extract the data between two cursors on an XY graph

    Well, you say xy graph, so this might be a more complicated problem.
    For a waveform graph it's trivial. Simply get the two cursor indices (property: cursor index) and apply them to array subset of the data. Is that all you need?
    Here's how the above code would look like. using cursor.index instead of cursor.x elimnates the need to include scaling information.
    For an xy graph, there could be multiple segments (e.g. imagine a spiral that passes the desired x range multiple times from both sides). This would neeed significantly more code for a general solution.
    Message Edited by altenbach on 11-24-2009 07:53 AM
    LabVIEW Champion . Do more with less code and in less time .
    Attachments:
    cursorsubset.png ‏17 KB

  • How to skip a selection screen mandatory field in debugging mode with out changing the code?

    Hi Team,
    In my report there is two fields on selection screen, one is obligatory field , but i just want to execute the report(for testing per pus) with out entering  the data for the mandatory field with out changing the code. could u let me know which event and how to debug the report. or else  in debugging mode how can i change that field as non mandatory field.
    Regards,
    Venkat.

    If the value is not of importance how come it became obligatory in your program?
    Any how you can do one thing...Put some value in your obligatory field, put a debugger point where you are interested in  and use the pencil tool to change the value of the field while debugging. 
    Regards,
    Philip.

  • How to code with tab strip control in se38

    HI masters,
          In SE38 i created tab strips with 4 tabs in selection screen. Each selection screen having 4 or 5 input fields.So whenever user click on first screen he will fill the input on that screen, and based on that fields only he will get information. Each tab is totally differ from other tabs, i mean first tab is orders and second is invoice like that. so there is no connection betwen tabs data. Everything is individual tabs.
         I dont know how to code that.Can you plz help me how to code that? Thanks in advance.Its very urgent.

    To create a tabstrip control area, choose Tabstrip control from the object list in the Screen Painter and
    place it on the screen. Fix the top-left hand corner of the table control area, and then drag the object to
    the required size.
    Assign a name to the tabstrip control in the Object name attribute. You need this name to identify your
    tabstrip control.
    In your ABAP program use the CONTROLS statement to declare an object with the same name. Use
    TABSTRIP as the type.
    The type TABSTRIP is defined in the type pool CXTAB. The field ACTIVETAB contains the function
    code of the tab title of the currently active tabstrip. The other fields are reserved for internal use.
    The default number of tab pages for a tabstrip control is two.
    Technically, tab titles are treated in the same way as pushbuttons. They have a name, a text, a function
    code, and a function type. You enter these in the Name, Text, FctCode and FctType fields of the object
    attributes.
    A tab title can have the function type ' ' (space) or 'P'. If the function type is ' ' (space), the PAI
    processing block is triggered when the user chooses that tab, and the function code of the tab title is
    placed in the command field. If the function type is 'P', the user can scroll between different tab pages of
    the same type without triggering the PAI processing block. For further details, see the following pages.
    If you want your tabstrip control to have more than two pages, you must create further tab titles. To do
    this, choose Pushbutton from the object list in the Screen Painter and place it in the tab title area.
    You must assign a subscreen area to each tab page.
    The subscreen area assigned to a tab page is automatically entered as the Reference object (in the
    Dictionary attributes) for the tab title of that page.
    To assign a subscreen area to one or more tab pages, choose the relevant tab title in the fullscreen
    editor, choose the Subscreen object, and place it on the tab page.
    Alternatively, you can assign a single subscreen area to several tab pages by entering the name of the
    subscreen area directly in the Reference object field of the attributes of the relevant tab pages.
    If you have assigned a different subscreen area to each page element in a tabstrip control, you can
    scroll between the pages locally at the front end.
    To do this, you must send all of the subscreens to the front end when you send the main screen itself. All
    of the tab titles in the tabstrip control must also have function type 'P'.
    Now, when you scroll between the different page elements, there is no communication between the
    presentation server and the application server.
    When the user chooses a function on the screen that triggers PAI processing, the system processes the
    PAI blocks of all of the subscreens as well. This means that all of the field checks are run. In this
    respect, you could regard the tabstrip control as behaving like a single large screen.
    Local scrolling in tabstrip controls is more appropriate for display transactions.
    <b>Screen Painter:</b>
    <b>PROCESS BEFORE OUTPUT.</b>
    CALL SUBSCREEN subarea1
    INCLUDING SY-CPROG '0101'.
    CALL SUBSCREEN subarea2
    INCLUDING SY-CPROG '0102'.
    CALL SUBSCREEN subarea3
    INCLUDING SY-CPROG '0103'.
    <b>PROCESS AFTER INPUT.</b>
    CALL SUBSCREEN subarea1.
    CALL SUBSCREEN subarea2.
    CALL SUBSCREEN subarea3.
    <b>ABAP:
    CONTROLS: my_tab_strip TYPE TABSTRIP.</b>
    To program a tabstrip control to scroll locally at the front end, you must:
    Assign
    a separate subscreen area to each tab page; a subscreen will be sent to each of these
    when the screen is processed.
    Call
    all of the subscreens from the flow logic.
    Assign
    function code type 'P' to all of the tab titles.
    The system hides any page element whose subscreen contains no elements that can be displayed.
    If there are no page elements containing elements that can be displayed, the system hides the entire
    tabstrip control.
    Hope this is helpful, Do reward.

  • How can I group two annotations with JavaScript?

    I create two annotations with JavaScript:
    var annot1 = this.addAnnot({
                      page: 0,
                      type: "Square",
                      rect: [150, 150, 180, 180],
                      name: "OnMarketShare",
                      author: "A. C. Robat",
                      contents: "This section needs revision."});
    var annot2 = this.addAnnot({
                      page: 0,
                      type: "Square",
                      rect: [200, 200, 300, 300],
                      name: "OnMarketShare",
                      author: "A. C. Robat",
                      contents: "This section needs revision."});
    How can I group them with JavaScript?

    Can I select them in JavaScript Code?
    like:
    annot1.selected = true;     //wrong JavaScript code.
    annot2.selected = true;     //wrong JavaScript code.
    annot3.selected = true;     //wrong JavaScript code.
    annot4.selected = true;     //wrong JavaScript code.
    If I can do these, I can group them easily : right-click them, and then, click "group" button.

  • Want to Join two tables with cursors

    Can anyone tell me how to join two tables only by cursors.
    Not with:
    1.Joining condition
    2.Procedure etc.
    Only cursors with passing parameters.
    Sowmya

    This is my assignment. I want to get the matching records from both tables. I have pk, fk for both tables for deptno.Can I pass parameter for this.
    dept table:
    deptno deptname
    10 sales
    20 Erp
    30 hr
    40     marketing
    emp table:
    Empno deptno Name
    1 10 lee
    2 20 hans
    3 10 peter
    4 30 james
    5 30 king
    Output should be:
    deptno empname empno deptname
    10 lee 1 sales
    10 peter 3 erp
    30 james 4 hr
    30 king 5 marketing

  • How to compare two cursors

    Hi all,
    I have two different cursors of same recors.
    create table temp_emp as select * from emp;
    cursor cr_emp IS
    select * from emp;
    cursor cr_tmp IS
    select * from temp_emp;
    how to compare these two cursors.
    Im trying to learn comparing cursors variables. I have oracle 11g, windows Vista.
    Initially, I tried to compare only ename coloum:
    create or replace procedure compare
    IS
    cursor cr_emp IS
    select * from emp;
    cursor cr_temp IS
    select * from temp_emp;
    v_cr_emp cr_emp%rowtype;
    v_cr_temp cr_temp%rowtype;
    emp_rownum integer;
    tmpy_rownum integer;
    V_CODE NUMBER;
    V_ERRM VARCHAR2(64);
    BEGIN
    select MAX(rownum) into emp_rownum from emp;
    select MAX(rownum) into tmpy_rownum from tmpy_emp;
    open cr_emp;
    open cr_temp;
    FETCH CR_EMP INTO v_cr_emp;
    FETCH CR_TEMP INTO v_cr_temp;
    for emp_rownum in cr_emp
    loop
    for tmpy_rownum in cr_tmpy
    loop
         if(v_cr_emp.ename = v_cr_temp.ename)
         then
         dbms_output.put_line('equal');
         else
         dbms_output.put_line( 'not equal');
         end if;
    end loop;
    end loop;
    EXCEPTION
    WHEN OTHERS
    THEN
    V_CODE := SQLCODE;
    V_ERRM := SUBSTR(SQLERRM,1,64);
    DBMS_OUTPUT.put_line ('ERROR CODE' || V_CODE || ':' || V_ERRM);
    close cr_emp;
    close cr_tmpy;
    END;
    SQL> start rain.sql
    Procedure created.
    SQL> exec compare
    ERROR CODE-6511:ORA-06511: PL/SQL: cursor already open
    PL/SQL procedure successfully completed.
    Edited by: user10676396 on Apr 16, 2009 7:50 PM

    Hi user10676396,
    Hope this helps you understand the Nested loop, which I assumed you are trying to do in you program.
    Note : Code not compiled and tested.
    create or replace procedure compare
    IS
    cursor cr_emp IS
    select * from emp;
    cursor cr_temp IS
    select * from temp_emp;
    v_cr_emp cr_emp%rowtype;
    v_cr_temp cr_temp%rowtype;
    emp_rownum integer;
    tmpy_rownum integer;
    V_CODE NUMBER;
    V_ERRM VARCHAR2(64);
    BEGIN
    select MAX(rownum) into emp_rownum from emp;
    select MAX(rownum) into tmpy_rownum from tmpy_emp;
    FOR REC IN cr_emp LOOP
         dbms_output.put_line('For Ename in EMP : '||REC.ename ');
    -- In FOR loops demo concept of nested loops
      FOR TREC in cr_temp LOOP
         if(REC.ename = TREC.ename)
         then -- only output when you found a record in Temp
         dbms_output.put_line('....EQUAL rec found in Temp : ' ||TREC.ename');
         else
           null ; -- Don't output if it is not equal
         end if ;
      END LOOP;
    END LOOP;
    EXCEPTION
    WHEN OTHERS
    THEN
    V_CODE := SQLCODE;
    V_ERRM := SUBSTR(SQLERRM,1,64);
    DBMS_OUTPUT.put_line ('ERROR CODE' || V_CODE || ':' || V_ERRM);
    close cr_emp;
    close cr_tmpy;
    END;
    /Sanjay

  • How to declare a cursor with stored proc?

    Hi All,
    Can we declare a cursor with stored proc?
    For Example -
    CREATE PROCEDURE DDL_proc() LANGUAGE SQLSCRIPT AS
        CURSOR c_cursor1 (v_isbn VARCHAR(20)) FOR CALL SYS.GET_OBJECT_DEFINITION(<SCHEMA_NAME>, <TABLE_NAME>).;
         BEGIN
              FOR cur_row as c_cursor1 DO
              END FOR;
         END;
    Could you please have a look on the same?
    Thank you,
    Vijeesh

    Oracle PL/SQL also has a select into statement which is described in the same manual the link takes you to part of.
    select column_list
    into variable_list
    from table(s)
    where conditions
    The PL/SQL Users Guide is something you are going to want to have gone over cover to cover before you start converting because so that you make the best choices for how to rewrite the code: select into, explicit cursor, implicit cursor, for loop, simple loop, while loop, collections,bulk load, etc ....
    HTH -- Mark D Powell --

  • I have two users with different music on each itunes and i can only use one library, how do i use both for one iTouch?

    i have two users with different music on each itunes and i can only use one library, how do i use both for one iTouch?

    Chris, I believe this link may have the information you're looking for. Welcome to discussions!
    http://docs.info.apple.com/article.html?artnum=300432

  • How to use two split this method in my code

    How to use two split this method in my code
    if i got one string line which like this
    ("aa!bb!cc~ab!bc!cd") a
    nd want to use two split to spare ! and ~ this seal for my spare point how that output
    has come diff ?
    public static void main(String[] args) {
        String str = "aa!bb!cc~ab!bc!cd";
        String strs[]= str.split("~");
        String strE[]= str.split("!");
        int count =0;
        for(int j=0; j < strs.length; j++){
          for (int i = 0; i < strE.length; i++){   
              System.out.println(count + " " + strE);
    count++;
    the output how can it be like this
    0 aa
    0 bb
    0 cc
    1 ab
    1 bc
    1 cd

    Move your second slit inside the first loop, so you are splitting the substring, not the entire string.

Maybe you are looking for

  • Exchange 2013 / 2010 co-existence - Outlook won't connect to Exchange 2010 mailboxes

    Greetings!  I have a lab set up at home where I have been testing co-existence of Exchange 2013 and 2010 for a future corporate upgrade project. I am running into some odd behavior.  Any mailbox that has been migrated to Exchange 2013 works just fine

  • Can I set parental control on firefox for a specific user only?

    I'm using windows 7 and firefox 18.0.2. On my PC I have my Admin account and my kids account. Now as they're coming to an age they're needing Internet access for projects and so on, I wonder if I can set parental control on firefox (by add-on?) as I

  • How can i decide candidates for line item dimension?

    1Q): we have many infocubes out of all these infocubes, i have to decide which infocubes are the candidates for lineitem dimension? How to do it? Please tell me the technical specs how to do the analysis to find out the candidates for line item dimen

  • Canceled pre-order issue

    I canceled a pre-order ablum but when I went back to the store the ablum still says that I have it pre-ordred even though my account does not show it anymore. Does it take time to clear out ?

  • How to make sure frames are present

    i am trying to open two web pages in a single window , but while browsing i am not able to keep my frames , when i press some links the frames r lost. What should i do to keep my frames in place and open pages without losing any frames i have used al