Loop on column A + condition on column B = sum (column C)


I currently have the following pl/sql:

Select sum (case when xpr.past_week = 'Y' and xpr.costgroup = '12111' then xpr.qtyout)

otherwise 0 end) as qtyout

of xxmlx_probing_report_v xpr.

What I need is for the "costgroup" column to not be limited to one! I have to be able to loop over all the costgroup values available in the X_REPORT_V view.

How can I do this? How to apply this logic in a function?

Thanks for the answers in advance.


I think that you probably just want:

SELECT xpr.costgroup, sum (CASE WHEN xpr.past_week = 'Y') THEN xpr.qtyout

ELSE 0 END) AS qtyout

OF xxmlx_probing_report_v xpr

Xpr.costgroup group;

Tags: Database

Similar Questions

  • How to get out of a loop of 2 different conditions

    Hello Experts

    I'm writing a procedure in which I take the PROVINCE as an input parameter. All provinces are stores in a table. I use a cursor variable and rowtype to control the province. After I find province I displays its code and province. Everything works very well wait for me if I can't find the province. I don't know how to get out of the loop, if I can't find the province. I already use the output when the condition. I was expecting another condition should function and must break out of the loop, but it doesn't. Can you please someone show me how to get out if I can't find the province in the table through the cursor.

    Here is my code

    Set serveroutput on
    variable G_TAX varchar2 (10)
    CREATE OR REPLACE PROCEDURE p_create (i_state VARCHAR2) is
    CURSOR cur_bb_tax is
    Select * from bb_tax;
    bb_tax_details bb_tax % rowtype;
    If it is cur_bb_tax % isopen then
    Open cur_bb_tax;
    end if;
    extract the cur_bb_tax in bb_tax_details;
    If (bb_tax_details.state = i_state) then
    dbms_output.put_line (bb_tax_details. State | bb_tax_details.idstate);
    end if;
    When the output bb_tax_details.state = i_state;
    end loop;
    close cur_bb_tax;

    while others then
    dbms_output.put_line (SQLERRM);

    Thanks in advance

    extract the cur_bb_tax in bb_tax_details;

    IF (cur_bb_tax%notfound) then  exit;END IF;

    If (bb_tax_details.state = i_state) then
    dbms_output.put_line (bb_tax_details. State | bb_tax_details.idstate);
    end if;


  • A loop until the end of the column


    I have a string type 2D table that I want to loop through. The output of the table is feeding an "analysis of the string" vi and then converted into a timestamp. The problem is that the column of my table 2D is a different size. This means that some columns have a few empty cells towards the end of the column. When the cells are feed to the 'string analysis', and it occurs. I want it first is research in the table and delete all of the empty cells or find a way to the loop to the bottom of the column and stop when empty cells begin. The table is not fixed, then it is impossible to use a constant here. Can someone help me on this one?



    Here is my interpretation of the solution of Alexander.

    Right-click on the inner circle for loop to add the conditional terminal.

  • Reports - the values of the checkbox and the conditional column visibility

    Although I use Oracle databases for many years, I must admit that I am rather new to APEX. I build a first prototype project to convince our project managers and managers of the usefulness of the product for future projects (as for interfaces Visual of the database where the focus is on forms and reports, it is often quicker and cheaper to build an APEX application instead of a webapp in Java or c# full blown).

    I have a few problems however.

    I'll start with a quick description. I have the following structures of parent/child:

    Provinces > cities > Intersections > earrings > detections in loop.

    Basically, I want to do a report on detections of loop. Now, as we have thousands of detections someday, I did kind of a mining structure down. First, you choose your province, then the city, then the intersection and finally the loop. Do not use the lists to make the selection. Instead, I did a (classic) report on each side because it allows me to give detailed information for each part. In each report, I made a link on the identifier. For example, when I select a province (by clicking on the link), I pass the identifier of the province of cities report and use it in the query to restrict my report to these cities in the province. I repeat these step for different parts, which, in the end, gives me the selected loop loop detections. The good news is that all this works very well. However, the person who will use this application made a suggestion and asked if it would be possible to select different loops and thus get the detections for several loops (the rest of the application remains the same). However, this means that I can't use the link in the column more. Now, I thought in the sense of a checkbox control. The link to the column is still there, but the user would also have the possibility to select several items in the report. I have managad to add a checkbox control to the report, associated with the loop identifier column. It's not really hard. I can't understand, however, how I can pass the identifiers found in the next report (detections for the selected loops) and use it in the report's underlying query. The problem is, I do not know how to pass the values checked to the next page and how to use these values in the request of the State of this page. I guess it must be possible, but I couldn't figure out how. I tried to play with APEX_APPLICATION. G_Fxx and others. Don't forget I'm a newbie and this seems to be more advanced features.

    I downloaded the images from the 2 on the pages of the report to illustrate:



    A second point that's bothering me is this. In the loop detection report, I show a link column (I chose an icon instead of the value of the column) for each line. I also have a column that displays the number of detections for the given line (and one day). Now, for some lines, there is no detection that has emerged. Therefore, there is no point in the selection of this loop, because the result will be a blank report. However, I still want to show the loop in the loop selection report, as it is always present (it is not as if he suddenly disappeared). Of course, no big problem and most users probably have sufficient intelligence to understand this. However, it would be nice if I could do the icon (link colum) visible only for these loops with detections, or in other words, for these recordings in which number_of_detections (COUNTY of underlying column) is greater than 0. I couldn't find a way to do it either. The conditional display for a column option didn't really offer me a solution here.

    Before I forget, I use APEX 4.0.1 on an Oracle XE database.

    Any advice or suggestions would be welcome! :)

    Thank you.



    You mean something as in this example

    You can store the values checked into collection and then use it in another report where clause

    See for example this post for how you can memorize checked lines to the collection
    Re: Need help with APEX_Collection

    In the sample report DEPT query is

      CASE WHEN EXISTS(SELECT 1 FROM emp e WHERE e.deptno = d.deptno) THEN
      END AS row_selector,
      c.c002 AS checked,
    FROM dept d,
      apex_collections c
    WHERE c.collection_name = 'DEPT_CHK_COLLECTION'
      AND d.deptno = c.c001

    And the report on EMP

    FROM emp e,
      apex_collections c
    WHERE c.collection_name = 'DEPT_CHK_COLLECTION'
      AND c.c002 = 'true'
      AND e.deptno = c.c001

    Kind regards

  • Add the elements of a single line/column of a matrix

    Hi all

    What is the best way to add all the elements of a single row or column of a given matrix the matrix and the number of this line/column on LabVIEW? The output should be a number for each row or column.


    You always said not if you use a table or a matrix.

    The transposition has to be out of the loop.

    This will give you the sum of all rows and all columns.

    These are functions and LabVIEW base constructions. If you do not understand the concepts, please visit the online tutorials.


  • Add/change the columns of data type (scripting)


    I am writing a script to add default columns and their values in tables. So far, for me, it is possible to add columns to an existing table in the model.

    Then, I want to add value to the data type. I discovered there is a method called getDataType(). So far, I have not found the "setter; for this property.

    Like other scripts, I started by creating a table with columns, and then using the API to read the values.

    A simplified version of my code:

    get the columns of table

    myTab var = model.getTableSet () .getByName ("T_POSITIONS");

    colArray = myTab.getElementsCollection () .toArray ();

    loop through columns

    for (i = 0; i < colArray.length; i ++) {}

    the type of var data = colArray [i] .getDataType ();

    model.getAppView () .log (colArray [i] + "a datatype" + dataType);

    colArray [i] .setDataType ("VARCHAR2 (1)");


    An error occurs when I want to run this script: could not find setDataType function in the object ID.

    I can't find the right method to add/change the datatype property.

    Any information would be appreciated.

    Hi Bart,.

    Here's what's in the column in the XML file:

    2015-01-13 12:45:08 UTC
    First name of the employee. A not null column.
    20 BYTE

    20 BYTES

    If you need set the size in "20 BYTE" or "20 CHAR".


  • Question PLSQL with variable compound column names

    Hello gentlemen,

    I'm new to PLSQL and I would like to display some values of columns, which are listed in the defined variable in dbms.output. I created under code, which I think it could work, but I guess that it is far from correct.

    (1000 BYTE);
    (1000 BYTE) := 'FILTER_01, FILTER_02';


    Thanks in advance for your help and advice on this issue and sincerely,


    OK, first of all, you're confused concepts here.

    cursor loop FOR waiting for a slider must be provided, and yet you offer an assignment statement and a command immediately execute for it.  Syntax is not valid.

    Secondly, you force the way of the use of dynamic SQL, which is generally bad practice.  This use of the SQL is rarely necessary, because it often indicates that functional requirements, database design, or design/code enforcement has not examined properly... the first question is "why don't you know the names of the columns to select.  With dynamic columns in your query, you need some kind of dynamic code to manage the results of the query, you will not be able to reference the columns directly by name.

    Thirdly, the DBMS_OUTPUT package doesn't make "blank".  This may sound pedantic, but it's a common mistake that people make.  There is no fill data dbms_output buffer.  It relies on call code/interface to read the contents of the output buffer and display it in fact (for example, in SQL * more you use SQL * Plus the command "set serveroutput we ' to achieve)

    In general, in a well designed where application you know your columns you would only make...

    I'm in (select distinct filter_01, filter_02 from vw_page_component_templ where component_templ_id = 10000034642)
    dbms_output.put_line (i.filter_01 | ',' | i.filter_o2);
    end loop;

    If the columns are dynamic, then you have three options:

    (1) create a ref and pass cursor who back, extracting the content for example

    sys_refcursor getRc function returns (in numbers, in colnames templ_id varchar2) is
    RC sys_refcursor;
    Open rc for ' select distinct ' | colnames |' from vw_page_component_templ where component_teml_id =: 1' using templ_id;
    return rc;

    In SQL * more:

    var r refcursor; -It creates a variable in SQL * Plus ref cursor type
    : r: = getRc (10000034642, ' filter_01, filter_02');
    print r;      -It's SQL * command to retrieve the cursor open and display the results

    (2) create an object and collection of the right structure to receive the results and use execute immediate to select IN this structure.

    (I will not show in this way, because it is absolute rubbish, and if you know the columns that you want to create the object and the collection, then you should not use dynamic SQL in the first place)

    (3) use the DBMS_SQL package to run the query and retrieve the data from the column in position rather than example:

    create or replace procedure run_query (p_sql IN VARCHAR2) is
    v_v_val varchar2 (4000);
    number of v_n_val;
    date of v_d_val;
    number of v_ret;
    c number;
    number d;
    whole col_cnt;
    Boolean f;

    rec_tab dbms_sql.desc_tab;
    number of col_num;
    v_rowcount number: = 0;
    -create a slider
    c: = dbms_sql.open_cursor;
    -analyze the SQL statement in the cursor
    DBMS_SQL. Parse (c, p_sql, dbms_sql.native);
    -run the cursor
    d: = dbms_sql.execute (c);
    -Describe the columns that are returned by the SQL statement
    DBMS_SQL. DESCRIBE_COLUMNS (c, col_cnt, rec_tab);
    -Local variables Bind to return to the different columns according to their types
    dbms_output.put_line (' number of columns in the query: ' | col_cnt);
    1.col_cnt j
    case rec_tab (j) .col_type
    When 1 then dbms_sql.define_column (c, j, v_v_val, 2000); -Varchar2
    When 2 then dbms_sql.define_column (c, j, v_n_val);      -Number
    12. When can dbms_sql.define_column (c, j, v_d_val);     -Date
    on the other
    DBMS_SQL.define_column (c, j, v_v_val, 2000);  -Any other type of return as varchar2
    end case;
    end loop;
    -Display columns are returned...
    dbms_output.put_line ('- Columns-');
    1.col_cnt j
    dbms_output.put_line (rec_tab (j) .col_name |') -' || case rec_tab (j) .col_type when 1 then 'VARCHAR2 '.
    When 2 then 'NUMBER '.
    When 12 can "DATE".
    "Otherwise, 'Other' end);
    end loop;
    -This part generates the DATA
    -Retrieves a row of data using the cursor
    v_ret: = dbms_sql.fetch_rows (c);
    -Output when no more line
    When the output v_ret = 0;
    v_rowcount: = v_rowcount + 1;
    dbms_output.put_line (' line: ' | v_rowcount);
    -Extract the value of each column of the row
    1.col_cnt j
    -Fetch each column to the correct data type according to the description of the column
    case rec_tab (j) .col_type
    When 1 then dbms_sql.column_value (c, j, v_v_val);
    dbms_output.put_line (rec_tab (j) .col_name |': ' | v_v_val);
    When 2 then dbms_sql.column_value (c, j, v_n_val);
    dbms_output.put_line (rec_tab (j) .col_name |': ' | v_n_val);
    12. When can dbms_sql.column_value (c, j, v_d_val);
    dbms_output.put_line (rec_tab (j) .col_name |': ' | to_char (v_d_val,' DD/MM/YYYY HH24:MI:SS'));))
    on the other
    DBMS_SQL.column_value (c, j, v_v_val);
    dbms_output.put_line (rec_tab (j) .col_name |': ' | v_v_val);
    end case;
    end loop;
    end loop;
    -Close the cursor, now we're done with it
    DBMS_SQL.close_cursor (c);

    SQL > run_query exec ('select ename, empno, sal, deptno from emp where deptno = 10');
    Number of columns in the query: 4
    Line: 1
    EMPNO: 7782
    DEPTNO: 10
    SAL: 2450
    Row: 2
    EMPNO: 7839
    DEPTNO: 10
    SAL: 5000
    Row: 3
    EMPNO: 7934
    DEPTNO: 10
    SAL: 1300

    PL/SQL procedure successfully completed.

    As you can see with this method, you can extract the column details, including the name and data etc. types and then extract each line and extract the data from the column position.

    Of course, this is quite complex, so it would be a last resort.

  • Transformation scripts: how to make the PK column

    Is there a function to get the name of the column of the PK to use in processing scripts?


    Try this:

    importPackage (javax.swing);

    tables = model.getTableSet () .toArray ();
    for (var t = 0; t)<>
    table = table [t];
    s = table.getPK ();
    cols = s.getColumns ();

    JOptionPane.showMessageDialog (null, COL);

    Then you loop PK columns and use getName()

  • make a column in tab form readonly but writable when you press Add the line

    Hi all

    I have several pages with tabular forms based on a single table with an ID as the key primary and filled with a sequence by a trigger. So far so good. Now, I also have the 'significant primary key', presents a second column that has a unique key constraint. Now, I want to make this column readonly tabular form for all records already there and I want to do is return to writing when I push add line. So in fact, a user can insert this 'unique key', but then he can't change it more.

    I already tried to use java script as
    < script type = "text/javascript" >
    $(document) .ready (function () {}

    var arr1 = $("td[@headers=ART_ID]");
    for (i = 0; i < arr1.length; i ++)
    If ($(arr1_).html() == null | $(arr1[i]).html () .length == 0)
    {$(arr1[i]).each (function ()}
    $(arr1[i]).readOnly = true ;}) ;
    on the other
    {$(arr1[i]).each (function ()}
    $(arr1[i]).readOnly = false ;}) ;
    < /script >

    But this only works with the "disabled" attribute, but then I had the problem that I can't introduce any line.

    Anyone with a solution for this?

    Thank you



    so you use javascript to loop on columns and set the attributes of cells to all that. You're right, disabled will cause the error message "no data found" to appear (I've had this a few days ago and first I wondered why :)).

    Of course, readonly works. You just need to write a little different:

    $x('f01_0001').readOnly = "readonly";

    and it will do the job.

    See this example:


    Denes Kubicek

  • How to run a while loop continuously without iterate loop inside the fi, a condition is met

    I enclose a VI that once the answer 28 comes the loop must stop and loop should continue an iteration. Can you show me a way for her

    You can stop the loop when a certain condition occurs: http://zone.ni.com/reference/en-XX/help/371361H-01/lvhowto/add_cond_to_for_loop/

    Add a register to offset for the while loop and to implement the feature as below:

  • How to program a loop to turn on or off

    Hi all

    I have the same code in two while controlling the four lights of loops. I use a case structure in each loop to pass two conditions (condition A and condition B).

    Provided that A light 1 LED in the first loop and LED 2 in the second

    Condition sitchecs B on 3 LED in the first loop and 4 LED in the second.

    Each case selector receives a numeric value of a waveform.

    OK, here's what I would ideally reach.

    Have an On / Off button (or similar) connected to a terminal of two loops loop condition

    If the button is enabled, loop stops 1 and 2 runs in a loop.

    If the if off button loop 2 stops and loop a restarts... IE, I want the button to turn on and off the curls with the running program

    Thank you


    You can't do what you want unless there is a larger structure around all of this, but it is easier to make everything advises RavensFan. Unless there is more problem than what you told us, it's easier to have a loop that does two things that both loops that do one thing each.


  • Stop an application of loop within an event from the front panel

    Hi all

    I was asked to add a stop button feature to a vi wrote to someone who is no longer with us. They wrote a "loop" feature is to crunch some data and adds it to a file during each iteration of the loop for. They put that inside a structure of the event. Is there a start button that when pressed triggers the event containing this loop. The program takes a long time to run each iteration of the loop (sound using serial communications to transfer data, it crunches and then add to the save file). If the wrong file, this can take a long time to run in full. If you stop there, other issues with resetting devices, what it is communicating.

    I need to add a stop button to close the event at the end of the next iteration of the loop for. is possible for the 'down' in the loop inside the event information once it has been triggered? I have very little experience with the structure of the event other than Basic programs to test.

    See you soon,.


    If this description does not describe the problem, I can publish a modified version of the .vi showing the basic idea of what they have done and what I want to achieve. I am unable to put the real .vi due to NDA issues.

    You can right click on your for loop and add a conditional terminal:

  • stop the loop inside while loop

    Hi all

    Is there a way to stop a loop within a while loop? Error-free program will stop running when you press the stop button, but I want to while loop continues to run in and only for the loop stops when the condition is met.

    Thank you!

    Hi TayAus 1.

    If you want to run the execution once? Maybe something like below? The code inside the structure of events can only run when the event source is triggered. For example, the code in the digital works only when it detects a change in the numerical value (just an example).

    When the button is selected, it should raise the event to stop as shown below:

    If you want to create the event source, create the structure of the event > right-click on the frame and select edit events dealing with this... > This should open Edit events.

    The example below is that I put the event source for the change in the value of x and x 2.

    So when x or x 2 value is changed, executes the code in the sous-schema event shown below.

    Warm greetings,


  • Looping through the operator stored in the table

    I have a table test

    > =.100
    < =.10

    create table test (oprtr varchar2 (5), op_val number);

    Insert test values (' > =', 100);

    Insert into test values ('=', 50);

    insert into test values ("< =', 10");

    Insert test values (' < ', 5);

    I tried to loop through all the conditions of this table, this table is configurable, and there are cases where the new terms will be added and the old conditions can be modified or deleted,

    I wanted to do something like that, is there a way to do it or I have to write manually if condition to test all these cases by taking all the considerartion operator.

    Val: = 500;

    I'm in (select * from test)


    If val i.oprtr i.op_val then

    dbms_output.put_line ('True Condition is' |: o PRTR);

    end if;

    end loop;

    Please help me if there is a way to do it.

    Hello raj4tech

    Here's an example how you can solve this problem:


    Val the NUMBER: = 500;

    v_statement VARCHAR2 (500);

    v_result VARCHAR2 (10);


    FOR I IN (SELECT oprtr, op_val





    v_statement: = 'SELECT BOX WHEN "| To_char (Val). ' ' || i.oprtr | ' ' || To_char (i.op_val) | "THEN" TRUE"OTHER"FALSE"END AS A RESULT OF THE DOUBLE."


    IN v_result;

    DBMS_OUTPUT. PUT_LINE (' "' |") To_char (Val). ' ' || i.oprtr | ' ' || To_char (i.op_val) | ""--> Result: ' | v_result);





    "500 > = 100 '--> result: TRUE

    '500 = 50'--> result: FALSE

    ' 500 <= 10" ="" --="">result: FALSE ".

    ' 500 < 5"   ="" --=""> result: FALSE ".

    I hope it helps.

    Best regards, David

  • The truth about the IMBRIQUEE LOOP

    Hi Experts,

    Yesterday, I looking for the differences between IN and there ARE operators. I googled and I read a thread in the site of Tom Kyte (http://asktom.oracle.com/pls/asktom/f?p=100:11:P11_QUESTION_ID:953229842074). In short, Tom wrote that the use of these two operators are different. They are also treated differently by the optimizer. IN operator works more like join two tables (hased, sorting, etc.) On the other hand, works longer EXIST as LOOP IMBRIQUEE. In other words, the inner loop runs once for each row in the outer loop. He also suggested that if the sub query is very large and high query in small. And let's say that the index of use of subquery, operator EXIST becomes much more appropriate. However, if the subquery is small FOR operator to become effective.

    In addition, Jonathan Lewis also mentions the nested loops in his book (based on Oracle Fundemantals costs) with a chapter. He says the following.

    "The execution plan of a join on the inner table index nested loops can have two different forms of 9i from: a when the optimizer uses the index on the inner table for a single scan and another when the optimizer uses the index for a range scan." The second form ceases to be an option, however, if the external table is guaranteed to return a single line. »

    My question is, if you look at the loop nested within query plans. It always works as a line returns unique index for Each row of the outer query as follows. I mean that you can come through a loop nested between two tables with FULL TABLE SCAN (usually used for this hash join). The optimizer work like this? If the join condition contains indexes between two tables and one of them is quite huge according to the other. Then the optimizer always uses NESTED LOOP rather than other join methods?

    If the above is correct, can we write the following formula?

    NESTED LOOP = QUERY EXTERNAL (a low num lines FTS) & SUB QUERY (a high number of lines with INDEX)


    | ID | Operation | Name | Lines | Bytes | Cost (% CPU). Time |


    |   0 | SELECT STATEMENT |         |     1.    58.     4 (0) | 00:00:01 |

    |   1.  NESTED LOOPS |         |       |       |            |          |

    |   2.   NESTED LOOPS |         |     1.    58.     4 (0) | 00:00:01 |

    |*  3 |    TABLE ACCESS FULL | EMP |     1.    38.     3 (0) | 00:00:01 |

    |*  4 |    INDEX UNIQUE SCAN | PK_DEPT |     1.       |     0 (0) | 00:00:01 |

    |   5.   TABLE ACCESS BY INDEX ROWID| DEPT |     1.    20.     1 (0) | 00:00:01 |




    The optimizer uses nested loops"when the following conditions are met:

    • The database attached to small subsets of data.
    • The join condition is an effective way to access the second table. ":

    The query optimizer

Maybe you are looking for

  • E-mail responded

    Suddenly, it seems that TBird (sorta) reacts to every email I receive. First message, (e.g., daily promotional coupon of the local business service) is complete as sent. From and to addresses, open the content. Second message as a reply (Re: whatever

  • faster conversion of DBL in U8 array

    Hello I need to convert an array of Monster double of a byte array. So first of all, I converted (cast) all details of array of bytes of 8 elements. The result byte array has a size of 8 x Double size of the array. It takes so long... Is there a quic

  • Windows taskbar and Start Menu

    When I open the taskbar and Start Menu properties and click Customize the Notifications, how do I remove past items?

  • How can I change a user name?

    The user name was misspelled on a bulk import, and I don't see a way to change the spelling of their name. Any suggestions would be greatly appreciated.Thank you

  • Digital signature in Adobe Reader DC

    The sign with certificate function is used in Adobe reader XI. How I do that in Adobe reader DC?