HAVING clause, or ORDER BY clause ....ERROR
hi i have following query which throws error plz find the solution for the query to go safe..
Select C.src_cd,
D.src_nm,
count(*) supp_clm_count,
(CASE WHEN A.clm_ttladjamt > 0 then A.clm_ttladjamt else A.clm_ttlreqamt END) as amount
From IWOWNER.WC_clm A,
IWOWNER.WC_clm_srvc B,
SHOWNER.WC_SRC C,
SHOWNER.WC_SRC_lang D
where A.clm_id = B.clm_id
and A.rcv_loc_id = C.src_id
and C.src_id = D.src_id
and TRANSLATE(A.clm_rqst_type_cd) = 'SUPPLIERCLAIM'
and TRANSLATE(A.clm_typ_cd) in ('WARRANTY','PRE-DELIVERY')
and DATE(A.clm_create_dt) between '01/01/2000' and '01/01/2010'
Group by C.src_cd, D.src_nm
ERROR:
SQLSTATE: 42803, SQLERRMC: CLM_TTLADJAMT
Message: An expression starting with "CLM_TTLADJAMT" specified in a SELECT clause, HAVING clause, or ORDER BY clause is not specified in the GROUP BY clause or it is in a SELECT clause, HAVING clause, or ORDER BY clause with a column function and no GROUP BY clause is specified
Hi,
With analytic function
/* Formatted on 2009/07/22 10:47 (Formatter Plus v4.8.8) */
SELECT DISTINCT c.src_cd, d.src_nm,
COUNT (1) OVER (PARTITION BY c.src_cd, d.src_nm)
supp_clm_count,
SUM (CASE
WHEN a.clm_ttladjamt > 0
THEN a.clm_ttladjamt
ELSE a.clm_ttlreqamt
END
) OVER (PARTITION BY c.src_cd, d.src_nm) AS amount
FROM iwowner.wc_clm a,
iwowner.wc_clm_srvc b,
showner.wc_src c,
showner.wc_src_lang d
WHERE a.clm_id = b.clm_id
AND a.rcv_loc_id = c.src_id
AND c.src_id = d.src_id
AND a.clm_rqst_type_cd = 'SUPPLIERCLAIM'
AND a.clm_typ_cd IN ('WARRANTY', 'PRE-DELIVERY')
AND a.clm_create_dt BETWEEN TO_DATE ('01/01/2000', 'dd/mm/yyyy')
AND TO_DATE ('01/01/2010', 'dd/mm/yyyy')
Similar Messages
-
HAVING clause error in JPA 2 examples
In Chapter 8: Query Language of the Pro JPA 2 Mastering the Java Persistence API book, the jpqlExamples WAR has this query:
SELECT e, COUNT(p)
FROM Employee e JOIN e.projects p
GROUP BY e
HAVING COUNT(p) >= 2
When executed, the following error occurs:
java.lang.IllegalArgumentException: An exception occurred while creating a query in EntityManager:
Exception Description: Error compiling the query [SELECT e, COUNT(p) FROM Employee e JOIN e.projects p GROUP BY e HAVING COUNT(p) >= 2], line 1, column 80: invalid HAVING expression [COUNT(p) >= 2] for query with grouping [GROUP BY e]. The HAVING clause must specify search conditions over the grouping items or aggregate functions that apply to grouping items.
I bring this us because I have an application which is getting the same error and need a fix. If the query is indeed legal in JPA 2, then why the error? If if it is my setup however, then I would like suggestions on fixing it. I am using GlassFish v3 (build 74.2), updated regularly with the Update Tool.The bug has been reopened. Now it says:
Reopening because there is some debate about whether this should be supported
by the spec. Some people read the spec to say the above query is allowed - I
am not convinced, but discussion can be appended to this bug if necessary.
This is Bug 308482, and I assume at least a few might want to take a look.
https://bugs.eclipse.org/bugs/show_bug.cgi?id=308482 -
Using column aliases in having clause error
This is the query I'm trying to run.
select concat(concat(student.last_name, ', '), student.first_name) student, count(school_apply.decision) decision
from student, school_apply
where student.student_id = school_apply.student_id
and school_apply.decision = 'accepted'
having decision < 3
group by student.last_name, student.first_name
order by count(school_apply.decision) desc, student.last_name, student.first_name;
I got the following error
Error at Command Line:5 Column:7
Error report:
SQL Error: ORA-00979: not a GROUP BY expression
00979. 00000 - "not a GROUP BY expression"
if in the having I use count(school_apply.decision) instead of the alias, it works fine. Why is this? and how can I use the alias'd column in the SQL?Hi,
user4038196 wrote:
This is the query I'm trying to run.
select concat(concat(student.last_name, ', '), student.first_name) student, count(school_apply.decision) decision
from student, school_apply
where student.student_id = school_apply.student_id
and school_apply.decision = 'accepted'
having decision < 3
group by student.last_name, student.first_name
order by count(school_apply.decision) desc, student.last_name, student.first_name;
I got the following error
Error at Command Line:5 Column:7
Error report:
SQL Error: ORA-00979: not a GROUP BY expression
00979. 00000 - "not a GROUP BY expression"
if in the having I use count(school_apply.decision) instead of the alias, it works fine. Why is this? and how can I use the alias'd column in the SQL?When you define a column alias, like decision, in a query, you can use it in the ORDER BY clause. That's the only place in that query where you can use it.
You can always assign the alias in a sub-query, then use it anywhere you want to in a super-query, like this:
WITH got_aggregates AS
select student.last_name || ', '
|| student.first_name AS student
, count (school_apply.decision) AS decision
from student
, school_apply
where student.student_id = school_apply.student_id
and school_apply.decision = 'accepted'
group by student.last_name
, student.first_name
SELECT *
FROM got_aggregates
WHERE decision < 3 -- Okay to use alias from sub-query
ORDER BY decision DESC -- Okay to use alias from sub-query
, student -- Okay to use alias from sub-query
; -
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 -
Urgent Group by Having Clause Fails in Oracle and Works in Sybase
Hello EveryBody I need to select curve data of curve whose date is maximum in that curve so say if i ahve
so if records are
curveid curvename curvedate
1001 test1 12/12/1003
1001 test1 12/13/1003
1002 test2 12/12/2002
1002 test2 12/12/2004
I have query which run well in sybase but oracle screw up...My logic say having clause is use to filter the records of group so it should have worked in oracle.....
Here is query
select curveid,curvename from curve group by curveid having curvedate =max(curve_date)
This give "not a Group by " error in oracle....It work well in sybase...
ORA-00979: not a GROUP BY expression
I have query which use subquery to select these records but i don't want that to use that query
Please helpplease understand what I need the result
for each curve i need that data of that curve who date is max in that curve group
so say for 1001 there are two date 1/1/2001 and 1/1/2002
I need the data of curve 1001 with date 1/1/2002
Oracle should have some alternative solution for this....
If i have to use subquery I do have subquery
select a.curveid, b.curvename from curve a group by curveid having curvedate = (select max(curvedate) from curve where a.curveid=b.curveid group by curveid );
I don't want to use that ,,,
I want to solve my probelm using having and group/....
Main purpose of having is to filter the records from group result so IT SHOULD DO THAT WHY ORACLE GIVE ORA ERROR FOR
group by curve_id having curve_date=max(curve_date)
Creators of oracle please reply -
Use of Where and having clause
Hi all,
I always have a doubt about use of HAVING and WHERE clause,
suppose I have table T1 with only one column C1
CREATE TABLE T1
(C1 VARCHAR2(1) );
which having data by following INSERT scripts
INSERT INTO T1 VALUES('A');
INSERT INTO T1 VALUES('B');
INSERT INTO T1 VALUES('C');
INSERT INTO T1 VALUES('A');
INSERT INTO T1 VALUES('B');
INSERT INTO T1 VALUES('A');
Now I want result as follows
C1 ==== COUNT(C1)
==============
B ===== 2
A ===== 3
So out of query 1 and 2 which approach is right ?
1) SELECT C1,COUNT(C1) FROM T1
WHERE C1<>'C'
GROUP BY C1
ORDER BY C1 DESC;
2) SELECT C1,COUNT(C1) FROM T1
GROUP BY C1
HAVING C1<>'C'
ORDER BY C1 DESC;
Edited by: user13306874 on Jun 21, 2010 2:36 AMIn SQL, it's always best to filter data at the earliest moment possible.
In your example the WHERE clause would be that moment:
SQL> explain plan for
2 select c1,count(c1)
3 from t1
4 where c1 != 'C'
5 group by c1
6* order by c1 desc;
Explained.
SQL> select * from table(dbms_xplan.display);
PLAN_TABLE_OUTPUT
Plan hash value: 3946799371
| Id | Operation | Name | Rows | Bytes |
| 0 | SELECT STATEMENT | | 5 | 10 |
| 1 | SORT GROUP BY | | 5 | 10 |
|* 2 | TABLE ACCESS FULL| T1 | 5 | 10 |
Predicate Information (identified by operation id):
2 - filter("C1"!='C')
18 rows selected.
SQL>As you can see the filter is applied during the scan of T1.
Whereas in the HAVING case:
SQL> explain plan for
2 select c1,count(c1)
3 from t1
4 group by c1
5 having c1 != 'C'
6* order by c1 desc;
Explained.
SQL> select * from table(dbms_xplan.display);
PLAN_TABLE_OUTPUT
Plan hash value: 3146800528
| Id | Operation | Name | Rows | Bytes |
| 0 | SELECT STATEMENT | | 6 | 12 |
|* 1 | FILTER | | | |
| 2 | SORT GROUP BY | | 6 | 12 |
| 3 | TABLE ACCESS FULL| T1 | 6 | 12 |
Predicate Information (identified by operation id):
1 - filter("C1"!='C')
18 rows selected.
SQL>The scan is done after all groups have been computed: one of which was computed in vain, since it will be filtered away due to the HAVING clause.
In general I would use as a guideline: if you are not using aggregate functions in your HAVING clause predicate, then move that predicate to the WHERE portion of your query.
Edited by: Toon Koppelaars on Jun 21, 2010 11:54 AM -
Having clause with Analytic function
can you pls let me know if we can use HAVING clause with analytic function
select eid,empno,sum(sal) over(partition by year)
from employee
where dept = 'SALES'
having sum(sal) > 10000I m getting error while using the above,
IS that we can use HAVING clause with partition by
Thanks in advanceYour having clause isn't using an analytical function, is using a regular aggregate function.
You also can't use analytical functions in the where clause or having clause like that as they are windowing functions and belong at the top of the query.
You would have to wrap the query to achieve what you want e.g.
SQL> ed
Wrote file afiedt.buf
1 select deptno, total_sal
2 from (
3 select deptno,sum(sal) over (partition by deptno) as total_sal
4 from emp
5 )
6 group by deptno, total_sal
7* having total_sal > 10000
SQL> /
DEPTNO TOTAL_SAL
20 10875
SQL> -
I feel like this should be simple but it I'm just not grasping it.
Say I have this query:
SELECT order_number, order_date FROM orders
I basically want to pull any order from the last 24 hours. Well this is fairly easy for Tue-Fri but when I run the query on Mon, it will have zero results since there are no orders from the weekend. If the query is ran on Mon it should query
Fri instead.
I'm attempting to use case in my having clause but no luck.
HAVING (DATEDIFF(dd, GETDATE(), order_date = (CASE order_date WHEN DATENAME(weekday,getdate()) = 'Friday' THEN '3' ELSE '0' END)
Sure my syntax or parentheses is way off, any help is appreciated. Thanks.Check out the following CASE expression:
SELECT CASE WHEN datename(dw,getdate()) = 'Monday' THEN
CONVERT(DATE,(SELECT dateadd(d, -((datepart(weekday, getdate()) + 1 + @@DATEFIRST) % 7), getdate())))
ELSE CONVERT(DATE, DATEADD(dd, -1, getdate())) END;
/********* TEST ****************/
DECLARE @Dt datetime;
DECLARE @i int = 0; WHILE (@i < 10) BEGIN
SET @Dt = dateadd(dd, @i, getdate());
SELECT Today = @Dt, WeekDay=DATENAME(dw,@Dt), PrevBusinessDay= CASE WHEN datename(dw,@Dt) = 'Monday' THEN
CONVERT(DATE, (SELECT dateadd(d, -((datepart(weekday, @Dt) + 1 + @@DATEFIRST) % 7), @Dt)))
ELSE CONVERT(DATE, DATEADD(dd, -1, @Dt)) END;
SET @i += 1; END -- WHILE
Today WeekDay PrevBusinessDay
2014-06-20 19:46:45.130 Friday 2014-06-19
Today WeekDay PrevBusinessDay
2014-06-23 19:49:04.817 Monday 2014-06-20
Today WeekDay PrevBusinessDay
2014-06-24 19:46:45.130 Tuesday 2014-06-23
Today WeekDay PrevBusinessDay
2014-06-25 19:46:45.130 Wednesday 2014-06-24
Today WeekDay PrevBusinessDay
2014-06-26 19:46:45.130 Thursday 2014-06-25
Today WeekDay PrevBusinessDay
2014-06-27 19:46:45.130 Friday 2014-06-26
Datetime functions:
http://www.sqlusa.com/bestpractices/datetimeconversion/
Consider also implementation with a calendar table.
Kalman Toth Database & OLAP Architect
SQL Server 2014 Design & Programming
New Book / Kindle: Exam 70-461 Bootcamp: Querying Microsoft SQL Server 2012 -
Group By -- Having Clause related doubt.
Hello,
Can we Write/Use rather a 'Having Condition' Before a Group by Clause ..?
If Yes then How does it Work.. I mean a 'Having' is a WHERE clause (filter) on Aggregate results ...
SO how does Having works before grouping the Results..??Hi,
Aijaz Mallick wrote:
Hello,
Can we Write/Use rather a 'Having Condition' Before a Group by Clause ..?What happens when you try it?
If Yes then How does it Work.. I mean a 'Having' is a WHERE clause (filter) on Aggregate results ... Right; the HAVING clause is like another WHERE clause.
The WHERE clause is applied before the GROUP BY is done, and the aggregate functions are computed.
The HAVING clause is applied after the GROUP BY is done, and the aggregate functions are computed, so you can use aggregate functions in the HAVING clause.
SO how does Having works before grouping the Results..??The order in which clauses appear in your code isn't necessarily the order in which they are performed. For example,
SELECT job
, COUNT (*) AS cnt
FROM scott.emp
GROUP BY job;Does it confuse you that this query can reference COUNT (*) in the SLECT clause, which is before the GROUP BY clause?
The SELECT clause which always comes before the GROUP BY clause in code. That does not mean that the SELECT clause is completed before the GROUP BY clause is begun.
If the documentation says that clauses must be in a certain order, then use that order, even if your current version of Oracle allows them to be in a different order. There's no guarantee that the next version of Oracle will allow something that was always said to be wrong. -
Hello all,
i'm getting error when i try to write sub query in having clause. check out my query
select ROUND( Count( distinct Sales2011_DIVDPTA.DIVDPTA_Item_Dept ),0) AS "F1" , ROUND( Count( Sales2011.Sales2011_DG1.Item_Season ),0) AS "F2"
from Sales2011.Sales2011_JT
INNER JOIN Sales2011.Sales2011_DG1 On Sales2011.Sales2011_DG1.DG1_ID =Sales2011.Sales2011_JT.DG1_ID
LEFT JOIN Sales2011.Sales2011_DIVDPTA On nvl(Sales2011.Sales2011_DIVDPTA.DIVDPTA_ITEM_DIVISION,' ')=nvl(Sales2011.Sales2011_DG1.Item_Division,' ')
AND nvl(Sales2011.Sales2011_DIVDPTA.DIVDPTA_ITEM_DEPT,' ')=nvl(Sales2011.Sales2011_DG1.Item_Dept,' ')
having ( ROUND( Count( Sales2011.Sales2011_DG1.Item_Season ),0) in ( 0)
But it is not executed if I use the sub query in having clause
select ROUND( Count( distinct Sales2011_DIVDPTA.DIVDPTA_Item_Dept ),0) AS "F1" , ROUND( Count( Sales2011.Sales2011_DG1.Item_Season ),0) AS "F2"
from Sales2011.Sales2011_JT
INNER JOIN Sales2011.Sales2011_DG1 On Sales2011.Sales2011_DG1.DG1_ID =Sales2011.Sales2011_JT.DG1_ID
LEFT JOIN Sales2011.Sales2011_DIVDPTA On nvl(Sales2011.Sales2011_DIVDPTA.DIVDPTA_ITEM_DIVISION,' ')=nvl(Sales2011.Sales2011_DG1.Item_Division,' ')
AND nvl(Sales2011.Sales2011_DIVDPTA.DIVDPTA_ITEM_DEPT,' ')=nvl(Sales2011.Sales2011_DG1.Item_Dept,' ')
having ( ROUND( Count( Sales2011.Sales2011_DG1.Item_Season ),0)
in ( select ROUND( Count(
Sales2011.Sales2011_DG1.Item_Season ),0) from Sales2011.Sales2011_DG1 )
Error at Command Line:1 Column:0
Error report:
SQL Error: ORA-00979: not a GROUP BY expression
00979. 00000 - "not a GROUP BY expression"
*Cause:
*Action:any help ???Sorry unintentionally i have posted my question here.
will you please elaborate this? Becoz i'm not using group by clause in both query. First query run successfully but as i put sub query in having clause it raised an error. will you tell where i'm committing mistake?
Aggregates in the HAVING clause do not need to appear in the SELECT list. If the HAVING clause contains a subquery, the subquery can refer to the outer query block if and only if it refers to a grouping column.Edited by: Oracle Studnet on Aug 14, 2011 11:28 PM -
I have a test data, like this
with t as (
select 1 tp, 0 sm_1, 1 sm_2, 'a' code from dual
union all
select 1 tp, 0 sm_1, 2 sm_2, 'b' from dual
union all
select 2 tp, 3 sm_1, 0 sm_2, 'c' from dual
union all
select 3 tp, 0 sm_1, 2 sm_2, 'a' from dual
union all
select 4 tp, 0 sm_1, 1 sm_2, 'a' from dual
union all
select 4 tp, 2 sm_1, 3 sm_2,'b' from dual
union all
select 4 tp, 2 sm_1, 3 sm_2, 'c' from dual
union all
select 5 tp, 0 sm_1, 0 sm_2, 'a' from dual
union all
select 5 tp, 0 sm_1, 0 sm_2, 'b' from dual
union all
select 5 tp, 0 sm_1, 0 sm_2, 'c' from dual)
SQL> with t as (
2 select 1 tp, 0 sm_1, 1 sm_2, 'a' code from dual
3 union all
4 select 1 tp, 0 sm_1, 2 sm_2, 'b' from dual
5 union all
6 select 2 tp, 3 sm_1, 0 sm_2, 'c' from dual
7 union all
8 select 3 tp, 0 sm_1, 2 sm_2, 'a' from dual
9 union all
10 select 4 tp, 0 sm_1, 1 sm_2, 'a' from dual
11 union all
12 select 4 tp, 2 sm_1, 3 sm_2,'b' from dual
13 union all
14 select 4 tp, 2 sm_1, 3 sm_2, 'c' from dual
15 union all
16 select 5 tp, 0 sm_1, 0 sm_2, 'a' from dual
17 union all
18 select 5 tp, 0 sm_1, 0 sm_2, 'b' from dual
19 union all
20 select 5 tp, 0 sm_1, 0 sm_2, 'c' from dual)
21 select tp, SUM(sm_1), SUM(sm_2), code from t
22 group by tp, code
23 /
TP SUM(SM_1) SUM(SM_2) CODE
1 0 1 a
1 0 2 b
2 3 0 c
3 0 2 a
4 0 1 a
4 2 3 b
4 2 3 c
5 0 0 a
5 0 0 b
5 0 0 c
10 rows selected
SQL>
And I want to get this result
TP SUM(SM_1) SUM(SM_2) CODE
1 0 1 a
1 0 2 b
2 3 0 c
3 0 2 a
4 0 1 a
4 2 3 b
4 2 3 c
5 0 0 a
E.g. if SM_1 = SM_2(all of the rows) In group of tp, then return random rows with tp = 5, if
5 0 0 c
5 1 2 a
5 3 1 b
then return
5 1 2 a
5 3 1 b
And I don't undersatnd how it's do with Having Clause.
Thank you!SQL> with t as (
2 select 1 tp, 0 sm_1, 1 sm_2, 'a' code from dual
3 union all
4 select 1 tp, 0 sm_1, 2 sm_2, 'b' from dual
5 union all
6 select 2 tp, 3 sm_1, 0 sm_2, 'c' from dual
7 union all
8 select 3 tp, 0 sm_1, 2 sm_2, 'a' from dual
9 union all
10 select 4 tp, 0 sm_1, 1 sm_2, 'a' from dual
11 union all
12 select 4 tp, 2 sm_1, 3 sm_2,'b' from dual
13 union all
14 select 4 tp, 2 sm_1, 3 sm_2, 'c' from dual
15 union all
16 select 5 tp, 0 sm_1, 0 sm_2, 'a' from dual
17 union all
18 select 5 tp, 0 sm_1, 0 sm_2, 'b' from dual
19 union all
20 select 5 tp, 0 sm_1, 0 sm_2, 'c' from dual)
21 select tp, sm_1, sm_2, code
22 from (select tp,
23 SUM(sm_1) sm_1,
24 SUM(sm_2) sm_2,
25 code,
26 count(*) over (partition by tp) ct_tp,
27 count(*) over (partition by tp, SUM(sm_1), SUM(sm_2)) ct,
28 row_number() over (partition by tp order by null) rn
29 from t
30 group by tp, code)
31 where decode(ct,ct_tp,rn,1)=1
32 and case when sm_1 = 0 and sm_2 = 0 and ct_tp-ct > 0 then 0 else 1 end = 1;
TP SM_1 SM_2 C
1 0 1 a
1 0 2 b
2 3 0 c
3 0 2 a
4 0 1 a
4 2 3 b
4 2 3 c
5 0 0 a
8 rows selected.
SQL> with t as (
2 select 1 tp, 0 sm_1, 1 sm_2, 'a' code from dual
3 union all
4 select 1 tp, 0 sm_1, 2 sm_2, 'b' from dual
5 union all
6 select 2 tp, 3 sm_1, 0 sm_2, 'c' from dual
7 union all
8 select 3 tp, 0 sm_1, 2 sm_2, 'a' from dual
9 union all
10 select 4 tp, 0 sm_1, 1 sm_2, 'a' from dual
11 union all
12 select 4 tp, 2 sm_1, 3 sm_2,'b' from dual
13 union all
14 select 4 tp, 2 sm_1, 3 sm_2, 'c' from dual
15 union all
16 select 5 tp, 0 sm_1, 0 sm_2, 'a' from dual
17 union all
18 select 5 tp, 1 sm_1, 0 sm_2, 'b' from dual
19 union all
20 select 5 tp, 2 sm_1, 0 sm_2, 'c' from dual)
21 select tp, sm_1, sm_2, code
22 from (select tp,
23 SUM(sm_1) sm_1,
24 SUM(sm_2) sm_2,
25 code,
26 count(*) over (partition by tp) ct_tp,
27 count(*) over (partition by tp, SUM(sm_1), SUM(sm_2)) ct,
28 row_number() over (partition by tp order by null) rn
29 from t
30 group by tp, code)
31 where decode(ct,ct_tp,rn,1)=1
32 and case when sm_1 = 0 and sm_2 = 0 and ct_tp-ct > 0 then 0 else 1 end = 1;
TP SM_1 SM_2 C
1 0 1 a
1 0 2 b
2 3 0 c
3 0 2 a
4 0 1 a
4 2 3 b
4 2 3 c
5 1 0 b
5 2 0 c
9 rows selected.Nicolas. -
Hi All,
Current I am working on a Discoverer report modification,
Curent report query i ahve taken from View-Sql Inspector that is mentioned below(Sample).
Here i wanted to change the Having condition of the below query through the discoverer tool.
Can any one help me in this regard?
SELECT (bolinf.xxbt_disco_common_pkg.get_sitelevel_coll (o222236.customer_number)) AS c_18
, o222241.segment1 || '-' || o222241.segment2 || '-' || o222241.segment3 || '-' || o222241.segment4 || '-' || o222241.segment5 || '-' || o222241.segment6 || '-' || o222241.segment7 || '-'
|| o222241.segment8 || '-' || o222241.segment9 || '-' || o222241.segment10 AS c_19
,bolinf.xxbt_disco_common_pkg.get_rev_lob (o222245.gl_id_rev) AS c_22
, (bolinf.xxbt_disco_common_pkg.get_siebel_account_code (o222245.gl_id_rev)) AS c_3
, (bolinf.xxbt_disco_common_pkg.get_siebel_account_name (o222245.gl_id_rev)) AS c_4
,o222246.city AS e222753
,o222236.customer_category_code AS e222864
,o222236.customer_class_code AS e222869
,o222236.customer_name AS e222881
,o222236.customer_number AS e222885
,o222239.due_date AS e222988
,o222245.fob_point AS e223035
,o222239.invoice_currency_code AS e223477
,o222236.profile_class_name AS e223794
,o222239.staged_dunning_level AS e224084
,o222239.trx_date AS e224181
,o222239.trx_number AS e224185
,SUM (CASE
FROM ar.ar_collectors o222235
,apps.ar_customers_v o222236
,apps.ar_payment_schedules o222239
,gl.gl_code_combinations o222241
,ar.hz_cust_acct_sites_all o222243
,ar.hz_cust_site_uses_all o222245
,ar.hz_locations o222246
,ar.hz_party_sites o222247
,ar.ra_cust_trx_line_gl_dist_all o222251
, (SELECT lv.lookup_type
,lv.lookup_code
,lv.meaning
,lv.enabled_flag
,lv.start_date_active
,lv.end_date_active
FROM fnd_lookup_values lv
WHERE lv.LANGUAGE = USERENV ('LANG')
AND lv.view_application_id = 222
AND lv.security_group_id = 0
AND lv.lookup_type = 'INV/CM') o410858
,ar.hz_customer_profiles o415961
, (SELECT crm.customer_id
,crm.primary_flag
,crm.site_use_id
,rm.NAME "Payment Method Name"
,haou.NAME
,l_cat.meaning "Account Manager"
FROM ra_cust_receipt_methods crm
,ar_receipt_methods rm
,hz_cust_site_uses_all hcs
,hz_cust_acct_sites_all hcasa
,hr_all_organization_units haou
,ar_lookups l_cat
WHERE crm.receipt_method_id = rm.receipt_method_id(+)
AND hcs.site_use_id = crm.site_use_id
AND haou.organization_id = hcs.org_id
AND hcasa.cust_account_id = crm.customer_id
AND hcasa.cust_acct_site_id(+) = hcs.cust_acct_site_id
AND hcasa.customer_category_code = l_cat.lookup_code(+)
AND l_cat.lookup_type(+) = 'ADDRESS_CATEGORY'
AND haou.NAME NOT LIKE '%OLD%') o681593
WHERE ( (o222236.customer_id = o222239.customer_id)
AND (o222241.code_combination_id = o222251.code_combination_id)
AND (o222243.cust_acct_site_id(+) = o222245.cust_acct_site_id)
AND (o222245.site_use_id(+) = o222239.customer_site_use_id)
AND (o222246.location_id(+) = o222247.location_id)
AND (o222247.party_site_id(+) = o222243.party_site_id)
AND (o222251.customer_trx_id(+) = o222239.customer_trx_id)
AND (o410858.lookup_code = o222239.CLASS)
AND (o222235.collector_id = o415961.collector_id)
AND (o415961.cust_account_id = o222236.customer_id)
AND (o681593.site_use_id = o222245.site_use_id)
AND (o415961.site_use_id IS NULL)
AND (o222251.account_set_flag(+) = 'N')
AND o222239.trx_number ='29801276'
--AND (o222235.NAME = :"Collector Name")
--AND (o222236.profile_class_name = :"Profile Class Name")
--AND (( o222236.customer_category_code = :"Customer Category"
-- OR :"Customer Category" IS NULL))
--AND (( o222236.customer_class_code = :"Customer Class"
-- OR :"Customer Class" IS NULL))
--AND (o222239.invoice_currency_code = :"Currency")
AND ((DECODE ('Y', 'Y', NVL (o222239.amount_due_remaining * NVL (o222239.exchange_rate, 1), 0), NVL (o222239.amount_due_remaining, 0))) 0)
AND (o222251.account_class(+) = 'REC')
GROUP BY (bolinf.xxbt_disco_common_pkg.get_sitelevel_coll (o222236.customer_number))
, o222241.segment1 || '-' || o222241.segment2 || '-' || o222241.segment3 || '-' || o222241.segment4 || '-' || o222241.segment5 || '-' || o222241.segment6 || '-' || o222241.segment7 || '-'
|| o222241.segment8 || '-' || o222241.segment9 || '-' || o222241.segment10
,bolinf.xxbt_disco_common_pkg.get_rev_lob (o222245.gl_id_rev)
, (bolinf.xxbt_disco_common_pkg.get_siebel_account_code (o222245.gl_id_rev))
, (bolinf.xxbt_disco_common_pkg.get_siebel_account_name (o222245.gl_id_rev))
,o222246.city
,o222236.customer_category_code
,o222236.customer_class_code
,o222236.customer_name
,o222236.customer_number
,o222239.due_date
,o222245.fob_point
,o222239.invoice_currency_code
,o222236.profile_class_name
,o222239.staged_dunning_level
,o222239.trx_date
,o222239.trx_number
,o410858.meaning
,o681593.primary_flag
,o681593."Payment Method Name"
,o681593."Account Manager"
HAVING ((((SUM (CASE
WHEN (TO_DATE ('30-APR-2011', 'DD-MON-YYYY') - o222239.due_date) BETWEEN 1 AND 30 THEN (DECODE ('Y'
,'Y', NVL (o222239.amount_due_remaining * NVL (o222239.exchange_rate, 1), 0)
,NVL (o222239.amount_due_remaining, 0)
ELSE 0
END)
+ (SUM (CASE
WHEN (TO_DATE ('30-APR-2011', 'DD-MON-YYYY') - o222239.due_date) BETWEEN 31 AND 60 THEN (DECODE ('Y'
,'Y', NVL (o222239.amount_due_remaining * NVL (o222239.exchange_rate, 1), 0)
,NVL (o222239.amount_due_remaining, 0)
ELSE 0
END)
+ (SUM (CASE
WHEN (TO_DATE ('30-APR-2011', 'DD-MON-YYYY') - o222239.due_date) BETWEEN 61 AND 90 THEN (DECODE ('Y'
,'Y', NVL (o222239.amount_due_remaining * NVL (o222239.exchange_rate, 1), 0)
,NVL (o222239.amount_due_remaining, 0)
ELSE 0
END)
+ (SUM (CASE
WHEN (TO_DATE ('30-APR-2011', 'DD-MON-YYYY') - o222239.due_date) BETWEEN 91 AND 120 THEN (DECODE ('Y'
,'Y', NVL (o222239.amount_due_remaining * NVL (o222239.exchange_rate, 1), 0)
,NVL (o222239.amount_due_remaining, 0)
ELSE 0
END)
+ (SUM (CASE
WHEN (TO_DATE ('30-APR-2011', 'DD-MON-YYYY') - o222239.due_date) BETWEEN 121 AND 150 THEN (DECODE ('Y'
,'Y', NVL (o222239.amount_due_remaining * NVL (o222239.exchange_rate, 1), 0)
,NVL (o222239.amount_due_remaining, 0)
ELSE 0
END)
+ (SUM (CASE
WHEN (TO_DATE ('30-APR-2011', 'DD-MON-YYYY') - o222239.due_date) BETWEEN 151 AND 180 THEN (DECODE ('Y'
,'Y', NVL (o222239.amount_due_remaining * NVL (o222239.exchange_rate, 1), 0)
,NVL (o222239.amount_due_remaining, 0)
ELSE 0
END)
+ (SUM (CASE
WHEN (TO_DATE ('30-APR-2011', 'DD-MON-YYYY') - o222239.due_date) BETWEEN 181 AND 365 THEN (DECODE ('Y'
,'Y', NVL (o222239.amount_due_remaining * NVL (o222239.exchange_rate, 1), 0)
,NVL (o222239.amount_due_remaining, 0)
ELSE 0
END)
+ (SUM (CASE
WHEN (TO_DATE ('30-APR-2011', 'DD-MON-YYYY') - o222239.due_date) < 1 THEN (DECODE ('Y'
,'Y', NVL (o222239.amount_due_remaining * NVL (o222239.exchange_rate, 1), 0)
,NVL (o222239.amount_due_remaining, 0)
ELSE 0
END)
) <> 0
ORDER BY e222881 ASC;Duplicate post -- Having Clause In Discoverer
-
Why Scalar Subquery expression cannot be used in HAVING clauses?
Hi All,
I'm new to SQL. I'm confused with Scalar Subquery.
Is there anyone who can answer me why Scalar Subquery expression cannot be used in HAVING clauses.
Can you show me a example?
Thanks very much,
Xianyi.Ye
Edited by: 908428 on 2012-1-16 下午7:24Hi,
908428 wrote:
Hi Frank,
Thank you for your quick reply. I also agree with your point.
But when I read the book, "OCA Oracle Database SQL Certified Expert Exam Guide",( link to illegal copies of book removed by moderator )
on Page 359, it said that
Scalar subquery expressions cannot be used in the following locations:
1. In CHECK constraints
2. In GROUP BY clauses
3. In HAVING clauses
4. In a function-based index (which is coming up in Chapter 11)
5. As a DEFAULT value for a column
6. In the RETURNING clause of any DML statement
7. In the WHEN conditions of CASE
8. In the START WITH and CONNECT BY clauses, which we discuss in
Chapter 16.7 and 8 are wrong. Scalar sub-queries can be used in WHEN conditions and START WITH and CONNECT BY clauses. (At least in Oracle 10.2. Is the book based on some earlier version?)
Edited by: BluShadow on 17-Jan-2012 09:08 -
Which two statements are true about WHERE and HAVING clause ?
Which two statements are true about WHERE and HAVING clause ?
1. WHERE clause can be used to restict rows only
2.HAVING clause can be used to restrict groups only
3.HAVING clause can be used to restrict groups and rows
Plz help me in dis ques...which two will be correct...i think its 1 and 2...but not sure.863180 wrote:
Plz help me in dis ques...which two will be correct...i think its 1 and 2...but not sure.If you are not sure then you do not fully understand HAVING.
SY. -
Select stmts with Having Clause
Hi,
Can some body help me to improve the performance of the below query...
select t.seq_no,t.contract_id,t.date_from from test_plan_tab t
group by t.seq_no,t.contract_id,t.date_from
having (select count(*)
from test_plan_tab p
where p.contract_id = t.contract_id
and p.date_from = t.date_from) > 1
The having clause will reduce the performance of the above query I guess...
Thanks And Best Regards,
/Dinesh...If Seq_no is unique then try this
SELECT MIN(t.seq_no),contract_id,t.date_from,count(*) from test_plan_tab t
group by t.contract_id,t.date_from
HAVING COUNT(*) > 1or
you can try as fsitja said, but you can omit the group by as the partition has already been done on grouping columns
SELECT seq_no, contract_id, date_from
FROM (SELECT t.seq_no, t.contract_id, t.date_from, COUNT(*) over(PARTITION BY contract_id, date_from) tot
FROM test_plan_tab t)
WHERE tot > 1Regards,
Prazy
Maybe you are looking for
-
Problem with Duplicate Movie Clip which is tracked in an Array
I have a problem with my inventory code using Shared Objects. What i did was to track the collected items in an array. Each time the user collects an item, the original iconMC is duplicated and loads a picture of the item (function loadImage). Howeve
-
hi everyone, first time using this forum! I have an old iphone 3 and for Xmas received an iphone 5. I have 2 apple ID accounts, one on each phone. For me to be able to transfer messages, fotos, contacts etc from the iphone 3 to 5 do I have to be usin
-
Receiver JDBC CCerror "Could not do TCP/IP Connection to host Manufactured"
Hi, I want to connect to MS SQL server 2000 Database from my PI 7.1 EHP1 server. Basis team has installed MS SQL 2005 drivers in PI system by downloading it from the below link http://www.microsoft.com/downloads/details.aspx?displaylang=en&FamilyID=c
-
Can a drop down list item be multi line?
got (another) problem. Client has asked me to add a new drop down list of reference codes. Some of these codes are very long - the biggest one reaching 3 lines A4 width. So testing it as a pdf I notice that anything that goes beyond the visual area o
-
How to change MRP Type in mass ?
Hi experts, I want to change MRP type (MDMA-DISMM) in MRP 1 view of material master. I checked in Tcode MM17 , but this field is not available for mass maintenance. How to change it in mass? Thanks in advance. Regards, Jay