Adding columns in Group by clause

Hi ,
My query is like that ,
Select empid , sum(sal) from emp group by empid ,
I want additional columns to be listed in the query
select empid , address , sum(sal) from group by empid
Is there any possible way to include other columns apart from group by column .
Thanks

user5003725 wrote:
Hi ,
My query is like that ,
Select empid , sum(sal) from emp group by empid ,
I want additional columns to be listed in the query
select empid , address , sum(sal) from group by empid
Is there any possible way to include other columns apart from group by column .
ThanksYou asking this question makes mee feel you cant visualize how group by works. Based on your query see the example below.
You have a table emp with values like this
empid     address     sal
1         A           10
1         B           20
1         C           30now when i query like this
select empid, sum(sal) from emp group by empidwhat i will get?
empid     sal
1         60So now i add address to the select list
select empid, sum(sal), address from emp group by empidNow you say which address should be taken. Its A, B or C?
If you can answer this question we can tell how you can achieve it.

Similar Messages

  • Select not working with Case unless adding field to group by clause

    Hi, I have a simple query that I am trying to run.  The first version runs, but I am trying to get the second version to run, because I do not want to have to include PAYRCORD in the Group By.  Since I do not want it grouped by PAYRCORD.
    Is there a way to write the second version to work?
    ---- First Version ---
    SELECT     TOP (100) PERCENT YEAR1, EMPLOYID, PAYRCORD,
    CASE WHEN (dbo.tw80507.PAYRCORD LIKE '%OT%' AND NOT (dbo.tw80507.PAYRCORD LIKE 'OTHER'))
                          THEN SUM(DISTINCT dbo.tw80507.TwOriginalTaxableWages) ELSE NULL END AS OT,
    CASE WHEN dbo.tw80507.PAYRCORD NOT LIKE '%OT%'
                          THEN SUM(DISTINCT dbo.tw80507.TwOriginalTaxableWages) ELSE NULL END AS REG
    FROM         dbo.TW80507
    GROUP BY YEAR1, EMPLOYID, PYRLRTYP, PAYRCORD
    HAVING      (YEAR1 > 2013) AND (PYRLRTYP = 3)
    ORDER BY YEAR1
    --- Second Version ---
    SELECT     TOP (100) PERCENT YEAR1, EMPLOYID,
    CASE WHEN (dbo.tw80507.PAYRCORD LIKE '%OT%' AND NOT (dbo.tw80507.PAYRCORD LIKE 'OTHER'))
                          THEN SUM(DISTINCT dbo.tw80507.TwOriginalTaxableWages) ELSE NULL END AS OT,
    CASE WHEN dbo.tw80507.PAYRCORD NOT LIKE '%OT%'
                          THEN SUM(DISTINCT dbo.tw80507.TwOriginalTaxableWages) ELSE NULL END AS REG
    FROM         dbo.TW80507
    GROUP BY YEAR1, EMPLOYID, PYRLRTYP
    HAVING      (YEAR1 > 2013) AND (PYRLRTYP = 3)
    ORDER BY YEAR1

    Put the CASE inside the SUM.
    I also cleaned up your query by removing an excess of prefixes, making the code difficult to read, and I replaced HAVING with WHERE. (You use HAVING WHEN you need to filter on an aggregate value, for instance HAVING COUNT(*) > 2.) Finally, I removed the
    DISTINCT from the SUM, because that looked, well, funny.
    SELECT  YEAR1, EMPLOYID,
             SUM(CASE WHEN PAYRCORD LIKE '%OT%' AND NOT (PAYRCORD LIKE 'OTHER')
                      THEN TwOriginalTaxableWages)
                      ELSE 0
                 END) AS OT,
            SUM(CASE WHEN PAYRCORD NOT LIKE '%OT%'
                     THEN TwOriginalTaxableWages
                     ELSE 0
                END AS) REG
    FROM    dbo.TW80507
    WHERE   YEAR1 > 2013
       AND  PYRLRTYP = 3
    GROUP BY YEAR1, EMPLOYID
    ORDER BY YEAR1
    Erland Sommarskog, SQL Server MVP, [email protected]

  • More than 1 column in the group by clause

    DB Version:10gR2
    I understand the basics of GROUP BY clause. I have a question on why we have on more than 1 columns in GROUP BY clause.
    In the below example, the course name by itself does not make up a group. A Course name plus its BeginDate make up a group. This is the whole point of having more than 1 columns in the GROUP by clause. Right?
    SQL> select r.course, r.begindate , count(r.attendee) as attendees
      3   from registrations r
      4   group by r.course, r.begindate
      5   order by course
      6  /
    COURSE BEGINDATE      ATTENDEES
    JAVA    12/13/1999           5
    JAVA    2/1/2000             3
    OAU     8/10/1999            3
    OAU     9/27/2000            1
    PLS     9/11/2000            3
    SQL     4/12/1999            4
    SQL     10/4/1999            3
    SQL     12/13/1999           2
    XML     2/3/2000             2

    ExpansiveMind wrote:
    Thanks Dmorgan. I am just learning the basics of GROUP BY clause. I have noticed that all Non-aggregate columns in SELECT list have to be present in the GROUP BY clause. I thought it was a "syntactical requirement". Now, i realise that these columns are present in the GROUP BY clause because only a combination of columns make up a group.Well, it is a bit of both actually. It is a syntactic requirement that all non-aggregated columns in the select list must appear in the group by clause. However, the non-aggregated columns in the select list is what defines your group. Your two examples define two different groups, and would answer two different questions.
    John

  • Reg - Search Form for a VO with group by clause

    Hi,
    I have a Bar graph that displays data based on the Query below.
    SELECT STATUS.STATUS_ID AS STATUSID,STATUS.STATUS,COUNT(SR.SERVICEREQUEST_ID) AS SRCOUNT
    FROM SERVICE_REQUEST SR ,SERVICEREQUESTSTATUS STATUS
    WHERE SR.STATUS_ID = STATUS.STATUS_ID
    GROUP BY STATUS.STATUS_ID,STATUS.STATUS,SR.STATUS_ID
    It displays the count of SRs against a particular status.
    Now I need to add a search form to this graph with customer and date range.
    So we need to add the line below to the where clause.
    "SR.CUSTOMER_ID = :customerId AND SR.REQUESTED_ON BETWEEN :fromDate and :toDate"
    But the columns SR.CUSTOMER_ID, SR.REQUESTED_ON also need to be added to the select clause to create the View criteria for search panel.
    The two columns should also need to be added to the group by clause if we are to add them in the select clause.
    This would not produce the expected results.
    How do I create a search form with the criterias applied only at the where clause.Please help.
    With Regards,
    Guna

    The [url http://docs.oracle.com/cd/E16162_01/apirefs.1112/e17483/oracle/jbo/server/ViewObjectImpl.html]ViewObjectImpl has methods for doing this programmatically (setQuery, defineNamedWhereClauseParam, setNamedWhereClauseParam) that you can use to manipulate the query, the bind variables expected, and the values for the binds.
    John

  • Group by clause will try to group the primay key's

    hi all,
    For my requiremments and to avoid the the subquery i am using join and group by clause in my sql query in oracle 10g r2 database.my question is if i put a primary column the group clause then oracle try to group the records?.
    i know if we use a primary key column in group by clause there is no grouping will occur.
    but i have doubt oracle will try to group ? because it will take some amount of time to achive?
    else due presense of primay key column in group by clause oracle won't try?
    Please advice ??

    My query return the records less then the min v_date(date datatype) column.
    i check this condition in having clause
    my old query is
    select emp_id,emp_name,voldate,volume,productivity
    from volume v1
    where v1.voldate<=(select min(v2.voldate) form volume v2 where v2.emp_id=1)
    and v1.emp_id=1
    i change it as
    select emp_id,emp_name,voldate,volume,productivity
    from volume v1,volume v2
    where v1.emp_id=v2.emp_id
    having v1.voldate<=min(v2. voldate)
    group by emp_id,emp_name,voldate,volume,productivity
    above the query's emp_id is primay key for volume table
    comparing both query's which one is the best while looking th response time.
    else any other alternative for both the queries.??

  • The order of the columns in GROUP BY doesn`t matter?  why?

    I did a little test and find out that the order of the columns in GROUP BY does not matter to ORACLE.
    Why? How does oracle do GROUP BY exactly?
    Could anyone show me its processing logic? maybe in pseudo-code?
    Thanks in advance.

    I mean,
    I thought oracle would group data based on the order of the columns in the GROUP BY LIST.
    For example: for select deptno, job, count(*) from emp group by deptno, job;
    I thought, oracle first would group the data based on deptno (10,20,30), e.g. in deptno=30, we got 6 employees.
    and then, group this 6 employees based on JOB (CLERK, SALESMAN, MANAGER).and
    for select job, deptno, count(*) from emp group by job, deptno;
    first would group the data based on JOB (CLERK, SALESMAN, MANAGER) e.g., for CLERK, we got 4 employees,
    and then, group this 4 employees based on deptno (10,20, 30).and to me, it seems that in some circumstances, their result could be different.
    So, I wonder if the order of the columns in GROUP BY Clause matter?

  • Conditional GROUP BY clause

    Hi,
    I have four columns in group by clause. I want to add conditional group by clause.
    Case 1 : If one of the  column's value is null, I don't want to include it in group by clause. Means, Now GROUP BY clause will have only 3 columns.
    Case 2 : If not null, then GROUP BY clause with all four columns.
    Please help me out on this.
    Thanks in advance.  

    Hi
    I think it won't matter, all group functions by default ignore NULLs so your result won't differ.
    select  dept, loc, sum(sal)
    from (
    select 'A' emp , 1 dept , 'P' loc , 100 sal from dual union all
    select'B',1,'P',200 from dual union all
    select'C',2,'P',300 from dual union all
    select'D',2,'P',400 from dual union all
    select'E',3, 'P',500 from dual union all
    select'F',3, 'P',600 from dual union all
    select'G',4, 'Q',700 from dual union all
    select'H', null,'Q' , 1000 from dual union all
    select'I',null ,'Q', 2000 from dual union all
    select 'J' ,null, 'Q',300 from dual)
    group by dept,loc;
    Output
    DEPT      LOC      SUM(SAL)
    1                P      300
    2                P      700
    3                P      1100
                    Q      3300
    4                Q      700
    Now by doing grouping only for NOT NULL values,
    select dept,loc, sum(sal)
    from (
    select 'A' emp , 1 dept , 'P' loc , 100 sal from dual union all
    select'B',1,'P',200 from dual union all
    select'C',2,'P',300 from dual union all
    select'D',2,'P',400 from dual union all
    select'E',3, 'P',500 from dual union all
    select'F',3, 'P',600 from dual union all
    select'G',4, 'Q',700 from dual union all
    select'H', null,'Q' , 1000 from dual union all
    select'I',null ,'Q', 2000 from dual union all
    select 'J' ,null, 'Q',300 from dual)
    where dept is not null          --------------NOT NULL Condition
    group by dept, loc;
    Output
    DEPT     LOC      SUM(SAL)
    1           P           300
    2           P           700
    3           P           1100
    4           Q           700
    Now by doing grouping only for NULL values,
    select dept,loc, sum(sal)
    from (
    select 'A' emp , 1 dept , 'P' loc , 100 sal from dual union all
    select'B',1,'P',200 from dual union all
    select'C',2,'P',300 from dual union all
    select'D',2,'P',400 from dual union all
    select'E',3, 'P',500 from dual union all
    select'F',3, 'P',600 from dual union all
    select'G',4, 'Q',700 from dual union all
    select'H', null,'Q' , 1000 from dual union all
    select'I',null ,'Q', 2000 from dual union all
    select 'J' ,null, 'Q',300 from dual)
    where dept is null               --------------NULL Condition
    group by dept, loc;
    Output
    DEPT      LOC         SUM(SAL)
                    Q           3300
    The output is same for both the conditions.

  • Columns not to be included in group by clause

    I have a select query where i need to display count of records group by state_name
    In my main select query i need to include two more columns which are not be included in group by clause so that the number of records displayed does not increase
    I need to pass all these values from front end application so that i get count of records only by statename
    But when i include these two columns in my select query my query is throwing an error bcoz i have not included them in my group by
    How can we prevent this by not adding it to group by clause

    One general approach is (using the emp dept example)
    I want
    deptno dname, count(distinct empno)
    I can do that
    select d.deptno, d.dname, count(distinct e.empno)
    from dept d, emp e
    where e.deptno=d.deptno
    group by d.deptno, d.dname;
    or
    select d.deptno, d.dname, count_emp
    from dept d,
    (select e.deptno, count(distinct empno)
    from emp e
    group by e.deptno
    where d.deptno = e.deptno
    or depending on version (you don't mention versionk, though most questions have version dependent answers)
    select deptno
    , dname
    , (select count(distinct empno) from emp e where e.deptno=d.deptno)
    from dept d
    Obviously you can also use the FIRST and the LAST function to avoid having to include a column in the GROUP BY clause.
    Sybrand Bakker
    Senior Oracle DBA

  • Adding subquery to query w/ Group By clause crashes

    Hello all! I am trying to add in the subquery to the statement below. The query works fine before I add in the subquery, but after adding the subquery it will time out. If I comment out the MIN(table1.EOD_DATE) and the Group By clause, the query works fine.
    I do not want to do this I need those two items. What am I missing here? Any ideas are appreciated, thanks!
    SELECT table1.FUND,
    table1.DEPT,
    table1.ORG,
    table1.ACCT,
    LPAD(table1.FUND,3,0)||LPAD(table1.DEPT,2,0)||LPAD(table1.ORG,4,0)||SUBSTR(table1.ACCT,1,2) acct_no,
    LPAD(table1.FUND,3,0)||LPAD(table1.DEPT,2,0)||LPAD(table1.ORG,4,0)||table1.ACCT acct_no1,
    table2.FUND,
    table2.DEPT,
    table2.ORG,
    table2.ACCT
    MIN(table1.EOD_DATE)
    FROM table1,
    table2
    WHERE table1.fund BETWEEN substr(:P_ACCT_FROM,0,3) AND substr(:P_ACCT_TO,0,3)
    ANDtable1.dept BETWEEN substr(:P_ACCT_FROM,4,2) AND substr(:P_ACCT_TO,4,2)
    ANDtable1.org BETWEEN substr(:P_ACCT_FROM,6,4) AND substr(:P_ACCT_TO,6,4)
    ANDtable1.acct BETWEEN substr(:P_ACCT_FROM,10,5) AND substr(:P_ACCT_TO,10,5)
    AND floor(table1.acct/10000) in (6,8)
    AND SUBSTR(table1.acct,3) != '000'
    ANDtable1.eod_date BETWEEN :p_from_date AND :p_to_date
    AND table2.fund (+)=table1.fund
    AND table2.dept (+)=table1.dept
    AND table2.org (+)=table1.org
    AND table2.acct (+)=table1.acct
    AND table2.type IN( 'PI','JE','PR','VD','VU','AC','AD')
    AND table2.po_no IN
    SELECT trans.po_no
    FROM table2 trans LEFT OUTER JOIN req ON trans.po_no = req.po_no
    WHERE (trans.po_no IS NULL OR (TO_CHAR(req.open_date,'YYYY')) = (TO_CHAR(:p_year,'FM9999')))
    GROUP BY table1.fund,
    table1.dept,
    table1.org,
    table1.acct,
    table2.fund,
    table2.dept,
    table2.org,
    table2.acct
    ORDER BY LPAD(table1.FUND,3,0)||LPAD(table1.DEPT,2,0)||LPAD(table1.ORG,4,0)||SUBSTR(table1.ACCT,1,2),
    LPAD(table1.FUND,3,0)||LPAD(table1.DEPT,2,0)||LPAD(table1.ORG,4,0)||table1.ACCT

    Some untested comments;
    AND floor(table1.acct/10000) in (6,8)
    AND SUBSTR(table1.acct,3) != '000'Can these two conditions be combined, ie is this the same as;
    AND (table1.acct LIKE '0006%' OR table1.acct LIKE '0008%')Ignoring the hard coded dates (assumed they where used for testing) you should avoid using implicit conversions and two digit years;
    AND trans.activity_date BETWEEN TO_CHAR('01-jan-2007', 'dd-mon-yyyy') AND TO_CHAR('01-feb-2007', 'dd-mon-yyyy')You can convert your parameters once rather than converting every row by making hte parameter match the column data type;
    AND (   trans.po_no IS NULL
         OR req.open_date BETWEEN TO_DATE('01-JAN-' || TO_CHAR(:p_year,'9999') || ' 00:00:00' ,'DD-MON-YYYY HH24:MI:SS')
                              AND TO_DATE('31-DEC-' || TO_CHAR(:p_year,'9999') || ' 23:59:59' ,'DD-MON-YYYY HH24:MI:SS')
        )

  • 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 ORDER BY Different column than in the group by clause.

    Hello, I have this sql statement
    SELECT COUNT([Tags]) AS CNT, [Tags]
    FROM [Tags], [Images]
    WHERE
    ([Images].[Tags] LIKE '%' + [Tags].[LongTag] + '%') AND
    ([Tags].AllowTagPost = 'True' )
    GROUP BY [Images].[Tags]
    HAVING COUNT([Tags]) > 30
    ORDER BY [Tags].[LastTagPost] DESC
    It returns the tags, the number of occurances sorted by count.  However, I want to sort it by a column in the Tags table named LastTagPost (DATETIME)  The idea is to get the tag that hasn't been used in the longest time and also has more occurances
    than 30 in the image table.  When I try to order by this column i get an error that i'm sure most of you are all to familer with.
    Error:
    Column "Tags.LastTagPost" is invalid in the ORDER BY clause because it is not contained in either an aggregate function or the GROUP BY clause.
    Any help would be greatly appreciated.

    Couldn't you just include the LastTagPost as a MIN?
    DECLARE @tags TABLE (LastTagPost DATETIME, LongTag VARCHAR(100), allowTagPost CHAR(5))
    DECLARE @images TABLE (tags VARCHAR(10))
    SELECT COUNT(i.Tags) AS CNT, i.Tags, MIN(t.LastTagPost) AS lastTagPost
    FROM @Tags t
    INNER JOIN @Images i
    ON i.Tags LIKE '%' + t.LongTag + '%'
    WHERE t.AllowTagPost = 'True'
    GROUP BY i.Tags
    HAVING COUNT(i.Tags) > 30
    ORDER BY LastTagPost DESC
    Don't forget to mark helpful posts, and answers. It helps others to find relevant posts to the same question.

  • Select columns not in group by clause

    Hi Guys,
    I want to fetch columns from a table which are not in group by clause. The catch here is that I also need a count and decode column..
    SELECT col_A, col_B, decode(col_C, '10', '10', '26', '26', '00') col_CT, col_X, col_Y count(*) CNT
    FROM TABLE_T
    WHERE col_B IN (100,101,102) AND col_C IN ('44','45','10','26')
    GROUP BY col_A, col_B, decode(col_C, '10', '10', '26', '26', '00')
    ORDER BY col_CT
    Since, col_X and col_Y are not in GROUP BY clause, it throws error. Also, decode/count of the columns makes it more complex.
    Please help me on this.
    Thanks,
    Amy

    Hi, Amy,
    Welcome to the forum!
    Whenever you have a problem, post a little sample data (CREATE TABLE and INSERT statments, relevant columns only) for all tables involved, and the results you want from that data.
    Always say which versionof Oracle you're using.
    How to do what youy want depends on what you want, which is unclear.
    If you say "GROUP BY a, b, c", that means you only want one row of output for each distinct combination of a, b and c. How do x and y fit into that? Do you want a separate output row for each distinct combination of a, b,c, x and y? Then add x and y to the GROUP BY clause. If you don't want separate rows for each combinataion of x and y, what do you want when a group has more than 1 value for either of them?
    You can simplify the code a little by doing the DECODE in a sub-query; then you can use the alias col_ct as many times as you like in the main query. That will make the code easier to maintain, too.

  • How to display column specified in group by clause only once?

    I've a result coming out of a sql query that uses group by clause as follows:
    2008-07-25 19" LCD Screen 5
    2008-07-25 HP Printer     4
    2008-08-01 Hanging Files 11
    2008-08-01 Stapler     3
    2008-08-15 19" LCD Screen 5
    2008-08-15 Hanging Files 14
    What should I do in the query to display the result set as follows:
    2008-07-25 19" LCD Screen 5
         HP Printer     4
    2008-08-01 Hanging Files 11
         Stapler     3
    2008-08-15 19" LCD Screen 5
    Hanging Files 14

    Hi,
    That's a display issue, and display issues are best handled by the front end.
    What is your frone end?
    If it's SQL*Plus, you can use the BREAK command.
    For example:
    BREAK ON  deptno     NODUPLICATES
    SELECT       deptno
    ,       ename
    FROM       scott.emp
    ORDER BY  deptno
    ,            ename
    ;Output:
    `   DEPTNO ENAME
            10 CLARK
               KING
               MILLER
            20 ADAMS
               FORD
               JONES
               SCOTT
               SMITH
            30 ALLEN
               BLAKE
               JAMES
               MARTIN
               TURNER
               WARDNote that deptno is never NULL: KING and MILLER have deptno=10; but it doesn't show in the display, because it's the same as the deptno on the previous row.
    NODUPLICATES is actually the default, so you could just say
    BREAK ON deptnoThe BREAK command remains in force until you end the session (or un-do it, e.g. with "CLEAR BREAKS")
    Remember to issue the BREAK command at least once in the session before running the query.
    To get the same results in SQL, use the analytic ROW_NUMBER function.

  • 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

  • HOW TO ADD GROUP BY CLAUSE WHEN USING Jbo:Datatable

    Would like to know if there are any provisions for adding a Group by clause so the data shown using DataTable Component cn be grouped.
    eg:
    Currently using DataTable component shows data as shown below.
    Dept Name
    10 ABC
    10 DEF
    10 PQR
    20 XYZ
    20 QQQ
    Can this be done as shown bewlow
    Dept Name
    10 ABC
    DEF
    PQR
    20 XYZ
    QQQ
    Thanks

    Hi Sashi,
    For a bc4j view object, the shape of the result set needs to be consistent. So, there can not be a groupby directly in the result set.
    However, that does not prevent you from inserting a calculated column that performs a similar calculation.
    Another mechanism might be to consider a master detail arrangement of the views where the master contains an aggregation of data from its children. You are then free to access and display this data as you choose.
    Hope this helps,
    Pete

Maybe you are looking for

  • Video Message not Sending

    I sent my friend a video message, and for the past 24 hours it has been stuck at 99% and simply wont send.  There has been no error message.  I tried restarting Skype and restarting my computer.  Now, the icon has even turned black and I'm starting t

  • Clearing documents through EBS

    Hi Experts, Is there a way to clear the documents of sub G/L's or Clearing accounts with out having any information in the EBS file. To be more specific on my requirement  when i paid the vendor invoices DR Vendor CR  Clearing account When EBS statem

  • How do i write text in a reverse curve?

    I am designing a logo in a circle form.  I have written the lettering on top of the circle and now would like to write underneath the circle.

  • URGENT java to_char(..,'IW') vs java Calendar WEEK_OF_YEAR

    Hi all; I have written a sql stmt that groups values as to_char(date1,'IW'). Result of each group is saved in an Vector array length of 54. On JSP, I get values by calendar1.WEEK_OF_YEAR. When I check end and begining of each year there is difference

  • Second Ipod with iTunes

    I have a regular IPod I've been using with iTunes through Firewire. I bought a nano as a second player but can't get iTunes to recognize it. It's connected through USB. What am I doing wrong? Bill