Group By Clause Problem
Hello every body,
I want to make a report joining two tables one is master and other is detail. The tables have following detail;
Master Table(FILEMASTER)
FILEID NUMBER,(PK)
FILENAME VARCHAR2(80),
DESCRIPTION VARCHAR2(50)
Detail Table(FILEDETAIL)
FILEDETAILID NUMBER,(PK)
FILEID NUMBER,(PK)(FK)
RECEIVEDATE DATE,
SENDDATE DATE,
TO_FROM VARCHAR2(50),
ACTION VARCHAR2(50),
FROM_TO VARCHAR2(50),
DESIGNATION_ID NUMBER,
TODESIGNATION NUMBER,
FROMDESIGNATION NUMBER
Now there are multiple records in the detail table against each record in the master. I want to display the max(FILEDETAILID) with some other columns of DETAILED Table and MASTER table. for example I want to display FILENAME,FILEDETAILID,FILEID,RECEIVEDATE and ACTION fields I used the following query;
select max(FILEDETAIL.FILEDETAILID), FILEDETAIL.FILEID, FILEDETAIL.RECEIVEDATE, FILEDETAIL.ACTION,FILEMASTER.FILENAME
from FILEDETAIL, FILEMASTER
where FILEMASTER.FILEID=FILEDETAIL.FILEID
group by FILEDETAIL.FILEID, FILEMASTER.FILENAME, FILEDETAIL.RECEIVEDATE, FILEDETAIL.ACTION;
The number of rows returned are 32. Which include some rows which should not be. Because total record in the master table are 29 and I want one (maximum filedetailid) record from detail table for each record of master table.
The following query runs fine and return 29 record;
select max(FILEDETAIL.FILEDETAILID), FILEDETAIL.FILEID, FILEMASTER.FILENAME
from FILEDETAIL,FILEMASTER
where filemaster.FILEID=filedetail.FILEID
group by FILEDETAIL.FILEID,filemaster.FILENAME
so what happens if add some more columns like FILEDETAIL.RECEIVEDATE and FILEDETAIL.ACTION from child table?
Please tell me as soon as possible.
Regards,
Iftikhar Ahmad
Try something like this one:
select FILEDETAIL.FILEDETAILID, FILEDETAIL.FILEID, FILEDETAIL.RECEIVEDATE, FILEDETAIL.ACTION,FILEMASTER.FILENAME
from FILEDETAIL, FILEMASTER
where FILEMASTER.FILEID=FILEDETAIL.FILEID
and (FILEDETAIL.FILEID, FILEDETAIL.FILEDETAILID) in
(select FILEID, max(FILEDETAILID) from FILEDETAIL group by FILEID)
I have not tested this.
Martin
Similar Messages
-
SQL Server Compact 3.5 GROUP BY Clause Problem
Hi,
I have a table as below:
Tag(string) Name(string) Description(string) LocationID(Guid)
"SELECT Tag, Name, Description, LocationID FROM MyTable GROUP BY LocationID"
gives me an error, any ideas on what I am doing wrong?
Thanks
Paul.
Paul WainwrightHi pr_wainwright,
GROUP BY statement is used in conjunction with the aggregate functions to group the result-set by one or more columns
e.g
select Tag,name, Description, sum(locationid) from mytable group by
Tag,name, Description
SELECT columnname, aggregatefunction(columnname)
FROM tablename
WHERE columnname operator value
GROUP BY columnname; -
Problem with the query in group by clause
hi, i have problem with group by clause, can some one please help me.
select
header_id,
(select sum(nvl(dr,0) - nvl(cr ,0)) from temp_tab a1
where
a1.country=a.country
and a1.source='AP'
and a1.header_id=a.header_id) WHT,
sum(dr),
sum(cr) from temp_tab a
group by header_id,
(select sum(nvl(dr,0) - nvl(cr ,0)) from temp_tab a1
where
a1.country=a.country
and a1.source='AP'
and a1.header_id=a.header_id)
select * from temp_tab
drop table temp_tab
create table temp_tab(header_id number ,line_num number, country varchar2(2),
source varchar2(2), dr number, cr number,primary key(header_id,line_num));
insert into temp_tab(header_id, line_num,country, source, dr,cr) values(1, 1,'NL','AP',100,20);
insert into temp_tab(header_id, line_num,country, source, dr,cr) values(1, 2,'PO','AP',20,20);
insert into temp_tab(header_id, line_num,country, source, dr,cr) values(1, 3,'NL','AP',70,20);
insert into temp_tab(header_id, line_num,country, source, dr,cr) values(2, 1,'NL','PA',100,20);
insert into temp_tab(header_id, line_num,country, source, dr,cr) values(2, 2,'NL','PA',100,20);
insert into temp_tab(header_id, line_num,country, source, dr,cr) values(3, 1,'KR','PO',100,20);
commit;
Appreciate your help.
Thanks,select header_id,
(select sum(nvl(dr,0) - nvl(cr ,0)) from temp_tab a1
where a1.country=a.country
and a1.source='AP'
and a1.header_id=a.header_id) WHT,
sum(dr),
sum(cr)
from temp_tab a
group by header_id
,countryIt's kinda hard to follow what your query does... maybe because I'm only at my second coffee..
Edited by: Alex Nuijten on Oct 2, 2009 8:07 AM -
Problem trying to use a group by clause
hey good day,
i'm trying to use a group by clause in my application, but I'm getting the following error.
Error:- Query cannot be parsed, please check the syntax of your query. (ORA-00979: not a GROUP BY expression)
select "INSTRUCTOR"."EMPNUM",
"INSTRUCTOR"."FIRSTNAME",
"INSTRUCTOR"."LASTNAME",
"QUALN"."SPECIALIZE_FIELD" as "SPECIALIZE_FIELD",
"INSTRUCTOR"."USERNAME"
from "QUALN" "QUALN",
"INSTRUCTOR" "INSTRUCTOR"
where "INSTRUCTOR"."EMPNUM"="QUALN"."EMPNUM"
group by "INSTRUCTOR"."EMPNUM", "INSTRUCTOR"."FIRSTNAME", "INSTRUCTOR"."LASTNAME"Thanks in advance,
RichieRichie wrote:
hey thanks for your reply,
i have tried what you have suggested, but now i got another error
Error :- The report query needs a unique key to identify each row. The supplied key cannot be used for this query. Please edit the report attributes to define a unique key column. ORA-01446: cannot select ROWID from, or sample, a ...
This error message is not from oracle, btu from your reporting tool. it might be MS Access or whatever other tool that you use. Some of these tools want a unique value to identify the current row. The logic applied depends on the relationship of your tables. however in your case you could do it without the group by condition. THen the rowid can still be applied to your query.
Example
note the usage of alias names to simplified the select
select i.EMPNUM ,
i.FIRSTNAME ,
i.LASTNAME ,
i.USERNAME
from INSTRUCTOR i
where i.EMPNUM in (select q.EMPNUM from QUALN q); -
Why is the GROUP BY clause not working in my Query?
Dear All,
Below is the Query for a Summary Debtors Aged Analysis.
The GROUP BY clause does not seem to be working.
The Query returns all the unpaid invoices, instead of a single total row for each Customer.
If a Customer X has 10 unpaid invoices, 10 rows are displayed - one for each invoice.
I was expecting only 1 row for Customer X, 1 for Customer Y, etc.
This is what GROUP BY is supposed to do, but it is not doing its work.
What has gone wrong?
Thanks
Leon Lai
Here's my Query:
declare @taxdt datetime
set @taxdt
/*select 1 from jdt1 t0 where t0.TaxDate*/ = [%1]
SELECT
CASE
WHEN T0.Account = 1220101 THEN 'Prim Cust'
WHEN T0.Account = 1220102 THEN 'Fgn Cust'
WHEN T0.Account = 1220103 THEN 'Local Cust'
WHEN T0.Account = 1220104 THEN 'Staff Loan'
WHEN T0.Account = 1220105 THEN 'Dep with TP'
WHEN T0.Account = 1220106 THEN 'Adv to Cust'
WHEN T0.Account = 1220108 THEN 'Sund Drs'
ELSE 'Error ! ! !'
END AS 'Control A/c',
T1.CardCode AS 'BP Code',
T2.Notes2 AS 'BP Name',
SUM ((T0.Debit - T0.Credit)) AS 'Orig. Rs',
SUM ((T0.BalDueDeb - T0.BalDueCred)) AS 'Bal. Rs',
((SELECT SUM(T0.BalDueDeb) - Sum(T0.BalDueCred)
WHERE DateDiff(mm, T0.TaxDate, @taxdt) = 1))
AS '1 Mth Ago'
/* Similarly for other age brackets*/
FROM JDT1 T0
INNER JOIN OCRD T1 ON T0.ShortName = T1.CardCode
LEFT OUTER JOIN OCPR T2 ON T1.CardCode = T2.Cardcode
LEFT OUTER JOIN OJDT T3 ON T0.TransID = T3.TransID
LEFT OUTER JOIN OINV T4 ON T3.TransID = T4.TransID
LEFT OUTER JOIN ORIN T5 ON T3.TransID = T5.TransID
WHERE
T1.CardType = 'C'
and (Balance) != 0
and (T0.BalDueDeb - T0.BalDueCred) != 0
GROUP BY T0.Account, T1.CardCode, T2.Notes2, T0.TaxDateDear Neetu,
Thanks for your reply.
This Query is a modification of the Query you posted in SAP B1 SQL TIPS & TRICKS
http://wiki.sdn.sap.com/wiki/display/B1/SAPB1SQLB-FNDebtorsAgingReportbydate
So, maybe instead of referring to my Query, let's refer to yours. It may be easier for you to understand me.
Once I understand the problem, I can adapt your query to suit my requirements
So, let's start with a clean slate:
The Query you have posted is for a DETAILED Debtors Aging Report.
This lists all outstanding invoices, and ages them in the Age Brackets.
What I want is a SUMMARY Debtors Aging Report.
This will give the total amount owed by each Customer, and this amount is broken down in the Age Bracket Columns
There will be a single row listed for each customer, something like this:
Customer Total Due Current 1 Mth 2 Mth 3 Mth etc
Alfred 500,000 300,000 200,000
Charles 800,000 100,000 300,000 400,000
How can you modify your query to make it become a Summary Report (1 line for each customer even if he has many invoices)?
Thanks
Leon Lai
Here's your code
SELECT T1.CardCode, T1.CardName, T1.CreditLine, T0.RefDate, T0.Ref1 'Document Number',
CASE WHEN T0.TransType=13 THEN 'Invoice'
WHEN T0.TransType=14 THEN 'Credit Note'
WHEN T0.TransType=30 THEN 'Journal'
WHEN T0.TransType=24 THEN 'Receipt'
END AS 'Document Type',
T0.DueDate, (T0.Debit- T0.Credit) 'Balance'
,ISNULL((SELECT T0.Debit-T0.Credit WHERE DateDiff(day, T0.DueDate,'[%1]')<=-1),0) 'Future'
,ISNULL((SELECT T0.Debit-T0.Credit WHERE DateDiff(day, T0.DueDate,'[%1]')>=0 and DateDiff(day, T0.DueDate,'[%1]')<=30),0) 'Current'
,ISNULL((SELECT T0.Debit-T0.Credit WHERE DateDiff(day, T0.DueDate,'[%1]')>30 and DateDiff(day, T0.DueDate,'[%1]')<=60),0) '31-60 Days'
,ISNULL((SELECT T0.Debit-T0.Credit WHERE DateDiff(day, T0.DueDate,'[%1]')>60 and DateDiff(day, T0.DueDate,'[%1]')<=90),0) '61-90 Days'
,ISNULL((SELECT T0.Debit-T0.Credit WHERE DateDiff(day, T0.DueDate,'[%1]')>90 and DateDiff(day, T0.DueDate,'[%1]')<=120),0) '91-120 Days'
,ISNULL((SELECT T0.Debit-T0.Credit WHERE DateDiff(day, T0.DueDate,'[%1]')>=121),0) '121+ Days'
FROM JDT1 T0 INNER JOIN OCRD T1 ON T0.ShortName = T1.CardCode
WHERE (T0.MthDate IS NULL OR T0.MthDate > [%1]) AND T0.RefDate <= [%1] AND T1.CardType = 'C'
ORDER BY T1.CardCode, T0.DueDate, T0.Ref1 -
How to write a SQL Query without using group by clause
Hi,
Can anyone help me to find out if there is a approach to build a SQL Query without using group by clause.
Please site an example if is it so,
RegardsI hope this example could illuminate danepc on is problem.
CREATE or replace TYPE MY_ARRAY AS TABLE OF INTEGER
CREATE OR REPLACE FUNCTION GET_ARR return my_array
as
arr my_array;
begin
arr := my_array();
for i in 1..10 loop
arr.extend;
arr(i) := i mod 7;
end loop;
return arr;
end;
select column_value
from table(get_arr)
order by column_value;
select column_value,count(*) occurences
from table(get_arr)
group by column_value
order by column_value;And the output should be something like this:
SQL> CREATE or replace TYPE MY_ARRAY AS TABLE OF INTEGER
2 /
Tipo creato.
SQL>
SQL> CREATE OR REPLACE FUNCTION GET_ARR return my_array
2 as
3 arr my_array;
4 begin
5 arr := my_array();
6 for i in 1..10 loop
7 arr.extend;
8 arr(i) := i mod 7;
9 end loop;
10 return arr;
11 end;
12 /
Funzione creata.
SQL>
SQL>
SQL> select column_value
2 from table(get_arr)
3 order by column_value;
COLUMN_VALUE
0
1
1
2
2
3
3
4
5
6
Selezionate 10 righe.
SQL>
SQL> select column_value,count(*) occurences
2 from table(get_arr)
3 group by column_value
4 order by column_value;
COLUMN_VALUE OCCURENCES
0 1
1 2
2 2
3 2
4 1
5 1
6 1
Selezionate 7 righe.
SQL> Bye Alessandro -
-- 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. -
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 -
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. -
In evaluating several query plans, I've discovered several sqlstop messages that read:
<groupBy preclustered="true" sqlstop="Cannot generate SQL for the 'group-by' clause because it is not equivalent to the relational GROUP BY clause" stable="true">
I've examined the XQuery code and find no group clause and I can't find a reference to this message in the documentation or by googling the text. I assume that the grouping is being done implicitly by ODSI since it is not in the XQuery source. I have tried restructuring the code to no avail.
My question is -- is this causing me a performance problem since the sql is not generated, and if so, what steps must I take in the xquery structure to avoid this issue.
Many thanks to anyone who can provide some insight into this.
Regards,
PBis this causing me a performance problem since the sql is not generatedMy question is - why are you asking this question? :) Do you have a performance problem? The "Best Practices" posted as an announcement in this forum might help. Otherwise engage customer support.
To answer your question - your xquery likely generates nested/hierarchical xml - and looks something like below - which can be implemented with a sql left-outer-join ordered by CUSTOMER_ID of the customer-table, and taking the rows returned and ... grouping by CUSTOMER_ID to eliminate the duplicate customer information. So there's your group-by. But you cannot write sql that has a left-outer join, and then a group-by on the left-hand side. So the group by is done in the engine. Since the results are already sorted, the group-by in the engine simply skips over the duplicates (i.e. it's basically free).
for $c in CUSTOMER()
where $c/LAST_NAME = $lastname
return
<CUSTOMER>
... $c/CUSTOMER_ID ... (: in a left-outer-join, the CUSTOMER_ID is duplicated for every ORDER :)
{ for $o in ORDER()
where $o/CUSTOMER_ID eq $c/CUSTOMER_ID
return
</CUSTOMER> -
Aggregate fuction with group by clause
Hello,
Following is assignment is given but i dont get correct output
so please i am request to all of us write code to solve my problem.
There can be multiple records for one customer in VBAK tables with different combinations.
Considering that we do not need details of each sales order,
use Aggregate functions with GROUP BY clause in SELECT to read the fields.
<garbled code removed>
Moderator Message: Please paste the relevant portions of the code
Edited by: Suhas Saha on Nov 18, 2011 1:48 PMSo if you need not want all the repeated records, then you select all the values to an Internal table,
and declare an internal table of same type and Usee COLLECT
for ex:
itab1 type <xxxx>.
wa_itba like line of itab1.
itab2 type <xxxx>. "<-This should be same type of above.
select * from ..... into table itab1.
and now...
loop at itab1 into wa_itab.
collect wa_itab1 into itab2.
endloop.
then you will get your desired result.. -
Can't understand how this group by clause works
The Schema is as below: (http://www.psoug.org/reference/rollup.html)
CREATE TABLE grp_rep (
person_id NUMBER(3),
division VARCHAR2(3),
commission NUMBER(5));
INSERT INTO grp_rep VALUES (1,'SAM',1000);
INSERT INTO grp_rep VALUES (2,'EUR',1200);
INSERT INTO grp_rep VALUES (1,'EUR',1450);
INSERT INTO grp_rep VALUES (1,'EUR',700);
INSERT INTO grp_rep VALUES (2,'SEA',1000);
INSERT INTO grp_rep VALUES (2,'SEA',2000);
INSERT INTO grp_rep VALUES (1,'EUR',800);
COMMIT;
Query1:
SELECT person_id, division, SUM(commission)
FROM grp_rep
GROUP BY person_id, ROLLUP (person_id, division);
Query2:
SELECT person_id, division, SUM(commission)
FROM grp_rep
GROUP BY division, ROLLUP (person_id, division);
The results of query1 are okay. It has results from rollup and group by person_id.
But, in Query2 results of rollup are missing and results of group by division is there.
Anyone can explain how the group by clause works when there are multiple columns involving CUBE, ROLLUP and simple column names?
Regards.Thank you shoblock!
but, What i m really looking for is,
How group by clause works when i add regular column along with RollUp in group by clause?
I have understood simple group by clause like
...group by column1,column2,column3....
n I also know simple rollup clauses like
...group by rollup(column1,column2,column3,...)
But my Problem is how does this work:
...group by column2,rollup(column1,column2,...)
...group by column1,rollup(column1,column2,...)
See below Results:
Query1:
SELECT person_id, division, SUM(commission)
FROM grp_rep
GROUP BY person_id,ROLLUP ( person_id, division );
Result:
PERSON_ID DIVISION SUM(COMMISSION)
1 EUR 2950
1 SAM 1000
2 EUR 1200
2 SEA 3000
1 3950
2 4200
1 3950
2 4200
SELECT person_id, division, SUM(commission)
FROM grp_rep
GROUP BY division,ROLLUP ( person_id, division );
Query2:
SELECT person_id, division, SUM(commission)
FROM grp_rep
GROUP BY division,ROLLUP ( person_id, division );
Result:
PERSON_ID DIVISION SUM(COMMISSION)
1 EUR 2950
2 EUR 1200
1 SAM 1000
2 SEA 3000
1 EUR 2950
2 EUR 1200
1 SAM 1000
2 SEA 3000
EUR 4150
SAM 1000
SEA 3000
guys, help me make understand above results!
Regards. -
BUG?: Code Editor – Completion Insight - Autogenerate GROUP BY clause
With the “Autogenerate GROUP BY clause” enabled the following problem occurs:
Coding an inline select in an aggregate query:
SELECT (SELECT DEPARTMENT_NAME
FROM departments d
WHERE d.department_id = e.department_id) dep
, JOB_ID
, SUM (salary)
FROM employees e
GROUP BY department_id, job_idCode group-by-autogenerate transforms this from time to time into:
SELECT (SELECT DEPARTMENT_NAME
FROM departments d
WHERE d.department_id = e.department_id) dep
, JOB_ID
, SUM (salary)
FROM employees e
GROUP BY (SELECT DEPARTMENT_NAME
FROM departments d
WHERE d.department_id = e.department_id), JOB_IDWhich results in:
ORA-22818. 00000 - "subquery expressions not allowed here"
*Cause: An attempt was made to use a subquery expression where these
are not supported.
*Action: Rewrite the statement without the subquery expression.
when trying to run it.
(version 2.1.0.6.3; build MAIN-63.73; Windows XP)Duplicate of
2.1 EA1 - Auto Group By inserted when unwanted/expected -
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 issueGroup 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) -
How to handle Group by clause Dynamically in Oracle Form
Hi Expert,
Here i have problem :
i want display record with passing group by clause Parameter
Example :
Select zone,district,postal,count(Do_no) ,sum(Sales_Amt)
from DLV_ORD_mast
group by :zone,:district,:Postal
1-o/p like :
if user pass the :zone parameter in group by clause then record display like below o/p
zone count sls_amt
South 2 1000
central 3 5000
East 4 600
2-o/p
if use pass the :District parameter in group by clause then record display like below O/P
zone count sls_amt
South/Dist1 2 1000
central /Dist2 3 5000
central /Dist3 1 500
East /Dist4 4 600
2-o/p
if use pass the :postal parameter in group by clause then record display like below O/P
zone count sls_amt
South/Dist1/postal1 2 1000
central /Dist2/Postal2 3 5000
central /Dist3/postal3 1 500
East /Dist4/postal4 4 600
so if anybody come across same senario then Please give me the solution for this problem .
pleassssssssssssssssssssssssss
Thanks
AbhishekBuild a block one a Query FROM clause, like
SELECT GROUPCOL,
CNT,
SUMM
FROM (Select zone GROUPCOL,
count(Do_no) CNT,
sum(Sales_Amt) SUMM
from DLV_ORD_mast
group by zone
)Then at runtime, change the the block QUERY_DATASOURCE_NAME by SET_BLOCK_PROPERTY depending on the parameter, e.g. to
SELECT GROUPCOL,
CNT,
SUMM
FROM (Select zone ||', '|| DISTRICT GROUPCOL,
count(Do_no) CNT,
sum(Sales_Amt) SUMM
from DLV_ORD_mast
group by zone ||', '|| DISTRICT
)
Maybe you are looking for
-
Alternate Financial year(AFY) in Indian Payroll
Hi All, I am facing a strange problem in my payroll configuration. I activated business function for AFY and now Income tax should get calculated from March to Feb. I have necessary config for AFY including payroll period & control record. Now in my
-
HP Laserjet 4P Printing Problems
I am using a Mac G4 with 10.4.7 and printing on a HP Laser Jet 4P using driver HP Laser Jet 4 Series Gimp. When I first print a page or 20 pages of the job, all works fine. Once I go to print another job, I get the msg "GIMP Printing Page 1, 15% and
-
HP ENVY 17-2080en Notebook PC / Just Disappointment :(
Hi Everyone; On 03/03/2012 I bought an hp laptop. I bought this laptop I've had constant problems since. Products from the constant sound of the CPU fan comes and shuts off overheat product. To rectify this problem 3 times I sent the product to HP se
-
Lost Pictures, Help!!
I cannot find any pictures after May 2012 on my external hard drive even though I did a COMPLETE back up in Feb. 2013. Where can I find my pictures on the external hard drive? I went to Users > "MaW1028" > Pictures > Right Click on Iphoto to show pac
-
I run tiger on my mac can i upgrade to snow leopard?
I current run tiger and want to upgrade to leopard but can't find where to purchase it on the apple website. Is it still available for purchase, if it is how do I purchase it?