-- 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.
Similar Messages
-
Use of expressions or field aliases in GROUP BY clauses
I've seen numerous references, even BNF syntax
diagrams, that suggest that Oracle's parser will accept an expression in a
GROUP BY clause; however, I've had no success whatsoever in getting this to
work. Here's an example of something I had hoped would work, but does not:
SELECT
account_id,
CASE
WHEN txn_date BETWEEN DATE '2006-06-01' AND DATE '2006-06-07' THEN 1
WHEN txn_date BETWEEN DATE '2006-06-08' AND DATE '2006-06-14' THEN 2
ELSE 3
END AS week_id,
COUNT(*) AS week_cnt
FROM myschema.my_random_table_name
GROUP BY account_id, week_id;
The interpreter gags on the use of "week_id" in the GROUP BY statement.
Evidently Oracle syntax doesn't support the use of positional designations
in aggregating statements either - I tried using the clause
GROUP BY 1, 2;
as the last line, and it didn't like this either.
Any assistance or suggestions you can offer would be greatly appreciated.
I prefer to let the DBMS engine do my aggregating for me as much as
possible, but without being able to aggregate relative to programmer-defined
fields, I am somewhat hamstrung.try this :
SQL> select case when hire_date between '01-JAN-00' and '31-DEC-05' then 1 else 0 end col1, count(*)
2 from employees
3 group by col1;
group by col1
ERROR at line 3:
ORA-00904: "COL1": invalid identifier
SQL>
SQL>
SQL>
1 select case when hire_date between '01-JAN-00' and '31-DEC-05' then 1 else 0 end col1, count(*)
2 from employees
3* group by case when hire_date between '01-JAN-00' and '31-DEC-05' then 1 else 0 end
SQL> /
COL1 COUNT(*)
1 11
0 96 -
GROUP BY Clause -SQL Devolper error 00904. 00000 - "%s: invalid identifier
I'm a real novice with SQL and I am having a problem understanding why this doesn't work. Searching the web got me to this forum, but I haven't been able to find a solution. Obviuosly I don't really understand how to use the GROUP BY clause. The SQL works fine without that clause.
As stated in the subject I am getting this error:
ORA-00904: "SORTPLAN": invalid identifier
00904. 00000 - "%s: invalid identifier"
*Cause:
*Action:
Error at Line: 9 Column: 17
With this SQL statement:
SELECT START_DTM,
END_DTM,
MACHINE_SORT_PROGRAM_NAME as Sortplan,
sum(TOTAL_PIECES_FED_CNT) AS TotalFed
FROM END_OF_RUN
WHERE MODS_DATE BETWEEN '27-Jul-2011' AND '27-Jul-2011'
AND MAIL_OPERATION_NBR =919
AND SITE_ID = 81003
GROUP BY Sortplan
ORDER BY Sortplan;
TIA
MikeGary,
Thank you for pointing me in the right direction. There are so many choices here I had a hard time deciding which to use. :)
I tried your suggestion and still got an error. I had thought I didn't need to use aggregate functions on all of the fields. I guess that was wrong, so I changed it. Once I did that the SQL worked fine.
ORA-00979: not a GROUP BY expression
00979. 00000 - "not a GROUP BY expression"
*Cause:
*Action:
Error at Line: 2 Column: 3
SELECT MACHINE_SORT_PROGRAM_NAME AS Sortplan,
min(START_DTM),
max(END_DTM),
sum(TOTAL_PIECES_FED_CNT) AS TotalFed
FROM END_OF_RUN
WHERE MODS_DATE BETWEEN '27-Jul-2011' AND '27-Jul-2011'
AND MAIL_OPERATION_NBR =919
AND SITE_ID = 81003
GROUP BY MACHINE_SORT_PROGRAM_NAME
ORDER BY Sortplan;
Mike -
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.. -
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 -
Aggregate functions without group by clause
hi friends,
i was asked an interesting question by my friend. The question is...
There is a DEPT table which has dept_no and dept_name. There is an EMP table which has emp_no, emp_name and dept_no.
My requirement is to get the the dept_no, dept_name and the no. of employees in that department. This should be done without using a group by clause.
Can anyone of you help me to get a solution for this?select distinct emp.deptno,dname
,count(*) over(partition by emp.deptno)
from emp
,dept
where emp.deptno=dept.deptno;
10 ACCOUNTING 3
20 RESEARCH 5
30 SALES 6 -
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 2ExpansiveMind 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 -
Group by clause and having clause in select
hi frnds
plz give me some information of group by and having clause used in select statement with example
thanksThe Open SQL statement for reading data from database tables is:
SELECT <result>
INTO <target>
FROM <source>
[WHERE <condition>]
[GROUP BY <fields>]
[HAVING <cond>]
[ORDER BY <fields>].
The SELECT statement is divided into a series of simple clauses, each of which has a different part to play in selecting, placing, and arranging the data from the database.
You can only use the HAVING clause in conjunction with the GROUP BY clause.
To select line groups, use:
SELECT <lines> <s1> [AS <a1>] <s2> [AS <a2>] ...
<agg> <sm> [AS <am>] <agg> <sn> [AS <an>] ...
GROUP BY <s1> <s2> ....
HAVING <cond>.
The conditions <cond> that you can use in the HAVING clause are the same as those in the SELECT clause, with the restrictions that you can only use columns from the SELECT clause, and not all of the columns from the database tables in the FROM clause. If you use an invalid column, a runtime error results.
On the other hand, you can enter aggregate expressions for all columns read from the database table that do not appear in the GROUP BY clause. This means that you can use aggregate expressions, even if they do not appear in the SELECT clause. You cannot use aggregate expressions in the conditions in the WHERE clause.
As in the WHERE clause, you can specify the conditions in the HAVING clause as the contents of an internal table with line type C and length 72.
Example
DATA WA TYPE SFLIGHT.
SELECT CONNID
INTO WA-CONNID
FROM SFLIGHT
WHERE CARRID = 'LH'
GROUP BY CONNID
HAVING SUM( SEATSOCC ) > 300.
WRITE: / WA-CARRID, WA-CONNID.
ENDSELECT.
This example selects groups of lines from database table SFLIGHT with the value LH for CARRID and identical values of CONNID. The groups are then restricted further by the condition that the sum of the contents of the column SEATSOCC for a group must be greater than 300.
The <b>GROUP BY</b> clause summarizes several lines from the database table into a single line of the selection.
The GROUP BY clause allows you to summarize lines that have the same content in particular columns. Aggregate functions are applied to the other columns. You can specify the columns in the GROUP BY clause either statically or dynamically.
Specifying Columns Statically
To specify the columns in the GROUP BY clause statically, use:
SELECT <lines> <s1> [AS <a 1>] <s 2> [AS <a 2>] ...
<agg> <sm> [AS <a m>] <agg> <s n> [AS <a n>] ...
GROUP BY <s1> <s 2> ....
To use the GROUP BY clause, you must specify all of the relevant columns in the SELECT clause. In the GROUP BY clause, you list the field names of the columns whose contents must be the same. You can only use the field names as they appear in the database table. Alias names from the SELECT clause are not allowed.
All columns of the SELECT clause that are not listed in the GROUP BY clause must be included in aggregate functions. This defines how the contents of these columns is calculated when the lines are summarized.
Specifying Columns Dynamically
To specify the columns in the GROUP BY clause dynamically, use:
... GROUP BY (<itab>) ...
where <itab> is an internal table with line type C and maximum length 72 characters containing the column names <s 1 > <s 2 > .....
Example
DATA: CARRID TYPE SFLIGHT-CARRID,
MINIMUM TYPE P DECIMALS 2,
MAXIMUM TYPE P DECIMALS 2.
SELECT CARRID MIN( PRICE ) MAX( PRICE )
INTO (CARRID, MINIMUM, MAXIMUM)
FROM SFLIGHT
GROUP BY CARRID.
WRITE: / CARRID, MINIMUM, MAXIMUM.
ENDSELECT.
regards
vinod -
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. -
Group By clause is not working
I have two columns Department and EmpName:
Department EmpName
Sales empname1
Sales empname2
Marketing empname3
Development empname4
Now I want to count the number of employees in every department..
I want the output to be
Department Total
Sales 2
Marketing 1
Development 1
I am retrieving names of the department through a subquery
The query I am trying to execute is:
SELECT Department, Employee FROM
( SELECT ...query from other table) AS Department, count( A.EmpName) AS Employee
FROM Employer A, EmployeeInfo B
WHERE (A.EmpID = B.EmpID AND A.EmpCategory like 'Category2')
GROUP BY Department
I know that you cannot group by using aliases and hence a little work around, but still the query isn't working...I appreciate any help!!!!
Edited by: 968775 on Oct 31, 2012 12:53 PM
Edited by: 968775 on Oct 31, 2012 12:54 PMHi,
Welcome to the forum!
968775 wrote:
The query I am trying to execute is:
SELECT Department, Employee FROM
( SELECT ...query from other table) AS Department, count( A.EmpName) AS Employee
FROM Employer A, EmployeeInfo B
WHERE (A.EmpID = B.EmpID AND A.EmpCategory like 'Category2')
GROUP BY DepartmentRemember the ABC's of GROUP BY:
When you use a GROUP BY clause and/or an aggregate fucntion, then every item 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')").
In the query above, EMPOYEE is none of these. I think you meant COUNT ( employee).
SELECT Department
, COUNT (Employee) AS num_employees
FROM ...
I know that you cannot group by usingalias name and hence a little work around, You can assign the alias in a sub-query, then use the alias in the GROUP BY clause, or anywhere else you want to, in a super-query.
I hope this answers your question.
If not, post a little sample data (CREATE TABLE and INSERT statements, relevant columns only) for all tables involved, and also post the results you want from that data.
Explain, using specific examples, how you get those results from that data.
Always say which version of Oracle you're using (e.g., 11.2.0.2.0).
See the forum FAQ {message:id=9360002}
You'll get better answers faster if you always supply this information whenever you post a question. -
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 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,
CHYes. 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 -
Crystal error: A summary has been specified on a non-recurring field.
Hi,
Im encountering the following error while trying to aggregate a formula field in one of my reports:
"A summary has been specified on a non-recurring field"
The report calls a subreport in the details section which returns a shared currency variable for every record generated. Im capturing this varaiable using a formula field say Temp1 as below,
Temp1
====================
WhilePrintingRecords;
Shared CurrencyVar ExRate;
ExRate
Im using Temp1 in another formula field Temp2 to perform calculation using another formula field as below,
Temp2
=====================
@Temp1 * @Actual
I have to aggregate Temp2 for each group of records the report generates.
But the report isnt letting me do so via the insert menu. On manually adding the sum function in a formula, it says "cannot summarize field". If i add sum(temp2) to an existing formula field on the report, it gives the error 'A summary has been specified on a non-recurring field' while running it.
Have been stuck on this for a while.
Any Pointers will be appreciated.
Thanks
DhirajI"m not 100% sure about the reason behind this but I think it has something to do with the data not being part of the records on the main report. I've come across it many times myself and the only solution I have ever been able to get work it to create my own calculation for the subtotal, insert it in the same section as @Temp2 and indicate to EvalutateAfter(@Temp2).
Hope this helps, rasinc -
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 -
Error: "a summary has been specified on a non recurring field"
Hi,
I have a formula which calculates a quantity amount based on different quantity types & finally sums all the quantity to give a
result, tis result i want to show in crystal report, This field is put in report footer, when i try to run the report i get the following error for that field 'a summary has been specified on a non recurring field' below i put my formula:
//sum for qty type Fuel
numberVar qtyFuel;
if
{2O0GHO_C06_0GHO_T06_Q0002.[2O0GHO_C06-THEO_ACT_IND]} = 'A' and
{2O0GHO_C06_0GHO_T06_Q0002.[2O0GHO_C06-MEDIUM]} = 'GAS' and {2O0GHO_C06_0GHO_T06_Q0002.[2O0GHO_C06-QTY_TYPE]-[22O0GHO_C06-QTY_TYPE]} = '01'
then
qtyFuel := Sum ({2O0GHO_C06_0GHO_T06_Q0002.[Measures]-[DWQWE23XXYJCROQ978M8NX3J5]});
//sum for qty type Inventory
numberVar qtyInventory;
if
{2O0GHO_C06_0GHO_T06_Q0002.[2O0GHO_C06-THEO_ACT_IND]} = 'A' and
{2O0GHO_C06_0GHO_T06_Q0002.[2O0GHO_C06-MEDIUM]} = 'GAS' and {2O0GHO_C06_0GHO_T06_Q0002.[2O0GHO_C06-QTY_TYPE]-[22O0GHO_C06-QTY_TYPE]} = '03'
then
qtyInventory := Sum ({2O0GHO_C06_0GHO_T06_Q0002.[Measures]-[DWQWE23XXYJCROQ978M8NX3J5]});
//sum for qty type Non Sales
numberVar qtyNonSales;
if
{2O0GHO_C06_0GHO_T06_Q0002.[2O0GHO_C06-THEO_ACT_IND]} = 'A' and
{2O0GHO_C06_0GHO_T06_Q0002.[2O0GHO_C06-MEDIUM]} = 'GAS' and {2O0GHO_C06_0GHO_T06_Q0002.[2O0GHO_C06-QTY_TYPE]-[22O0GHO_C06-QTY_TYPE]} = '04'
then
qtyNonSales := Sum ({2O0GHO_C06_0GHO_T06_Q0002.[Measures]-[DWQWE23XXYJCROQ978M8NX3J5]});
//sum for qty type Sales
numberVar qtySales;
if
{2O0GHO_C06_0GHO_T06_Q0002.[2O0GHO_C06-THEO_ACT_IND]} = 'A' and
{2O0GHO_C06_0GHO_T06_Q0002.[2O0GHO_C06-MEDIUM]} = 'GAS' and {2O0GHO_C06_0GHO_T06_Q0002.[2O0GHO_C06-QTY_TYPE]-[22O0GHO_C06-QTY_TYPE]} = '05'
then
qtySales := Sum ({2O0GHO_C06_0GHO_T06_Q0002.[Measures]-[DWQWE23XXYJCROQ978M8NX3J5]});
//sum for qty type Enhanced Recovery
numberVar qtyEnhancedRecovery;
if
{2O0GHO_C06_0GHO_T06_Q0002.[2O0GHO_C06-THEO_ACT_IND]} = 'A' and
{2O0GHO_C06_0GHO_T06_Q0002.[2O0GHO_C06-MEDIUM]} = 'GAS' and {2O0GHO_C06_0GHO_T06_Q0002.[2O0GHO_C06-QTY_TYPE]-[22O0GHO_C06-QTY_TYPE]} = '14'
then
qtyEnhancedRecovery := Sum ({2O0GHO_C06_0GHO_T06_Q0002.[Measures]-[DWQWE23XXYJCROQ978M8NX3J5]});
//sum for qty type Load Oil Sales
numberVar qtyLdOilSales;
if
{2O0GHO_C06_0GHO_T06_Q0002.[2O0GHO_C06-THEO_ACT_IND]} = 'A' and
{2O0GHO_C06_0GHO_T06_Q0002.[2O0GHO_C06-MEDIUM]} = 'GAS' and {2O0GHO_C06_0GHO_T06_Q0002.[2O0GHO_C06-QTY_TYPE]-[22O0GHO_C06-QTY_TYPE]} = '08'
then
qtyLdOilSales := Sum ({2O0GHO_C06_0GHO_T06_Q0002.[Measures]-[DWQWE23XXYJCROQ978M8NX3J5]});
//sum for qty type receipt
numberVar qtyReceipt;
if
{2O0GHO_C06_0GHO_T06_Q0002.[2O0GHO_C06-THEO_ACT_IND]} = 'A' and
{2O0GHO_C06_0GHO_T06_Q0002.[2O0GHO_C06-MEDIUM]} = 'GAS' and {2O0GHO_C06_0GHO_T06_Q0002.[2O0GHO_C06-QTY_TYPE]-[22O0GHO_C06-QTY_TYPE]} = '13'
then
qtyReceipt := Sum ({2O0GHO_C06_0GHO_T06_Q0002.[Measures]-[DWQWE23XXYJCROQ978M8NX3J5]});
numberVar Total_Production;
//summing all quantity types
Total_Production := qtyFuel + qtyInventory + qtyNonSales + qtySales + qtyEnhancedRecovery + qtyLdOilSales - qtyReceipt;
I want to display the Total_Production in the report.
The formula name is: Total_Gas_Production.
kindly help me in this issue.
Regards,
Deepakis this one formula?
have you tried to run each formula seperatly to check which is the culprit. if you search the web there are some reasons why this could be happening.
generally you should use evaluation times on formulas
are you using manual running totals or this is the entire formula?
try
MANUAL RUNNING TOTALS
RESET
The reset formula is placed in a group header report header to reset the summary to zero for each unique record it groups by.
whileprintingrecords;
Numbervar X := 0;
CALCULATION
The calculation is placed adjacent to the field or formula that is being calculated.
(if there are duplicate values; create a group on the field that is being calculated on. If there are not duplicate records, the detail section is used.
whileprintingrecords;
Numbervar X := x + ; ( or formula)
DISPLAY
The display is the sum of what is being calculated. This is placed in a group, page or report footer. (generally placed in the group footer of the group header where the reset is placed.)
whileprintingrecords;
Numbervar X;
X
Maybe you are looking for
-
MBP internal drive failure causes external drive probs. Weird...
I've been booting off an external drive for some months now. I figured it would save wear on the internal drive, and when my boot drive eventually failed (the external one) it would be quicker and a lot cheaper to swap that out with another external
-
Credit management configuration in Third party sales
Dear All, Please let me know , what are the configuration setting for Credit Management in Third Party Sales. Regards SKumar
-
Sort order column with a between filter
Hi all, [using OBI-EE version 10.1.3.3.1] I've got a column 'BLOCK_ROW' which contains values A,B,C....AA,AB,AC...BA,BB...FK,FL Alphabetically this orders A,AA,AB.... which is incorrect. The order should be A,B,C... To be able to sort correctly we ad
-
Won't install Photoshop and Acrobat Pro
My macbook recently crashed and I have had to reload my Adobe software onto a new hard drive. I am able to install everything except for Photoshop and Acrobat Pro. I've gone through all of the troubleshooting documents in regards to the exit error (6
-
Programs are slow loading..
Okay, so I'm posting this for my husband that is currently deployed and having issues with his macbook that he just purchased right before he left (Febuary). His computer is now giving him troubles with programs taking ages to load. He said that one