Decimal Result from Select Statement

I have the following select:
select (sum(state_months)/count(state_key)) from state_tab;.
If I do a select on it I get, *1*: +(state_months summed to 3 and the count for state_key is 2)+, but
if I do the following select: select 3/2 from dual, the result is *1.5*,
The numbers are declared, in the table, as follows:
state_months - number(38)
state_key - number
Is there any way to get 1.5 for the equation, *(sum(state_months)/count(state_key))* ?
The result will be put updated to a number or decimal field.
Thanks

user1687851 wrote:
I have the following select:
select (sum(state_months)/count(state_key)) from state_tab;.
If I do a select on it I get, *1*: +(state_months summed to 3 and the count for state_key is 2)+, but
if I do the following select: select 3/2 from dual, the result is *1.5*,
The numbers are declared, in the table, as follows:
state_months - number(38)
state_key - number
As an aside, it doesn't matter that state_key is a number. You are dividing sum(something) by count(somthing). Now, state_months DOES need to be a number, because you are SUMmming it. But COUNT() returns a number regardless of the data type of what is being COUNTed.
Is there any way to get 1.5 for the equation, *(sum(state_months)/count(state_key))* ?
Please show us the actual result you are getting in sqlplus:
column my_average 999,999,999.999999
select sum(state_months)/count(state_key)
from your_table;COPY the ENTIRE sqlpus session and PASTE back here.
The result will be put updated to a number or decimal field.
Thanks

Similar Messages

  • How to find the number of fetched lines from select statement

    Hi Experts,
    Can you tell me how to find the number of fetched lines from select statements..
    and one more thing is can you tell me how to check the written select statement or written statement is correct or not????
    Thanks in advance
    santosh

    Hi,
    Look for the system field SY_TABIX. That will contain the number of records which have been put into an internal table through a select statement.
    For ex:
    data: itab type mara occurs 0 with header line.
    Select * from mara into table itab.
    Write: Sy-tabix.
    This will give you the number of entries that has been selected.
    I am not sure what you mean by the second question. If you can let me know what you need then we might have a solution.
    Hope this helps,
    Sudhi
    Message was edited by:
            Sudhindra Chandrashekar

  • How to pass rowtype argument to a function from select statement?

    Hi all!
    I have function that takes mytable%rowtype as in parameter. can I pass entire row of mytable to the function from select statement? kind of
    select myfunction(mytable.*) from mytable where ....
    Thanks in advance

    The function can be used in a SQL statement only if it accepts SQL types and returns SQL type. %ROWTYPE being PL/SQL construct and not a SQL datatype, can not be used in this context.
    http://download-west.oracle.com/docs/cd/B14117_01/server.101/b10743/datatype.htm#i2093

  • Odd results from SQL statement in JSP

    Hi.
    Getting very strange results from my SQL statement housed in my JSP.
    the last part of it is like so:
    "SELECT DISTINCT AID, ACTIVE, REQUESTOR_NAME, ..." +
    "REQUESTOR_EMAIL" +
    " FROM CHANGE_CONTROL_ADMIN a INNER JOIN CHANGE_CONTROL_USER b " +
    "ON a.CHANGE_CTRL_ID = b.CHANGE_CTRL_ID " +
      " WHERE UPPER(REQUESTOR_NAME) LIKE ? ";   I've set the following variables and statements:
    String reqName = request.getParameter("requestor_name");
    PreparedStatement prepstmt = connection.prepareStatement(preparedQuery);
    prepstmt.setString(1, "%" + reqName.trim().toUpperCase() + "%");
    ResultSet rslts = prepstmt.executeQuery();
    rslts.next();
    int aidn = rslts.getInt(1);          
    int actbox = rslts.getInt(2);     String reqname = rslts.getString(3).toUpperCase();
    String reqemails = rslts.getString(4);
    String bizct = rslts.getString(5);
    String dept = rslts.getString(6);
    String loc = rslts.getString(7);
    Date datereq = rslts.getDate(8);
    String busvp = rslts.getString(9);
    AND SO ONSo then I loop it, or try to with the following:
    <%
      try {
      while ((rslts).next()) { %>
      <tr class="style17">
        <td><%=reqname%></td><td><%=reqemails %></td><td><%=bizct %></td>td><%=dept %></td>
       <td><%=aidn %></td>
      </tr>
      <%
    rslts.close();
    selstmt.close();
    catch(Exception ex){
         ex.printStackTrace();
         log("Exception", ex);
    %>AND so on, setting 13 getXXX methods of the 16 cols in the SQL statement.
    Trouble is I'm getting wildly inconsistent results.
    For example, typing 'H' (w/o quotes) will spit out 20 duplicate records of a guy named Herman, with the rest of his corresponding info correct, just repeated for some reason.
    Typing in 'He' will bring back the record twice (2 rows of the complete result set being queried).
    However, typing in 'Her' returns nothing. I could type in 'ell' (last 3 letters of his name, Winchell) and it will again return two duplicate records, but typing in 'hell' would return nothing.
    Am I omitting something crucial from the while statement that's needed to accurately print out the results set without duplicating it and that will ensure returning it?
    There's also records in the DB that I know are there but aren't being returned. Different names (i.e. Jennifer, Jesse, Jeremy) won't be returned by typing in partial name strings like Je.
    Any insight would be largely appreciated.
    One sidenote: I can go to SQL Plus and accurately return a results set through the above query. Having said that, is it possible the JDBC driver has some kind of issue?
    Message was edited by:
    bpropes20
    Message was edited by:
    bpropes20

    Am I omitting something crucial from the while
    statement that's needed to accurately print out the
    results set without duplicating it and that will
    ensure returning it?Yes.
    In this code, nothing ever changes the value of reqname or any of the other variables.
      while ((rslts).next()) { %>
      <tr class="style17">
        <td><%=reqname%></td><td><%=reqemails %></td><td><%=bizct %></td>td><%=dept %></td>
       <td><%=aidn %></td>
      </tr>
      <%
    } You code needs to be like this:while (rslts.next()) {
      reqname = rslts.getString(3).toUpperCase();
      reqemails = rslts.getString(4);
      bizct = rslts.getString(5);
      dept = rslts.getString(6);
      loc = rslts.getString(7);
      datereq = rslts.getDate(8);
      busvp = rslts.getString(9);
    %>
      <tr class="style17">
        <td><%=reqname%></td><td><%=reqemails %></td><td><%=bizct %></td>td><%=dept %></td>
       <td><%=aidn %></td>
      </tr>
      <%
    There's also records in the DB that I know are there
    but aren't being returned. Different names (i.e.
    Jennifer, Jesse, Jeremy) won't be returned by typing
    in partial name strings like Je.Well, you're half-right, your loop won't display all the rows in the result set, because you call rslts.next(); once immediately after executing the query. That advance the result set to the first row; when the loop is entered, it starts displaying at the 2nd row (or later if there are more next() calls in the code you omitted).

  • Storing variable from select statement

    I need to store a variable from a select statement into a session. Before I was taken what was passed from another page and throwing it into a session.--
    String Password= request.getParameter("Password");
    String sql = "Select WEB_USER_ID FROM Web_User WHERE PASSWORD = '" + Password +"'";
    session.setAttribute("USER",Password);
    Now though I need to put in Web_User_Id and cannot find the proper syntax. Can someone help
    session.setAttribute("User",???????);

    WEB_USER_ID is unidentified because it is a column name, not a variable name. You should first assign its value to a variable and then put the variable name into the setAttribute statement instead.
    Your query may return more than one result based on the way it is written. It sounds like you are trying to find all users with a password that is equal to the value of Password. If this is correct, then are you trying to create an attribute for each user?
    If your intention is to locate a user, then the user id and password should have been provided. A variable should be defined for both the password and user id. You should then add the user id to the where clause. After a successful search, the attributes can be created using the same syntax.

  • Passing values to OnDemandProcess from Select Statement on Reports Page

    I think I'm just missing some of the proper syntax, Can somebody please help me.
    As I've come to understand this process of passing the values,
    I recognize it as (3) steps.
    1. Referencing the "javascript" in the Select Statement of the Reports Page.
    2. Including the "javascript" in the Page Header region.
    3. Defining the Process as an OnDemand Application Process.
    My problem is:
    a. I can't seem to get the Value passed to the Application Process.
    b. I don't know how to reference the Value passed once I get it passed.
    Here's what I've got done so far:
    1. (The Select Statement)
       onClick="javascript:ORDER_CONNECT_SEQUENCE('||ID||')
    {code}
    2. (The javascript)
    {code}
    function ORDER_CONNECT_SEQUENCE('+ID+')
    var get = new htmldb_Get(null,$x('pFlowId').value,'APPLICATION_PROCESS=ORDER_CONNECT_SEQUENCE',0);
    get.add('+ID+');
    gReturn = get.get('+ID+');
    {code}
    3. (The Application Process)
    {code}
       BEGIN
       Select '+ID+' into THIS_CIRCUIT from dual;
    {code}
    I Know that the Application Process is being referenced and that the OnClick routine is working properly,
    because if I plug a Value into the Application Process, instead of using '+ID+',
    Then I get all of my desired results.
    Any help you can give me here is greatly appreciated.
    Thanks- Gary                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       

    Dimitri, Thanks for your efforts here, but its still not working for me.
    Here's what I understand from your suggestions:
    1. (changes to the javascipt)
       a. replace function ORDER_CONNECT_SEQUENCE('+ID+')
           with
           function ORDER_CONNECT_SEQUENCE(pId)
       b. replace get.add('+ID+')
           with
           get.addParam('x01',pId)
       c. replace getReturn = get.get('+ID+')
          with
          getReturn = get.get()
          get = null2. (chages to the Application Process)
       a. add l_value varchar2(4000) to my list of declarations
       b. add l_value := wwv_flow.g_x01 below the begin statement
       c. I guess then I would reference the l_value like this:
           Select l_value into THIS_CIRCUIT from dual* Also, You didn't mention any changes in my Select statement in the Reports region of the page.
    FYI.
      onClick="javascript:ORDER_CONNECT_SEQUENCE('||ID||')
    {code}
      The 'ID' which I'm referencing here is an ID from the table which I'm querying.
    Am I misunderstanding you somewhere?
    Thanks- gary                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   

  • Dynamic internal table from SELECT - statement ?

    Hi, is it possible to define an internal table just after a select statement is executed so that this internal table holds all the data that come back from the statement ?
    thanks in advance,

    Check the link -
    Re: Create Dynamic internal table
    Regards,
    Amit

  • Calling PL/SQL code from Select statement

    Hi
    I have a PL/SQL function to calculate a value.
    create or replace procedure "SR_GROSS_MARGIN"
    (netsales IN NUMBER,
    margin IN NUMBER,
    GM OUT NUMBER)
    is
    BEGIN
        IF NETSALES = 0 THEN
           GM := 0;
        ELSIF
           NETSALES < 0 THEN
           GM := 0;
        ELSE
           GM := NETSALES / MARGIN;
        END IF;
    END;How do I call this from a SELECT statement?
    Regards
    Adam

    here you go:
    create or replace function SR_GROSS_MARGIN
    (netsales IN NUMBER,
    margin IN NUMBER)
    return number
    is
    gm number;
    BEGIN
        IF NETSALES = 0 THEN
           GM := 0;
        ELSIF
           NETSALES < 0 THEN
           GM := 0;
        ELSE
           GM := NETSALES / MARGIN;
        END IF;
    return gm;
    END;then you can:
    select gm(2500,20) from dual;

  • Snapshot too old ORA-01555 from select statement (discoverer)??

    Hi All
    Am I loosing the plot .. but we have a select statement run from discoverer which is causing the famous snapshot too old error.
    My understanding is that undo is generated from select/insert/update.
    So why is the following discoverer Select statement causing the error?
    from alert
    ORA-01555 caused by SQL statement below (SQL ID: gk0wxgqmx66sh, Query Duration=3866 sec, SCN: 0x001e.089cf3f9):
    SELECT  ( ROUND(( TO_DATE(SYSDATE)-o101038.HIRE_DATE )/365,2) ) as
      "  bla bla
    ORDER BY o101020.SUB_ORGANIZATION_NAME ASC
    Thanks in Advance

    simon.9999 wrote:
    Hi All
    Am I loosing the plot .. but we have a select statement run from discoverer which is causing the famous snapshot too old error.
    My understanding is that undo is generated from select/insert/update.
    So why is the following discoverer Select statement causing the error?
    from alert
    ORA-01555 caused by SQL statement below (SQL ID: gk0wxgqmx66sh, Query Duration=3866 sec, SCN: 0x001e.089cf3f9):
    SELECT  ( ROUND(( TO_DATE(SYSDATE)-o101038.HIRE_DATE )/365,2) ) as
      "  bla bla
    ORDER BY o101020.SUB_ORGANIZATION_NAME ASC
    Thanks in Advance
    The SELECT statement is the victim.
    Some session is doing DML against the same table against which the SELECT occurs & is likely doing COMMIT inside a LOOP.

  • Dynamic rowsize from select statement.

    Hi, I'm trying to get the lenght if the returned row from a select statement but I'm not sure how to do this properly.
    The statement contains 530 columns of different type, mostly VARCHAR but also different number formats and timestamps.
    I've tried to use LENGTH() + LENGTH() but our interactive SQL client is not very happy with the enormous statement size.
    Thank you in advance.
    /76...

    I think you need something like this:
    select column_name "name", data_type "type",
    data_length "length", nullable "null"
    from all_tab_columns
    where table_name =' Your table_name'

  • View last 5 lines from select statement?

    Hi,
    I would like to view only the last 5 lines from a select statement, how would this be done?
    Something like:
    select * from a .................
    regards
    RobH

    Hi RobH,
    First of all: You need to identify a criterion that helps to sort your data in the desired way.
    Done that you can start to read about analytical functions, maybe ROW_NUMBER(), RANK() and / or DENSE_RANK() might be interesting.
    Edit:
    Changed rn() to row_number()

  • Can i get an exact part of data from select statement

    hi...good afternoon...
    i want to fetch the records from a table by select statement. but i dont want to fetch the as it is record. i want to fetch a particular part of eacg record in that column.
    for example..
    table_name=tab1
    column_name=col1
    the col1 contains datas like *'customer search-cs', 'get customer-gc', 'customer delete-cd'* etc etc..
    now while writing the select query, i want to get the descriptive output as get customer, customer search, customer delete....that is i dont want the symbols..
    is it possible to get this???
    plss suggest...

    You mean like these possible ways of doing it...
    SQL> ed
    Wrote file afiedt.buf
      1* select substr('customer search-cs',1,length('customer search-cs')-3) from dual
    SQL> /
    SUBSTR('CUSTOME
    customer search
    SQL> ed
    Wrote file afiedt.buf
      1* select regexp_substr('customer search-cs','^[^-]*') from dual
    SQL> /
    REGEXP_SUBSTR('
    customer search
    SQL>

  • Need to return only one record from select statement.

    Hello friends,
    I have a scenerio in which code only want to fetch one value from the SQL statement, but in some cases the statment return more then one row due to which ORA-01422 : Exact fetch return more then one row, error comes.
    Can you suggest me to write a select statement that will handel this. I am using the below mentioned select statement:
    EXECUTE IMMEDIATE
    'SELECT rsdn FROM ' || schema || '.table_name' ||
    ' WHERE a = :1 AND b = :2 AND c IN (32,33,34)'
    INTO v_rsdn USING v_a, v_b;
    One question, Can I use Rownum < 2 in Where clause to restrict the select output to only one record ? Please suggest ?
    Regards,
    Rajat

    Well, if rownum=1 could be a trash answer, it would be an issue since we don't know which record you are interested to. Is it one particular ? Is it only to workaround the issue ? Then you may want to manage some BULK COLLECT or whatever else array to receive all the rows as well.
    What are you doing with the rows returned ?
    Nicolas.

  • Inserting results from select query into another table

    Hi: Is there a way to insert results returned from a SQL query into another table within the same SQL statement.
    For example
    SELECT a.SRCID,
    SUM(DECODE(a.CLASS_ID,91147,1,0)) BLOCK,
    SUM(DECODE(a.CLASS_ID,91126,1,0)) ALLOW,
    FROM EVENT_DATA a
    where
    a.class_id in (91147,91126)
    GROUP BY SRCID
    order by TOTAL DESC;The columns returned SRCID, BLOCK and ALLOW need to go into another table called AGGREGATE_COUNT. This table has fields SRCID, BLOCK and ALLOW. Or is the only way this can be done from outside of SQL like using perl or C.
    Thanks
    Ray

    INSERT INTO aggregate_count
         ( chart_id
         , srcid
         , block
         , allow
    SELECT 2234  chart_id
         , srcid
         , SUM(DECODE(class_id, 91147, 1, 0))
         , SUM(DECODE(class_id, 91126, 1, 0))
      FROM event_data
    WHERE class_id IN (91147, 91126)
    GROUP BY chart_id, srcid
    ORDER BY total DESC;

  • Limiting results for select statement against view in "apps"

    Hello (again),
    Our application queries the 'apps.fnd_concurrent_programs_vl' view in order to retrieve concurrent program names (long names). We also query 'apps.fnd_application' in order to retrieve application names.
    I'm wondering, is there a way to limit the results of these based on security? Maybe, based on responsibility or user...?
    I would think if I had added a new EBS user, it would also add a corresponding DB user, which would only have access to those items that its responsibility allowed it to access, however I suppose this isn't really the case.
    Is there some way?
    EDIT: I would also like to know if there's a similar thing for log files (well, their locations at least), but I suppose if you can answer my previous question, it might also answer this one.
    Edited by: asci on Sep 20, 2012 1:01 PM

    asci wrote:
    Hello (again),
    Our application queries the 'apps.fnd_concurrent_programs_vl' view in order to retrieve concurrent program names (long names). We also query 'apps.fnd_application' in order to retrieve application names.
    I'm wondering, is there a way to limit the results of these based on security? Maybe, based on responsibility or user...?
    I would think if I had added a new EBS user, it would also add a corresponding DB user, which would only have access to those items that its responsibility allowed it to access, however I suppose this isn't really the case.
    Is there some way?
    EDIT: I would also like to know if there's a similar thing for log files (well, their locations at least), but I suppose if you can answer my previous question, it might also answer this one.
    Edited by: asci on Sep 20, 2012 1:01 PMHere is the query you need:
    SQL> SELECT *
    FROM FND_CONCURRENT_PROGRAMS_VL P,
    FND_CONCURRENT_REQUESTS R,
    FND_USER U
    WHERE P.CONCURRENT_PROGRAM_ID = R.CONCURRENT_PROGRAM_ID
    AND P.CREATED_BY = U.USER_ID;Thanks,
    Hussein

Maybe you are looking for

  • .ai to pdf for large format print

    I am working with Illustrator CS6 and I have to prepare a file that will be printed as a 6 foot x 1.5 foot banner. Actually, it will be several banners; the same design but only the name of the organization will change. I have everything on different

  • Vendor ageing report data for all doc type needed(urgent)

    hello everyone once again i need ur help guys. in the vendor ageing report i am getting data for only one document type and not of other. ex- see i have two documents type- 1.KD 2. EJ so i am getting data for document type KD only. so how to get the

  • How do I pay for the premium service?

    Whether on my iphone or my ipad, my settings panel looks nothing like the photos posted here.  There is no option to upgrade.  What do I do?

  • My Layout View is not available?

    I use number 08. Today, the Layout View item is not available. I don't know why and how to retrieve it. Someone can help me?

  • Mac mini won't mount in Firewire target mode

    I connect one non-booting Mac mini to one working Mac mini and tried to get the data off the non-booting one, but it won't mount. When I try to repair the disk using the Disk Utility from the working mini, it says: Verify and Repair disk "disk1s3" Ch