'GROUP BY' clause in Database views

Hi,
I use SE11 to create database view from a table. I wonder if it is possible to specify a 'GROUP BY' clause? Basically I need a database view that looks like this:-
SELECT FISCAL_PERIOD, SUM(QUANTITY)
FROM TABLE A
GROUP BY FISCAL_PERIOD
Please help!
Thanks,
CH

Yes. I want to create a view that has values that are already aggregated by a field.
Example view that i created directly in SQL database:
CREATE VIEW ABC (F1, SUM_QTY)
AS SELECT F1, SUM(QTY)
FROM TABLE1
GROUP BY F1
My question is how to achieve the same result using Database view in SAP through transaction SE11 or any others?
Thanks,
CH

Similar Messages

  • Error in ADF-BC Database view instead of insert trigger

    I received the following errors when trying to insert. Any help or hint will be greatly appreciated.
    JBO-26041: Failed to post data to database during "Insert": SQL Statement "BEGIN INSERT INTO DEV_EQMT_HISTORY_BY_CODE(ID,D_E_ID,STATUS_KEY) VALUES (?,?,?) RETURNING ID, ROWID INTO ?, ?; END;".
    ORA-22816: unsupported feature with RETURNING clause ORA-06512: at line 1
    The followings are the tables and view I used:
    create table device_eqmt (
    id number(6),
    code varchar2(40)
    alter table device_eqmt
    add (constraint device_eqmt_pk primary key (id));
    create table device_eqmt_history (
    id number,
    d_e_id number(6),
    status_key number(6),
    alter table device_eqmt_history
    add (constraint device_eqmt_history_pk primary key (id));
    alter table device_eqmt_history
    add ( constraint device_eqmt_hist_de_id_fk
    foreign key (d_e_id)
    references device_eqmt(id)
    CREATE TRIGGER dev_eqmt_hist_bi
         BEFORE INSERT ON device_eqmt_history
    FOR EACH ROW
    BEGIN
    SELECT SHARED_SEQ.nextval
    INTO :new.ID
    FROM dual;
    END;
    create view dev_eqmt_history_by_code
    as select h.id,
    h.d_e_id,
    de.code,
    h.status_key
    from device_eqmt_history h, device_eqmt de
    where h.d_e_id = de.id
    CREATE OR REPLACE TRIGGER dev_eqmt_hist_by_code_insert
    INSTEAD OF INSERT ON dev_eqmt_history_by_code
    REFERENCING NEW AS n
    FOR EACH ROW
    DECLARE
    deid number;
    BEGIN
         SELECT de.id INTO deid FROM device_eqmt de WHERE de.code = :n.code;
         INSERT INTO device_eqmt_history (id, d_e_id, status_key)
         VALUES (:n.id, deid, :n.status_key);
    END;

    The Oracle database doesn't support the returning clause for database views using instead-of triggers. ADF BC inherently includes the returning clause, if in a relating EO you have the refresh-after-insert or refresh-after-update property set on for an attribute.
    Hope this helps.
    CM.

  • Creation of view with clob column in select and group by clause.

    Hi,
    We are trying to migrate a view from sql server2005 to oracle 10g. It has clob column which is used in group by clause. How can the same be achived in oracle 10g.
    Below is the sql statament used in creating view aling with its datatypes.
    CREATE OR REPLACE FORCE VIEW "TEST" ("CONTENT_ID", "TITLE", "KEYWORDS", "CONTENT", "ISPOPUP", "CREATED", "SEARCHSTARTDATE", "SEARCHENDDATE", "HITS", "TYPE", "CREATEDBY", "UPDATED", "ISDISPLAYED", "UPDATEDBY", "AVERAGERATING", "VOTES") AS
      SELECT content_ec.content_id,
              content_ec.title,
              content_ec.keywords,
              content_ec.content content ,
              content_ec.ispopup,
              content_ec.created,
              content_ec.searchstartdate,
              content_ec.searchenddate,
            COUNT(contenttracker_ec.contenttracker_id) hits,
              contenttypes_ec.type,
              users_ec_1.username createdby,
              Backup_Latest.created updated,
              Backup_Latest.isdisplayed,
              users_ec_1.username updatedby,
              guideratings.averagerating,
              guideratings.votes
         FROM users_ec users_ec_1
                JOIN Backup_Latest
                 ON users_ec_1.USER_ID = Backup_Latest.USER_ID
                RIGHT JOIN content_ec
                JOIN contenttypes_ec
                 ON content_ec.contenttype_id = contenttypes_ec.contenttype_id
                 ON Backup_Latest.content_id = content_ec.content_id
                LEFT JOIN guideratings
                 ON content_ec.content_id = guideratings.content_id
                LEFT JOIN contenttracker_ec
                 ON content_ec.content_id = contenttracker_ec.content_id
                LEFT JOIN users_ec users_ec_2
                 ON content_ec.user_id = users_ec_2.USER_ID
         GROUP BY content_ec.content_id,
         content_ec.title,
         content_ec.keywords,
         to_char(content_ec.content) ,
         content_ec.ispopup,
         content_ec.created,
         content_ec.searchstartdate,
         content_ec.searchenddate,
         contenttypes_ec.type,
         users_ec_1.username,
         Backup_Latest.created,
         Backup_Latest.isdisplayed,
         users_ec_1.username,
         guideratings.averagerating,
         guideratings.votes;
    Column Name      Data TYpe
    CONTENT_ID     NUMBER(10,0)
    TITLE          VARCHAR2(50)
    KEYWORDS     VARCHAR2(100)
    CONTENT          CLOB
    ISPOPUP          NUMBER(1,0)
    CREATED          TIMESTAMP(6)
    SEARCHSTARTDATE     TIMESTAMP(6)
    SEARCHENDDATE     TIMESTAMP(6)
    HITS          NUMBER
    TYPE          VARCHAR2(50)
    CREATEDBY     VARCHAR2(20)
    UPDATED          TIMESTAMP(6)
    ISDISPLAYED     NUMBER(1,0)
    UPDATEDBY     VARCHAR2(20)
    AVERAGERATING     NUMBER
    VOTES          NUMBERAny help realyy appreciated.
    Thanks in advance
    Edited by: user512743 on Dec 10, 2008 10:46 PM

    Hello,
    Specifically, this should be asked in the
    ASP.Net MVC forum on forums.asp.net.
    Karl
    When you see answers and helpful posts, please click Vote As Helpful, Propose As Answer, and/or Mark As Answer.
    My Blog: Unlock PowerShell
    My Book: Windows PowerShell 2.0 Bible
    My E-mail: -join ('6F6C646B61726C40686F746D61696C2E636F6D'-split'(?<=\G.{2})'|%{if($_){[char][int]"0x$_"}})

  • How to select value from database view with * in wher clause

    Hi ,
      I ahve a database view with some fields.
    Now my requirement is to serach a single row on the basis of process type.
    Process type can have values like ZBA,ZBC,ZBD,ZBE or similarly anything starting with ZB.
    Now i know that starting two letters will be ZB , but dont knwo the last letter.
    So how should i use select query for the same?
    Should i use like operator for the same?
    regards
    PG

    hi,
    u can use character '%'.sample code like this
    SELECT reltype
                 instid_a
                 catid_a
                 instid_b
                 FROM /dbm/ord_docflow
                 INTO TABLE it_link
                 FOR ALL ENTRIES IN it_pnwtyh
                 WHERE  instid_a  =  it_pnwtyh-instid_a AND
                 instid_b  LIKE 'QMSM%'  AND
                 typeid_a  = 'BUS2400'  AND
                 typeid_b  = 'BUS2400' AND
                 catid_a   = 'BO' AND
                 catid_b   = 'BO' AND
                 reltype   = 'VONA'.
    this is similar to using* while we fetch values from table.in the above code only i no QMSM rest values not sure,so used QMSM%

  • Improving Performance of Group By clause

    I'm a developer who needs to create an aggreagate, or roll-up, of a large table (tens of millions of rows) using a group by clause. One of the several items I am grouping by is a numeric column called YEAR. My DBA recommended I create an index on YEAR to improve the performance of the group by clause. I read that indexes only are used when referenced in the where clause, which I do not need. Will my DBA's reccomendation help? Can you recommend a technique? Thank you.

    When you select millions of rows, grouped or not, the database has to fetch
    each of them, so an index on the group column isn't useful.
    If you have a performance problem that cannot be solved through an index on
    columns used in your where-clause, perhaps a materialzed view with the
    dimension(s) of your group clause will help.

  • HR transactions and functionality and creation of Database view

    Hi Guys
        I am new to HR module . can anybody send me the list of transaction codes with HR functionalities.
    I also have to made a database view from hrp1001 and hrp1010 table to list all chief positions who are head of business group , Business unit , Site etc .
    Could you help me in creating this databse view.

    Hi,
    These are HR Infotypes
    0000    Events
    0001    Org assignment
    0002    Personal info
    0003    Payroll data
    0007    Work time
    0008    Basic pay
    0014    Reoccurring pay
    0015   1 X pay
    0027   Cost Center
    0041   Event Dates
    0057   Membership dues
    0165   Over ride  to limits on deductions
    0167   Health
    0168   Insurance
    0169   Savings
    0170   Spending   
    0194   Garnishment reduction
    0195   Garnishment order
    0207   Residence Tax
    0208   Work Tax
    0209   Unemployment Tax
    0210   Withholding
    0216   Garnishment adjustment
    0221   Adjustment
    0267   Off cycle
    2005   OT
    2010   Catts direct to cluster
    1000   Infotypes 1000 – 1999 are PD Relationship infotypes
    Logical Database (PNP) ·     
    Programming with Infotypes ·     
    Processing Time infotypes/Cluster ·     
    Processing Payroll infotypes/Cluster
    In OM level hrp 1001 and 1010 will be there.
    If u want total transactions goto SE11->TSTC Table.
    ****POINTS TO BE CONSIDERD
    Thanks
    P.SRIKANTH

  • -- SQL -- GROUP BY clause: non-aggregate fields mandate

    Hello,
    I was studying Databases, (particularly the retrieval of the data), and found something interesting.
    While using an Aggregate Function in the SELECT clause, it is mandatory to have all the non-aggregate fields in the SELECT clause to be there in the GROUP BY clause.
    For example,
    SELECT dept_no, SUM(salary)
    FROM employee
    GROUP BY dept_no;
    The above SQL works fine.
    But, what if the user misses the dept_no in the GROUP BY clause or he/she misses the GROUP BY clause itself?
    Certainly, it is an error.
    Why is this error not handled by the database. I mean, the database should be smart/intelligent enough to add the GROUP BY clause by itself. So suppose, if I miss out the GROUP BY clause or miss a non-aggregate field from the SELECT clause when I am having at least one aggregate function on a field with at least one non-aggregated field in the SELECT clause, the database should check the GROUP BY clause at time of compilation and add the mandate missed out fields in the GROUP BY clause.
    Example,
    SQL1:_
    SELECT dept_no, SUM(salary)
    FROM employee
    GROUP BY dept_no;
    SQL2:_
    SELECT dept_no, SUM(salary)
    FROM employee;
    Here, the SQL1 and SQL2, both should give me same outputs without an error.
    I am unable to understand why is this not handled?

    Hi,
    998478 wrote:
    ... If we mix aggregate and non-aggregate values then there must be a GROUP BY clause containing all the non-aggregate values. Why is this not handled by the database/compiler itself? It IS handled by the compiler itself. The compiler handles it by raising an error. The compiler has no way of knowing whether you want to remove something from the SELECT clause, or to add something to the GROUP BY clause, or not to use aggregate functions, or to use more aggregate functions, or some combination of the above. If the compiler re-wrote your code, and did any of these things automatically, it would be wrong more often than it was right, and you would (rightly) be complaining about its behavior.
    For example, this is clearly wrong:
    SELECT    deptno
    ,       job
    ,       SUM (sal)
    FROM       scott.emp
    GROUP BY  deptno
    ;What is the right way to fix it?
    <h3>1. Remove something from the SELECT clause</h3>
    SELECT    deptno
    ,       SUM (sal)
    FROM       scott.emp
    GROUP BY  deptno
    ;<h3>2. Add something to the GROUP BY clause</h3>
    SELECT    deptno
    ,       job
    ,       SUM (sal)
    FROM       scott.emp
    GROUP BY  deptno
    ,         job
    ;<h3>3. Not use aggregate functions</h3>
    SELECT    deptno
    ,       job
    ,       sal
    FROM       scott.emp
    ;<h3>4. Use more aggregate functions</h3>
    SELECT    deptno
    ,       MIN (job)
    ,       SUM (sal)
    FROM       scott.emp
    GROUP BY  deptno
    ;These aren't all the options, either. For example, the correct fix might be to use analytic functions instead of aggregate functions.
    How can anybody say which of these is right? All of them are the right answer for some problem.
    By the way, saying that everying in the SELECT clause must be an aggregate or in the GROUP BY clause is a bit over-simplified.
    More completely, here are the ABC's of GROUP BY:
    When you use a GROUP BY clause and/or an aggregate function, then everything in the SELECT clause must be:
    (A) an <b>A</b>ggregate function,
    (B) one of the "group <b>B</b>y" expressions,
    (C) a <b>C</b>onstant, or
    (D) something that <b>D</b>epends entirely on the above. (For example, if you "GROUP BY TRUNC(dt)", you can SELECT "TO_CHAR (TRUNC(dt), 'Mon-DD')").
    Edited by: Frank Kulash on Apr 13, 2013 1:44 PM
    Added code examples.

  • Query Rewrite with regular database Views

    Hi all,
    I'm trying to make my programmer's life easier by creating a database view for them to query the data, so they don't have to worry about joining tables. However, query rewrite doesn't work no matter how I define the MV and View. Here's an example:
    I've Sales tables with columns: PDate, CustCode, Amount
    and Customer table with columns: CustCode, CustDesc
    I create a view SALES_V with columns: PDate, CustCode, CustDesc, Amount by joining Sales table with Customer table as follows:
    create or replace view SALES_V as
    select PDate, c.CustCode, c.CustDesc, Amount
    from Sales s
    join Customer c on (s.CustCode=c.CustCode);
    For the sake of speed, I create a materialized view SALES_TOT_MV with columns: PDate, Amount with the following SQL:
    create materialized view SALES_TOT_MV
    enable query rewrite
    as select PDate, sum(Amount) Amount from Sales
    group by PDate;
    When I run the following query, I expect it to be rewritten to make use of SALES_TOT_MV:
    select PDate, sum(Amount) from SALES_V
    group by PDate;
    However, explain plan always tell me it's using SALES table, not the SALES_TOT_MV.
    Can somebody tell me it's a limitation of Oracle optimizer or I'm just missing something for this?
    Thanks in advance!!
    - Andrew
    Edited by: blackhole001 on Jan 28, 2010 12:34 PM

    blackhole001 wrote:
    Hi all,
    I'm trying to make my programmer's life easier by creating a database view for them to query the data, so they don't have to worry about joining tables. This sounds like a pretty horrible idea. I say this because you will eventually end up with programmers that know nothing about your data model and how to properly interact with it.
    Additionally, what you will get is a developer that takes one of your views and see's that of the 20 columns in it, it has 4 that he needs. If all those 4 columns comes from a simple 2 table join, but the view has 8 tables, you're wasting a tonne of resources by using the view (and heaven forbid they have to join that view to another view to get 4 of the 20 columns from that other view as well).
    Ideally you'd write stored routines that satisfy exactly what is required (if you are the database resource and these other programmers are java, .net, etc... based) and the front end developers would call those routines customized for an exact purpose.
    Creating views is not bad, but it's by no means a proper solution to having developers not learn or understand SQL and/or the data model.

  • Analytic Functions with GROUP-BY Clause?

    I'm just getting acquainted with analytical functions. I like them. I'm having a problem, though. I want to sum up the results, but either I'm running into a limitation or I'm writing the SQL wrong. Any hints for me?
    Hypothetical Table SALES consisting of a DAY_ID, PRODUCT_ID, PURCHASER_ID, PURCHASE_PRICE lists all the
    Hypothetical Business Question: Product prices can fluctuate over the course of a day. I want to know how much per day I would have made had I sold one each of all my products at their max price for that day. Silly question, I know, but it's the best I could come up with to show the problem.
    INSERT INTO SALES VALUES(1,1,1,1.0);
    INSERT INTO SALES VALUES(1,1,1,2.0);
    INSERT INTO SALES VALUES(1,2,1,3.0);
    INSERT INTO SALES VALUES(1,2,1,4.0);
    INSERT INTO SALES VALUES(2,1,1,5.0);
    INSERT INTO SALES VALUES(2,1,1,6.0);
    INSERT INTO SALES VALUES(2,2,1,7.0);
    INSERT INTO SALES VALUES(2,2,1,8.0);
    COMMIT;
    Day 1: Iif I had sold one product 1 at $2 and one product 2 at $4, I would have made 6$.
    Day 2: Iif I had sold one product 1 at $6 and one product 2 at $8, I would have made 14$.
    The desired result set is:
    DAY_ID                 MY_MEASURE
    1                        6
    1                       14The following SQL gets me tantalizingly close:
    SELECT DAY_ID,
      MAX(PURCHASE_PRICE)
      KEEP(DENSE_RANK FIRST ORDER BY PURCHASE_PRICE DESC)
      OVER(PARTITION BY DAY_ID, PRODUCT_ID) AS MY_MEASURE
      FROM SALES
    ORDER BY DAY_ID
    DAY_ID                 MY_MEASURE
    1                      2
    1                      2
    1                      4
    1                      4
    2                      6
    2                      6
    2                      8
    2                      8But as you can see, my result set is "longer" than I wanted it to be. I want a single row per DAY_ID. I understand what the analytical functions are doing here, and I acknowledge that I am "not doing it right." I just can't seem to figure out how to make it work.
    Trying to do a sum() of max() simply does not work, nor does any semblance of a group-by clause that I can come up with. Unfortunately, as soon as I add the windowing function, I am no longer allowed to use group-by expressions (I think).
    I am using a reporting tool, so unfortunately using things like inline views are not an option. I need to be able to define "MY_MEASURE" as something the query tool can apply the SUM() function to in its generated SQL.
    (Note: The actual problem is slightly less easy to conceptualize, but solving this conundrum will take me much closer to solving the other.)
    I humbly solicit your collective wisdom, oh forum.

    Thanks, SY. I went that way originally too. Unfortunately that's no different from what I could get without the RANK function.
    SELECT  DAY_ID,
            PRODUCT_ID,
            MAX(PURCHASE_PRICE) MAX_PRICE
      FROM  SALES
      GROUP BY DAY_ID,
               PRODUCT_ID
      ORDER BY DAY_ID,
               PRODUCT_ID
    DAY_ID                 PRODUCT_ID             MAX_PRICE             
    1                      1                      2                     
    1                      2                      4                     
    2                      1                      6                     
    2                      2                      8

  • Alternate to group by clause

    Hi
    I am using Oracle 8i and one of my queries is getting the data from multiple sources through a dblink from other databases (customer need cannot avoid dblinks).
    The query is using the group by clause to get the count based on specific columns and when i am not using the group by the query is pretty fast.
    select count(id), col1,col2,col3
    from source1@dblink1,
    source2@dblink1,
    source3@dblink1,
    source4@dblink1,
    source5@dblink1
    where
    group by col1,col2,col3
    Please suggest the use of other Oracle features that we can use (pl/sql table or looping in pl/sql instead of using group by ) as the amount of data is large and it has to go through several iterations and accordingly update my local db with the counts returned.
    An example will be quite helpful.
    Thanks in Advance.

    Hi,
    i have a procedure that has a cursor thats using db links as above, in the code if i open the cursor and say NULL even then it is taking about 8 mins while the cursor query is taking about 2 mins only when run separately.
    i open the cursor
    for c1 in cursor loop
    NULL;
    end loop;
    it hangs on for about 8 mins , while if i run the query of the cursor with the same parameters that i pass in the cursor at run time it takes about 2 mins.
    Dont understand where the 6 mins are going.
    If the cursor is not opened at all in the proc it gets executed in less than a second so there is no other code thats consuming time.
    Please suggest.

  • Right GROUP BY clause

    I wrote the view in the Original View section below. I query it with the following SELECT statement:
    SELECT *
    FROM ZZBUD_DEBT_SERVICE_V
    WHERE action_month IN ('01','02')
    AND action_year IN ('2011')
    AND short_name IN( 'WHIT-18','2REDFERN')
    ORDER BY action_year,action_month, group1, group2, short_name;
    An get these results:
    SHORT GROUP1 GROUP2 ACTION ACTION CURR CURR CURR
    NAME YEAR MONTH INT INT PRIN
    ACCRUED PAYMENT PAYMENT
    2REDFERN     05     02 2011 01     149.39     0     0     
    2REDFERN     05     02 2011 02     0     896.32 49795.84     
    WHIT-18     05     01 2011 01     442.86     0     0     
    WHIT-18     05     01 2011 02 442.86 2657.14 0     
    I need to compute the year-to-date CURR_INT_ACCRUED, CURR_INT_PAYMENT, CURR_PRIN_PAYMENT (SUM(CURR_INT_ACCRUED), etc.) by short_name and action_year but I can't tweek my GROUP BY statement properly. What would the GROUP BY expression look like to get the sum of these columns?
    Original View:
    CREATE OR REPLACE VIEW ZZBUD_DEBT_SERVICE_V(short_name, group1, group2, action_year, action_month
    , curr_int_acrrued, curr_int_payment, curr_prin_payment)
    AS
    SELECT short_name, group1, group2, action_year, action_month
    , SUM(curr_int_acrrued) curr_int_acrrued
    , SUM(curr_int_payment) curr_int_payment
    , SUM(curr_prin_payment) curr_prin_payment
    FROM (
    SELECT dsa.short_name
    , dsa.group1
    , dsa.group2
    , dsa.action_year
    , dsa.action_month
    , DECODE(SIGN(dsa.int_accrued),-1,0 ,0,0,dsa.int_accrued) curr_int_acrrued
    , DECODE(SIGN(dsa.ytd_accrued),1,0,0,0,ABS(dsa.ytd_accrued)) curr_int_payment
    , DECODE(SIGN(dsa.bond_outstand),1,0,0,0,ABS(dsa.bond_outstand)) curr_prin_payment
    FROM NYC.ZZBUD_DEBT_SERVICE_MASTER DSA
    WHERE dsa.bond_status = 'A'
    AND dsa.group1 IN ('03', '05', '06', '07')
    ORDER BY dsa.group1, dsa.group2, DSA.short_name, dsa.action_year, dsa.action_month
    GROUP BY short_name,group1,group2,action_year,action_month

    It depends on how you look at it. When I issue the command:
    SELECT group1, group2, short_name,SUM(curr_int_acrrued) ytd_accrued_int
    FROM ZZBUD_DEBT_SERVICE_V
    WHERE action_month IN ('01','02')
    AND action_year IN ('2011')
    AND short_name IN ('BXRIVER','WILSON','WHIT-18','2REDFERN')
    GROUP BY action_year,action_month,group1, group2, short_name
    ORDER BY action_year,action_month, group1, group2, short_name
    I'm not getting the results I want as 'WHIT-18' shows detail for both action_months and I want the SUM of 01 & 02 and the same for the other short_names. When I drop columns from the GROUP BY clause I get error ORA-0979 'Not a GROUP BY expression'.

  • Removing an attribute from a group by clause

    This concerns a web application developed by JDeveloper 10.1.2 (adf, uix). I have viewobject with a query, written in expert mode, that has a group by clause. I removed an attribute from the select statement that was included in the group by clause and removed it from the bindings of the page that displays the data from the view. When I test the application module the data is grouped correctly. But when I run the view the attribute is not shown on the page but the data is still grouped by the attribute. The same query works fine in SQL plus, it looks like JDeveloper 'remembers' the old group by clause somewhere. I checked the xml file of the view object and it looks ok. Where can the old group by clause be written? How comes it is still applied?

    joni1129 wrote:
    I am trying to place a shape on an image and remove it from the photo, creating just the picture within the shape.  When I follow instructions, I get a lovely shape around my image, but the image isn't removed.  It just sits in a white-checkered background.
    How do I get rid of the white background?
    The white-checkered background is a visual reference for transparency. (It won't print.) To keep the transparency, you need to save in a format that supports transparency, PSD, TIF (with save transparency checked), PNG, or GiF. If you save in a format such as JPG that doesn't support transparency, the transparency is replaced by white.
    Edit: If you are transferring the image into another software application, you may have to save the selection along with the image. Select<Save Selection. You'd have to also check which of the formats that application accepts.

  • Output not sorted when using group by clause

    Recently we migrated our application from Forms5 to Forms10g and database from 7.3.2 to 10gR2.
    More than 400 applications using sql stmt with "group by" clause and without "order by".
    In Earlier version of oracle 7.3.2 "group by" clause in SQL stmt sorts the output by order of the column in the "group by"
    stmt, where new version of oracle 10g (10.2.0.1.0 - 64bit Production) doesnt behave like older version.
    eg select div_cd,dept_cd,count(*) from class group by div_cd,dept_cd
    Output of the above query in Oracle7
    DIV_CD DEPT_CD COUNT(*)
    0 1     120
    0 9 131
    1 4 938
    1 6 1
    4 1 1490
    5 2 59
    6 6 848
    6 9 295
    8 1 45
    9 5 19
    Output of same query in Oracle10g
    DIV_CD DEPT_CD COUNT(*)
    0 6 120
    0 9 131
    4 1 1490
    6 9 295
    8 1 45
    9 5 19
    1 4 938
    1 6 1
    5 2 59
    6 6 848
    My question is
    a) Does the oracle behave in that manner?.
    b) if so, Any patch set available for this?
    c) if no patch set how to resolve this issue.
    if i use "order by" clause
    along with group by then i can get the output how i want.
    BUt problem is more than 400 applications (forms) using above method.
    Identify Forms/Reports and rectifing is a tedious job.
    Anyone can give me a good solution to solve the above issue

    Group By does not, and never has guaranteed order. Just because it happened that way on the past does not mean it will continue to do so.
    In the past, the algorithm used for Group By invoked a sort. It was convenient for the system to simply dump the output in that resulting order. These days, Group By could also be accomplished using a hash algorithm. Walking through the hash can easily result in a completely different ordering, as you have seen.
    Your problem is that the assumptions made previously are now invalid. Oracle is still working correctly. No patch, but you might be able to work around it by crippling your database and using the COMPATIBLE parameter (although the official lower limit is 9.2.0)

  • Can i use an analytic function instead of a group by clause?

    Can i use an analytic function instead of a group by clause? Will this help in any performance improvement?

    analytic can sometimes avoid scanning the table more than once :
    SQL> select ename,  sal, (select sum(sal) from emp where deptno=e.deptno) sum from emp e;
    ENAME             SAL        SUM
    SMITH             800      10875
    ALLEN            1600       9400
    WARD             1250       9400
    JONES            2975      10875
    MARTIN           1250       9400
    BLAKE            2850       9400
    CLARK            2450       8750
    SCOTT            3000      10875
    KING             5000       8750
    TURNER           1500       9400
    ADAMS            1100      10875
    JAMES             950       9400
    FORD             3000      10875
    MILLER           1300       8750
    14 rows selected.
    Execution Plan
    Plan hash value: 3189885365
    | Id  | Operation          | Name | Rows  | Bytes | Cost (%CPU)| Time     |
    |   0 | SELECT STATEMENT   |      |    14 |   182 |     3   (0)| 00:00:01 |
    |   1 |  SORT AGGREGATE    |      |     1 |     7 |            |          |
    |*  2 |   TABLE ACCESS FULL| EMP  |     5 |    35 |     3   (0)| 00:00:01 |
    |   3 |  TABLE ACCESS FULL | EMP  |    14 |   182 |     3   (0)| 00:00:01 |
    Predicate Information (identified by operation id):
       2 - filter("DEPTNO"=:B1)which could be rewritten as
    SQL> select ename, sal, sum(sal) over (partition by deptno) sum from emp e;
    ENAME             SAL        SUM
    CLARK            2450       8750
    KING             5000       8750
    MILLER           1300       8750
    JONES            2975      10875
    FORD             3000      10875
    ADAMS            1100      10875
    SMITH             800      10875
    SCOTT            3000      10875
    WARD             1250       9400
    TURNER           1500       9400
    ALLEN            1600       9400
    JAMES             950       9400
    BLAKE            2850       9400
    MARTIN           1250       9400
    14 rows selected.
    Execution Plan
    Plan hash value: 1776581816
    | Id  | Operation          | Name | Rows  | Bytes | Cost (%CPU)| Time     |
    |   0 | SELECT STATEMENT   |      |    14 |   182 |     4  (25)| 00:00:01 |
    |   1 |  WINDOW SORT       |      |    14 |   182 |     4  (25)| 00:00:01 |
    |   2 |   TABLE ACCESS FULL| EMP  |    14 |   182 |     3   (0)| 00:00:01 |
    ---------------------------------------------------------------------------well, there is no group by and no visible performance enhancement in my example, but Oracle7, you must have written the query as :
    SQL> select ename, sal, sum from emp e,(select deptno,sum(sal) sum from emp group by deptno) s where e.deptno=s.deptno;
    ENAME             SAL        SUM
    SMITH             800      10875
    ALLEN            1600       9400
    WARD             1250       9400
    JONES            2975      10875
    MARTIN           1250       9400
    BLAKE            2850       9400
    CLARK            2450       8750
    SCOTT            3000      10875
    KING             5000       8750
    TURNER           1500       9400
    ADAMS            1100      10875
    JAMES             950       9400
    FORD             3000      10875
    MILLER           1300       8750
    14 rows selected.
    Execution Plan
    Plan hash value: 2661063502
    | Id  | Operation            | Name | Rows  | Bytes | Cost (%CPU)| Time     |
    |   0 | SELECT STATEMENT     |      |    14 |   546 |     8  (25)| 00:00:01 |
    |*  1 |  HASH JOIN           |      |    14 |   546 |     8  (25)| 00:00:01 |
    |   2 |   VIEW               |      |     3 |    78 |     4  (25)| 00:00:01 |
    |   3 |    HASH GROUP BY     |      |     3 |    21 |     4  (25)| 00:00:01 |
    |   4 |     TABLE ACCESS FULL| EMP  |    14 |    98 |     3   (0)| 00:00:01 |
    |   5 |   TABLE ACCESS FULL  | EMP  |    14 |   182 |     3   (0)| 00:00:01 |
    -----------------------------------------------------------------------------So maybe it helps

  • GROUP BY clause diff in Sybase and Oracle

    Hi,
    I am migrating code from Sybase to Oracle and came across a strange issue which is widely known to others :)
    In Sybase , one can use columns or calculations in the SELECT expressions that don't appear
    in the GROUP BY clause of the query. Like
    SELECT order.custid,customer.name,MAX(payments)
    -> FROM order, customer
    -> WHERE order.custid = customer.custid
    -> GROUP BY order.custid;
    works fine but for SQL we need to add customer.name to GROUP BY clause.
    On the similar lines, here is my SQL query
    INSERT INTO GTT_CTRT_recalc_proc
    (id_fin_rec,
    id_imnt,
    id_instr_opn,
    dt_instr_opn,
    dt_opn,
    dt_cls_loanet,
    dt_prcss,
    am_invst)
    SELECT t1.id_fin_rec,
    t1.id_imnt,
    t1.id_instr_opn,
    t1.dt_instr_opn,
    t1.dt_opn,
    NVL(t1.dt_cls_loanet, l_dt_to),
    t1.dt_prcss,
    SUM(t2.am_invst) + (0.123 * (1 - abs(sign(0 - SUM(t2.am_invst)))))
    FROM GTT_CTRT_TEMP_recalc_process t1, GTT_CTRT_TEMP_recalc_process t2
    WHERE t2.id <= t1.id
    AND t2.id_imnt = t1.id_imnt
    AND t2.id_instr_opn = t1.id_instr_opn
    AND t2.dt_instr_opn = t1.dt_instr_opn
    GROUP BY t1.id_imnt,
    t1.id_instr_opn,
    t1.dt_instr_opn,
    t1.dt_opn,
    t1.dt_cls_loanet,
    t1.dt_prcss;
    Which does not have t1.id_fin_rec in GROUP BY failing it in SQL.
    I know that if I add this column in GROUP BY it will work fine but I am told to keep the functionality as it is as the result before and after adding the column would be diff of-course.
    Please guide me about what can be done in this situation and is there any work around for this?
    Thanks,
    Aashish

    That's a piece of nasty denormalisation. We shoudl also expect trouble we do stuff like that. Anyway, I think encapsulating the stuff from T2 into an inline view should sort you out....
    INSERT INTO GTT_CTRT_recalc_proc
    (id_fin_rec,
    id_imnt,
    id_instr_opn,
    dt_instr_opn,
    dt_opn,
    dt_cls_loanet,
    dt_prcss,
    am_invst)
    SELECT t1.id_fin_rec,
    t1.id_imnt,
    t1.id_instr_opn,
    t1.dt_instr_opn,
    t1.dt_opn,
    NVL(t1.dt_cls_loanet, l_dt_to),
    t1.dt_prcss,
    , t2.total
    FROM GTT_CTRT_TEMP_recalc_process t1
        , ( select id_imnt,
                    id_instr_opn,
                    dt_instr_opn,
                    SUM(am_invst) + (0.123 * (1 - abs(sign(0 - SUM(am_invst))))) AS total
            FROM GTT_CTRT_TEMP_recalc_process
           GROUP BY  id_imnt,
                           id_instr_opn,
                           dt_instr_opn ) t2
    WHERE t2.id <= t1.id
    AND t2.id_imnt = t1.id_imnt
    AND t2.id_instr_opn = t1.id_instr_opn
    AND t2.dt_instr_opn = t1.dt_instr_opn
    GROUP BY t1.id_imnt,
    t1.id_instr_opn,
    t1.dt_instr_opn,
    t1.dt_opn,
    t1.dt_cls_loanet,
    t1.dt_prcss;Cheers, APC
    blog: http://radiofreetooting.blogspot.com
    Edited by: APC on Mar 16, 2009 2:31 PM
    Forgot to include GROUP BY in the inline view

Maybe you are looking for