Group by function use in sql

I want to get an output using group by function of there is no data in the table to display a particular value
for eg:
SELECT TRUNC(updated_date) DATE1 , COUNT(1) COUNT FROM table
where TRUNC(updated_date) >=TRUNC(SYSDATE-18) AND TRUNC(updated_date) <=TRUNC(SYSDATE)
GROUP BY TRUNC(updated_date)
ORDER BY TRUNC(updated_date) DESC;
DATE1 COUNT
6/16/2012 14208
6/15/2012 307825
6/14/2012 172988
6/6/2012 138790
6/5/2012 167562
6/4/2012 51870
6/2/2012 130582
6/1/2012 239806
But i need the missed out date i.e 6/3/2012 - 0, 6/7/2012 - 0 to be displayed since there is no data on the repective dates.

Hi,
You can only display things that are in a table (or result set, but from now on, I'll just say table), or that can be derived from a table. If you want to display dates that are not in your table, then you have to get them from another table, or derive them from some table.
Some people actually keep tables of possible dates for queries like this. In this case, all you need is a table of the last 19 dates, ending with today. That's easy to derive from dual:
WITH     all_dates     AS
     SELECT     TRUNC (SYSDATE)     - LEVEL     AS date1
     ,     TRUNC (SYSDATE) + 1 - LEVEL     AS date2
     FROM     dual
     CONNECT BY     LEVEL <= 19
SELECT    a.date1
,        COUNT (x.updated_date)     AS count
FROM                all_dates     a
LEFT OUTER JOIN    table_x     x  ON     x.updated_date     >= a.date1
                            AND     x.updated_date     <  a.date2
GROUP BY  a.date1
ORDER BY  a.date1
;If you'd care to post CREATE TABLE and INSERT statements for some sample data, and the results you want from that data, then I could test this.

Similar Messages

  • Group by function use

    I want to get an output using group by function of there is no data in the table to display a particular value
    for eg:
    SELECT TRUNC(updated_date) DATE1 , COUNT(1) COUNT FROM table
    where TRUNC(updated_date) >=TRUNC(SYSDATE-18) AND TRUNC(updated_date) <=TRUNC(SYSDATE)
    GROUP BY TRUNC(updated_date)
    ORDER BY TRUNC(updated_date) DESC;
    DATE1 COUNT
    6/16/2012 14208
    6/15/2012 307825
    6/14/2012 172988
    6/6/2012 138790
    6/5/2012 167562
    6/4/2012 51870
    6/2/2012 130582
    6/1/2012 239806
    But i need the missed out date i.e 6/3/2012 - 0, 6/7/2012 - 0 to be displayed since there is no data on the repective dates.

    Please post here {forum:id=75}

  • Executing stored function using Native SQL

    In SAP documentation, I can see how to execute an Oracle stored procedure in an external database using:
    EXEC SQL.
      EXECUTE PROCEDURE procname
    ENDEXEC.
    I want to execute a stored FUNCTION within a package.  I have tried executing it using the syntax for PROCEDURE above and get a "ORA-06550 wrong type or number of arguments" error.  Since the function has one argument in and one argument out, I think I am counting the number of arguments correctly.  Has anyone executed from within ABAP an Oracle function stored in an external database?
    Thanks
    Janice Ishee

    Janice - from help.sap.com:
    Stored Procedures
    The command EXECUTE PROCEDURE proc allows you to call a procedure stored in the database. When you call it, you can pass a list of host variables as parameters. When yuo call a procedure, you must specify for each parameter whether it is an input parameter ( IN), output parameter (OUT) or changing parameter (INOUT).
    Example
    Calling a Procedure:
    DATA Y TYPE I VALUE 300.
    DATA Z TYPE I.
    EXEC SQL.
    INSERT INTO AVERI_CLNT (CLIENT, ARG1, ARG2, ARG3)
    VALUES ('000', 9, 2, 47)
    ENDEXEC.
    EXEC SQL.
    CREATE OR REPLACE PROCEDURE PROC1 (X IN NUMBER) IS
    BEGIN
    UPDATE AVERI_CLNT SET ARG3 = ARG3 + X;
    END;
    ENDEXEC.
    EXEC SQL.
    CREATE OR REPLACE PROCEDURE PROC2 (X IN NUMBER, Y OUT NUMBER) IS
    BEGIN
    SELECT ARG3 INTO Y
    FROM AVERI_CLNT
    WHERE CLIENT = '000' AND ARG1 = 9 AND ARG2 = 2;
    UPDATE AVERI_CLNT SET ARG3 = ARG3 - X;
    END;
    ENDEXEC.
    EXEC SQL.
    EXECUTE PROCEDURE PROC1 ( IN :Y )
    ENDEXEC.
    EXEC SQL.
    EXECUTE PROCEDURE PROC2 ( IN :Y, OUT :Z )
    ENDEXEC.
    IF SY-SUBRC <> 0 OR Z <> 347.
    WRITE: / 'Wrong result for EXECUTE PROCEDURE:', Z.
    ENDIF.
    EXEC SQL.
    DROP PROCEDURE PROC1
    ENDEXEC.
    EXEC SQL.
    DROP PROCEDURE PROC2
    ENDEXEC.
    Rob

  • Function used in SQL query not showing the desired results

    I have created a function that gives me the client id but when i am using the function in the query its not giving me any result i.e. 0 rows but if I execute the function sepreatly and put the result of that function in the qurey it is giving me the desired result please have a look on the following and please reply with you expert opinion:
    FUNCTION CODE:
    create or replace function j2
    (usid in varchar2)
    return varchar2
    is
    cidn_in varchar2(4000) := '';
    ls_sep varchar2(1) := ',';
    cursor predicate_cidn
    is
    select
    logical_schema_id
    from
    CLIENT_REGISTRY cr,
    MEMBER_ROLE_CLIENT_ASSIGNMENT mrca,
    MEMBER m
    where
    mrca.base_client_id = cr.base_client_id and
    mrca.member_id = m.member_id and
    cr.schema_in_use_in = '1' and
    m.active_status_id = 'A' and
    upper(mrca.member_id) = upper(usid);
    BEGIN
    for GET_CIDN in predicate_cidn
    loop
    cidn_in := cidn_in || ls_sep || '''' || GET_CIDN.logical_schema_id || '''';
    end loop;
    cidn_in := substr(cidn_in, 2);
    cidn_in := '('||cidn_in||')';
    --dbms_output.put_line(cidn_in);
    return cidn_in;
    END ;
    SQL> select j2('qaa@adp') from dual;
    J2('QAA@ADP')
    ('BASE18','SDK1TEST','SEETHA1CONFIG','BASE09','KAREN2','SIVACONFIG','BASE19','SD
    K1CTEST','VASU1','KAREN5','EVENTS1CTEST','SDK1','ADA1CONFIG','BASE10','VRB1CONFI
    G','CRU2CONFIG','BASE20','SIVATEST','PCOMCONFIG','KP1CONFIG','KAREN4CONFIG','EVE
    NTS1TEST','SUZ3CONFIG','DONG2CONFIG','ED2CONFIG','SIVACTEST','APDCONFIG','EVENTS
    1CONFIG','ESSCONFIG','SIVA','SDK2CONFIG','ED3CONFIG','RAJ4CONFIG','CBRCONFIG','N
    AMRATA2CONFIG','EMIL1','NAMRATA1CONFIG','SDK2CTEST','BVT2CONFIG','MURUGAN1CONFIG
    ','COB1CONFIG','GABY1TEST','CHR1CONFIG','ED4CONFIG','USE1CONFIG','SDK2TEST','DEM
    O1CONFIG','BERNIE1CONFIG','XA1','CAR2TEST','SDK2','ED5CONFIG','PAY1TEST','PAYXCO
    NFIG','KP1TEST','SEAN1CONFIG','CAR2CTEST','VASU1CONFIG','SEAN2CONFIG','KP1CTEST'
    ,'MAU1CONFIG','CONWCONFIG','PAN1CONFIG','MERAJ2','PRA3CONFIG','SEAN5CONFIG','KP1
    ','PRA4CONFIG','CAR2CONFIG','PRA2CONFIG','SEAN6CONFIG','PRA3TEST','RPT','GABY1CT
    EST','SDK3CONFIG','PRA2TEST','SEAN9CONFIG','CAR2','LV1CONFIG','PRA3CTEST','DONG1
    CONFIG','TE1CONFIG','MERAJ2CONFIG','VIV1CONFIG','GABY1','SEAN2','PRA2CTEST','PRA
    3','RPTCONFIG','PRA4CTEST','SEAN3','MITA1CONFIG','PRA2','RPTCTEST','PCOM1CONFIG'
    ,'SS2CONFIG','PAY1CONFIG','SEAN7','ANNCONFIG','BASE11','BASE02','LV2CONFIG','RPT
    TEST','IMPECONFIG','SUR1CONFIG','SREEK2CONFIG','BASE03','SRINU3CONFIG','BDCONFIG
    ','JAY1CONFIG','AMMAN1','PRA1TEST','WPGCONFIG','BASE12','MP5CONFIG','MERAJ1','SR
    INU2CONFIG','XA1CONFIG','RAJ1CONFIG','WPGCTEST','BASE04','CHRIS','RAJSCONFIG','P
    ANKAJ','BASE13','WPGTEST','RAJ3CONFIG','WPG','BASE14','VASU2CONFIG','SRINU1CONFI
    G','BASE05','PRA4TEST','VASU1TEST','PRA5CONFIG','NISH1CONFIG','CHRISCONFIG','SDK
    3TEST','RAJ3TEST','KP2CONFIG','BASE06','DEFPCONFIG','NISH2CONFIG','CHRISCTEST','
    WPSCONFIG','VASU2TEST','BASE15','GABY1CONFIG','PRA5TEST','MITA2CONFIG','BASE07',
    'VASU1CTEST','KP2TEST','VASU2CTEST','LAURA3CONFIG','BASE16','CHRISTEST','WPSTEST
    ','VASU2','KP2CTEST','EVENTS1','BASE17','RAJ2CONFIG','WPSCTEST','BASE08','SREEK1
    CONFIG','ADVE','PRA4','SDK1CONFIG','KP2','KAREN1','CRU1CONFIG','WPS')
    Now if i put the results (cut and paste )in the query it gives me the results
    SQL> select count(*) from baseclient.address_detail where cidn in ('BASE18','SDK1TEST','SEETHA1CONFIG','BASE09','KAREN2','SIVACONFIG','BASE19','SD
    2 K1CTEST','VASU1','KAREN5','EVENTS1CTEST','SDK1','ADA1CONFIG','BASE10','VRB1CONFI
    3 G','CRU2CONFIG','BASE20','SIVATEST','PCOMCONFIG','KP1CONFIG','KAREN4CONFIG','EVE
    4 NTS1TEST','SUZ3CONFIG','DONG2CONFIG','ED2CONFIG','SIVACTEST','APDCONFIG','EVENTS
    5 1CONFIG','ESSCONFIG','SIVA','SDK2CONFIG','ED3CONFIG','RAJ4CONFIG','CBRCONFIG','N
    6 AMRATA2CONFIG','EMIL1','NAMRATA1CONFIG','SDK2CTEST','BVT2CONFIG','MURUGAN1CONFIG
    7 ','COB1CONFIG','GABY1TEST','CHR1CONFIG','ED4CONFIG','USE1CONFIG','SDK2TEST','DEM
    O1CONFIG','BERNIE1CONFIG','XA1','CAR2TEST','SDK2','ED5CONFIG','PAY1TEST','PAYXCO
    8 9 NFIG','KP1TEST','SEAN1CONFIG','CAR2CTEST','VASU1CONFIG','SEAN2CONFIG','KP1CTEST'
    10 ,'MAU1CONFIG','CONWCONFIG','PAN1CONFIG','MERAJ2','PRA3CONFIG','SEAN5CONFIG','KP1
    11 ','PRA4CONFIG','CAR2CONFIG','PRA2CONFIG','SEAN6CONFIG','PRA3TEST','RPT','GABY1CT
    12 EST','SDK3CONFIG','PRA2TEST','SEAN9CONFIG','CAR2','LV1CONFIG','PRA3CTEST','DONG1
    13 CONFIG','TE1CONFIG','MERAJ2CONFIG','VIV1CONFIG','GABY1','SEAN2','PRA2CTEST','PRA
    14 3','RPTCONFIG','PRA4CTEST','SEAN3','MITA1CONFIG','PRA2','RPTCTEST','PCOM1CONFIG'
    15 ,'SS2CONFIG','PAY1CONFIG','SEAN7','ANNCONFIG','BASE11','BASE02','LV2CONFIG','RPT
    16 TEST','IMPECONFIG','SUR1CONFIG','SREEK2CONFIG','BASE03','SRINU3CONFIG','BDCONFIG
    17 ','JAY1CONFIG','AMMAN1','PRA1TEST','WPGCONFIG','BASE12','MP5CONFIG','MERAJ1','SR
    18 INU2CONFIG','XA1CONFIG','RAJ1CONFIG','WPGCTEST','BASE04','CHRIS','RAJSCONFIG','P
    19 ANKAJ','BASE13','WPGTEST','RAJ3CONFIG','WPG','BASE14','VASU2CONFIG','SRINU1CONFI
    20 G','BASE05','PRA4TEST','VASU1TEST','PRA5CONFIG','NISH1CONFIG','CHRISCONFIG','SDK
    21 3TEST','RAJ3TEST','KP2CONFIG','BASE06','DEFPCONFIG','NISH2CONFIG','CHRISCTEST','
    22 WPSCONFIG','VASU2TEST','BASE15','GABY1CONFIG','PRA5TEST','MITA2CONFIG','BASE07',
    23 'VASU1CTEST','KP2TEST','VASU2CTEST','LAURA3CONFIG','BASE16','CHRISTEST','WPSTEST
    24 ','VASU2','KP2CTEST','EVENTS1','BASE17','RAJ2CONFIG','WPSCTEST','BASE08','SREEK1
    25 CONFIG','ADVE','PRA4','SDK1CONFIG','KP2','KAREN1','CRU1CONFIG','WPS');
    COUNT(*)
    228
    BUT if I user the function it gives me no results
    0 rows selected and its taking a loon time to reurn

    This appears to be a duplicate of the question in this thread
    Re: VPD issue Need Assistance ASAP
    where I wrote
    "Your function returns a single string that happens to have a bunch of commas and quotes in it. An IN list generally requires a list of strings, which your function doesn't return.
    You could rewrite the function to be a pipelined table function and then do
    select count(*)
    from baseclient.address_detail
    where cidn in (SELECT cidn
    FROM TABLE(<<your pipelined table function>>))
    However, I would expect it to be easier not to wrap all this into a function and just have your IN clause be the logic in your predicate_cidn cursor.
    Justin
    "

  • Urgent *** function using in SQL *Loader

    Hi Guys,
    I am using a fuction in sql loader which returns back the maximum+1 of a primary key.
    I am getting this error when I see the log file:
    Record 2: Rejected - Error on table GT_PERILS_C24, column GT_PERILS_RECID.
    ORA-04091: table GT_LOAD.GT_PERILS_C24 is mutating, trigger/function may not see it
    ORA-06512: at "GT_LOAD.PGI_GET_NEWRECID", line 28
    ORA-06512: at line 1
    any quick suggestion will be highly appreciated.
    Tahnks in advance

    Use a sequence instead.
    The mutating table problem is well-known. It appears stupid but the problem is simply stated.
    You are inserting a row into a table. Every time you insert a row you ask the database to do a count of all the rows in that table. Should the database include the row you're inserting?
    Now you may think you know the answer in this case (i.e. No) but it's an area of uncertainty and Oracle likes to be certain. So it throws the mutating table error instead.
    Besides, it's not an approach that guarantees uniqueness and hence is inappropriate for creating keys. Much better to use a sequence.
    Cheers, APC

  • Equivalent of to_date function in Ms SQL and using it in Evaluate function

    Hi,
    I am trying to find out a function in MS SQL which is equivalent to to_date function in oracle. Please find below the advanced filter i am trying to use in OBIEE.
    Evaluate('to_date(%1,%2)' as date ,SUBSTRING(TIMES.CALENDAR_MONTH_NAME FROM 1 FOR 3)||'-'||cast(TIMES.CALENDAR_YEAR as char(4)),'MON-YYYY')>=Evaluate('to_date(%1,%2)' as date,'@{pv_mth}'||'@{pv_yr}','MON-YYYY') and Evaluate('to_date(%1,%2)' as date ,SUBSTRING(TIMES.CALENDAR_MONTH_NAME FROM 1 FOR 3)||'-'||cast(TIMES.CALENDAR_YEAR as char(4)),'MON-YYYY') <=timestampadd(sql_tsi_month,4,Evaluate('to_date(%1,%2)' as date,'@{pv_mth}'||'@{pv_yr}','MON-YYYY'))
    The statement above works fines with oracle DB with to_date function. The same statement throws an error with MS SQL since to_date is not a built in function.
    With MS SQL I tried with CAST, not sure how to pass parameters %1 and %2.
    Please help me how to use Evaluate function and passing parameters along with to_date funtion in MS SQL.
    Regards!
    RR

    Hi,
    please refer to this thread for useful information on using to_char and to_date functions of oracle in MS SQL server:
    http://database.ittoolbox.com/groups/technical-functional/sql-server-l/how-to-write-to-to_char-and-to_date-sql-server-351831
    Hope this helps.
    Thanks,
    -Amith.

  • How to use pl/sql code as ODI user defined function

    Hi All,
    i have a pl/sql code and i want to create ODI user defined function using this code .
    please find the pl/sql code below:
    ============================
    declare
    v_no_of_duplicate_rec number := 0;
    begin
    select count(*)
    into v_no_of_duplicate_rec
    from ( select 1
    from temp_pre_selections
    group by svb_number, selection_id
    having count(*) > 1);
    if v_no_of_duplicate_rec = 0 then
    return 'N';
    else
    return 'Y';
    end if;
    end if;
    ==========================
    please help me how to achieve the same .
    Thanks
    Vinod

    2 ways:
    a. implement logic in odi function directly: getCount, Oracle implementation:
    select case count(1) when 0 then 'N' else 'Y' end
    from hr.employees
    when you use this function to refresh a variable, the refresh statement should only be getCount, you shoueld not write select getCount from dual, otherwise it will become
    select select .... from ... from dual
    b. if your logic is complex, I suggest to write function directly in your database, then call this function in your ODI function
    eg:
    CREATE OR REPLACE FUNCTION hr.test RETURN varchar2 IS
    tmpVar NUMBER;
    BEGIN
    select count(1) into tmpVar from hr.employees;
    if tmpVar=0 then
    return 'N';
    else
    return 'Y';
    end if;
    END test;
    then create a ODI function, Oracle implementation is
    hr.test
    in your variable refresh statement, you can write select getCount from dual
    if you use the odi function in other locations expect for refreshing variable, the idea is similar

  • Error in nested group function used with column name.

    Hi Team,
    If i used nested group function with column name its not working. Could you please any one suggest me.
    How to use it.
    Regards,
    Venkat.
    Please find Spool ........
    SQL> select user_name,max(max(CNT)) from (select USER_NAME,count(*) CNT from v$open_cursor group by USER_NAME)
    2 group by user_name,CNT;
    select user_name,max(max(CNT)) from (select USER_NAME,count(*) CNT from v$open_cursor group by USER_NAME)
    ERROR at line 1:
    ORA-00937: not a single-group group function
    SQL> select max(max(CNT)) from(select USER_NAME,count(*) CNT from v$open_cursor group by USER_NAME)
    2 group by user_name;
    MAX(MAX(CNT))
    605

    Venkat wrote:
    Hi Sayan
    Its giving output like below, but not given maximum CNT.
    SQL> select user_name,max(CNT)from (select USER_NAME,count(*) CNT from v$open_cursor group by USER_NAME)
    2 group by user_name;
    USER_NAME MAX(CNT)
    BANES_LERG 6
    VENE_USER 8
    USER3 339
    DBUS 106
    VEL_USER 37
    SYS 597
    6 rows selected.Check it - Re: Error in nested group function used with column name.
    and previous post

  • SQL report region source to call a pl/sql function using DB link

    Hi - I have a pl/sql function fn_dbtype(id NUMBER) defined in database X. The pl/sql function executes couple DML statements and returns a string (a SELECT query). I am able to call this function using SQL Plus (Connected to Database X) as below and it works fine:
    declare
    vSQL VARCHAR2(100);
    begin
    vSQL := fn_dbtype(1);
    end;
    The DML operations completed fine and vSQL contains the "Select" query now.
    In APEX:
    I am trying to create a SQL report in APEX using SQL query(PL/SQL function returning a sql statement) option. I am trying to figure out what to put in the region source so that the output of the "Select" query is displayed in the report.
    Moreover APEX is hosted in a different database instance. So I would need to call this pl/sql function using a DB Link.
    Please let me know what I need to put in the region source to execute the pl/sql function which returns the "Select" query thereby displaying the query output in the report. Thanks.
    Edited by: user709584 on Mar 19, 2009 2:32 PM
    Edited by: user709584 on Mar 19, 2009 2:34 PM

    try something like this:
    return fn_dbtype(1)@dblink;

  • Use of ROW_NUMBER() function in PL/SQL

    I have a Table Emp with the following Structure
    SQL> desc emp
    Name Null? Type
    EMPNO NUMBER(2)
    ENAME VARCHAR2(50)
    HIREDATE DATE
    DEPTNO NUMBER(2)
    If I write a following query on this table
    SQL> SELECT deptno, hiredate, record_id
    2 FROM (SELECT deptno, ename, hiredate, ROW_NUMBER()
    3 OVER (ORDER BY hiredate) AS record_id
    4 FROM emp)
    5 WHERE record_id >= 2
    6 AND record_id <=5;
    The Result I get is
    DEPTNO HIREDATE RECORD_ID
    10 22-NOV-01 2
    10 22-NOV-01 3
    10 22-NOV-01 4
    10 22-NOV-01 5
    But if I put this query in a cursor in a PL/SQL block. The
    pl/sql does not compiles and gives me the following address
    SQL> DECLARE
    2 CURSOR c_my IS
    3 SELECT deptno, hiredate, record_id
    4 FROM (SELECT deptno, ename, hiredate, ROW_NUMBER()
    5 OVER (ORDER BY hiredate) AS record_id
    6 FROM emp)
    7 WHERE record_id >= 2
    8 AND record_id <=5;
    9 BEGIN
    10 FOR c_rec IN c_my LOOP
    11 dbms_output.put_line(c_rec.ename);
    12 END LOOP;
    13 END;
    14 /
    OVER (ORDER BY hiredate) AS record_id
    ERROR at line 5:
    ORA-06550: line 5, column 13:
    PLS-00103: Encountered the symbol "(" when expecting one of the
    following:
    , from
    Question: Can you please tell me how I can use the ROW_NUMBER()
    function in PL/SQL. I need to use this for selecting the correct
    range of records for Pagination on a website.
    Thanks in advance
    Prashant

    As Andrew said, PL/SQL hasn't caught up with the newer bits of
    SQL.  I have heard that in 9i, they will be the same, but in 8i
    there are still things that you can do in SQL that you cannot do
    directly in PL/SQL, such as the new functions like ROW_NUMBER. 
    However, you can use NDS as a work around.  The following does
    the same as what you posted:
    SET SERVEROUTPUT ON
    DECLARE
      TYPE c_my_type  IS REF CURSOR;
      c_my               c_my_type;
      TYPE c_rec_type IS RECORD
        (deptno          emp.deptno%TYPE,
         ename           emp.ename%TYPE,
         hiredate        emp.hiredate%TYPE,
         record_id       INTEGER);
      c_rec              c_rec_type;
      v_sql              VARCHAR2 (4000);
    BEGIN
      v_sql :=
      'SELECT deptno, ename, hiredate, record_id
       FROM   (SELECT deptno, ename, hiredate,
                      ROW_NUMBER() OVER
                        (ORDER BY hiredate)
                        AS record_id
              FROM    emp)
       WHERE  record_id >= 2
       AND    record_id <= 5';
      OPEN c_my FOR v_sql;
      LOOP
        FETCH c_my INTO c_rec;
        EXIT WHEN c_my%NOTFOUND;
        DBMS_OUTPUT.PUT_LINE (c_rec.ename);
      END LOOP;
    END;
    However, you stated that you need it for selecting the correct
    range of records for pagination on a website.  For that, you
    will want something more like this:
    CREATE OR REPLACE PACKAGE package_name
    AS
      TYPE c_my_type     IS     REF cursor;
      PROCEDURE procedure_name
        (c_my            IN OUT c_my_type,
         p_record_id1    IN     NUMBER DEFAULT 1,
         p_record_id2    IN     NUMBER DEFAULT 4);
    END package_name;
    CREATE OR REPLACE PACKAGE BODY package_name
    AS
      PROCEDURE procedure_name
        (c_my            IN OUT c_my_type,
         p_record_id1    IN     NUMBER DEFAULT 1,
         p_record_id2    IN     NUMBER DEFAULT 4)
      IS
        v_sql                   VARCHAR2 (4000);
      BEGIN
        v_sql :=
            'SELECT deptno, ename, hiredate, record_id'
        || ' FROM   (SELECT deptno, ename, hiredate,'
        ||                ' ROW_NUMBER() OVER'
        ||                  ' (ORDER BY hiredate)'
        ||                  ' AS record_id'
        || ' FROM    emp)'
        || ' WHERE  record_id >= :a'
        || ' AND    record_id <= :b';
        OPEN c_my FOR v_sql
        USING p_record_id1, p_record_id2;   
      END procedure_name;
    END package_name;

  • SQL Query (PL/SQL function body returning SQL query) when using to_char

    we are trying to build a report page of Type SQL Query (PL/SQL function body returning SQL query).
    our query is so simple, we need to extract the month from the recording_date column.
    declare
    l_query varchar2(1000);
    begin
    l_query:='select to_char(recording_date,'MM')from re_productive';
    return l_query;
    end;
    but we are having the following problem for this query
    Function returning SQL query: Query cannot be parsed within the Builder. If you believe your query is syntactically correct, check the generic columns checkbox below the region source to proceed without parsing.
    (ORA-06550: line 4, column 42: PLS-00103: Encountered the symbol "MON" when expecting one of the following: . ( * @ % & = - + ; < / > at in is mod remainder not rem <> or != or ~= >= <= <> and or like between || multiset member SUBMULTISET_ The symbol ". was inserted before "MON" to continue.)
    Notes:
    1-the query is correct and it was tested under Toad and SQL Plus.
    2- we tried Use Generic Column Names (parse query at runtime only) option but we get the same problem.
    any quick help please.

    Hi
    You haven't escaped your quotes in the string. Try this...
    DECLARE
    l_query VARCHAR2(32767);
    BEGIN
    l_query:= 'select to_char(recording_date,''MM'') from re_productive';
    RETURN l_query;
    END;Cheers
    Ben

  • Function 'myfunc' may not be used in SQL Options

    <p>
    Hello,
    I have created a simple function in a packege for use in an update statement. The function is
    FUNCTION myfnc(p1 IN t1.c1%TYPE)
    RETURN VARCHAR2
    IS
    BEGIN
    IF p1 = 3 THEN
    RETURN 'A';
    ELSE
    RETURN 'B';
    END IF;
    END myfnc;
    I use it in a procedure which exists in the same package as the function as follows
    Update t1
    set c1 = myfnc(c2);
    I am getting the compliation error Function 'myfunc' may not be used in SQL.
    There is no db manipulation going on and I have tried using
    PRAGMA RESTRICT_REFERENCES (myfnc, WNDS, WNPS, RNDS, RNPS);
    but to no avail.
    One other thing is that I thought this was compiling OK but then I decided to put the function in the package specification so I could unit test it using a db call. Now if I drop and recreate the package without the declaration in the specification the compile error still occurs. Perhaps that's my mind playing tricks but maybe there is something here I am not aware of.
    Any suggestions?
    </p>

    Hi,
    You found the solution.
    Although it's allowed to say
    Update     t1
    set     c1 = local_var;in a package, where local_var is not defined outside of the procedure, let alone the package, and
    although it's legal to say:
    local_var = myfnc (some_other_var); inside the package where myfnc is a private function (included in the package body, but not in the package header), still
    it is not legal to say:
    Update     t1
    set     c1 = myfnc (c2); If there is a problem with putting myfnc in the package header (for example, you don't want people calling it directly, for some reason), you can make it a stand-alone function, like Someoneelse did, or put in a different package, and give the owner of your first package (and no one else) EXECUTE privileges on it.
    As William pointed out, if myfnc is really this simple, you might not need a function at all.

  • Function name_function may not be used in SQL

    Dear forums,
    when i compiled my form that contain the function package, i got the error "Function <name_function> may not be used in SQL". But when i execute the function from sql plus, it works smoothly.
    What's the solution ?
    with great thanks,

    Hi Salim,
    as far as I understand you you try to use a function that is stored in the form. The select statement is worked on the database where your function is not known. You have to create this function on the database as you did it for using in SQLPlus.
    Regards,
    Werner

  • How to use Pl/Sql function in Discoverer report

    Hi Everybody,
    Would you be able to provide a workflow/guidance how to use sql function in Discoverer right from creating the function using PLSQL Developer.
    (I am new to Oracle environment... prev. worked as analyst with Microsoft Access... took PL/Sql class in school a few years ago)
    We are using Discoverer Desktop.
    a) Just emailed dba for permission to create pl/sql function.
    b) Recently got Discoverer Administrator installed on my pc.
    can you pls add to the list:
    c)....
    d)....
    etc.?
    tx for your help, sandra

    Hi Kranthi,
    Thx for your warm words....
    The links you provided are great! Thx soooo much!
    Thanks to you and the rest of forum members, am able to use what you've all taught me
    with other Discoverer reports.... so i was able to do more on my own....
    About a month ago, I got access to Discoverer Admin, and was able to paste SQL to create a custom folder.
    Then i joined the custom folder to an existing baseline folder.
    I have found that sometimes when I use calc within a calc when the calcs use analytic functions, that Discoverer
    just displays null in the column. I thought that maybe instead I could create my own function as a work-around.
    I wrote IT for permissions to create views, functions etc. Hopefully, they'll approve and can move toward learning
    pl/sql functions for Discoverer.
    tx again, sandra

  • To fetch data using group by function

    Hi Guys,
    I am having a table where it holds data with value with zero in one of the column and i need to fetch the records from the table using group by function but to display all the records if zero occurs in my table
    for eg
    table1
    id,customer_name,country
    0     sam     aus
    1     peter     ind
    1 peter    ind
    0 samy     us
    0 rayan     nz
    if i use select count(*),id from table1 group by id
    i will get
    count(*),id
    3-0
    1-2
    but wat i need is like below
    0     sam     aus
    0 samy     us
    0 rayan     nz
    2 peter ind
    whenever zero is occurring i need to display all the records but group by should work for non zero records

    The simplest way would be to have two queries and UNION them together. For example:
    WITH test_data AS
    ( SELECT 0 AS id, 'sam' AS customer_name, 'aus' AS country FROM DUAL UNION ALL
      SELECT 1 AS id, 'peter' AS customer_name, 'ind' AS country FROM DUAL UNION ALL
      SELECT 1 AS id, 'peter' AS customer_name, 'ind' AS country FROM DUAL UNION ALL
      SELECT 0 AS id, 'samy' AS customer_name, 'us' AS country FROM DUAL UNION ALL
      SELECT 0 AS id, 'rayan' AS customer_name, 'nz' AS country FROM DUAL
    SELECT cnt
         , customer_name
         , country
    FROM   ( SELECT id
                  , customer_name
                  , country
                  , COUNT(*) OVER (PARTITION BY id) AS cnt
                  , ROW_NUMBER() OVER (PARTITION BY id ORDER BY id) rn
             FROM   test_data
             WHERE  id != 0
    WHERE  rn = 1    
    UNION
    SELECT id
         , customer_name
         , country
    FROM   test_data
    WHERE  id = 0
    This gives the following results:
           CNT CUSTO COU
             0 rayan nz
             0 sam   aus
             0 samy  us
             2 peter ind

Maybe you are looking for

  • Problem with Checkbox in JTable

    This table here displays data from my MS Access database. It has 3 columns. Also I added the 4th column where this column contains checkboxes where I can select any row from the table. The checkboxes won't show except this "javax.swing.JCheckBox[,0,0

  • WIP assets has wrong Cost Centers/Profit Centers

    Hi Experts, There are several WIP (AuC) assets created after releasing the WBS Elements. Ideally the WIP assets should have the Cost Center/Profit Center similar to the ones in the WBS Elements. However the assets have different CC/PC than the WBS El

  • Add New columns on Form 992 landed costs

    Hi you all Well, at first i needed 2 new userdefined fields on OIPF2 (landed costs), after that i realize that i have no DI API objects for LandedCosts I've created a new usertable (@OIPF21) trying to synchronize it with the OIPF2, but to add the col

  • Camera Reset Frame Count

    Hi All, new here. Been using LR 4 on MBP 09 for about a year now, Love it! I'm shooting with a Fuji X100 exclusively (my only camera =). I recently updated firmware on it, and its reset frame count (up to 9500 frames). This means that the same frame

  • Speedgrade CC won't open after recent update

    After a recent update Speedgrade CC will not open. I get the following error message. Speedgrade exe.-Entry Point Not Found The Procedure Entry Point ?create@recycledMemory@utility@dvzcore@@SA?AV?$hared_ptr@VRecycledMemory@utility@dvacore@@ boost@@XZ