Problem in using aggregate functions inside case statement
Hi All,
I am facing problem while using aggregate functions inside case statement.
CASE WHEN PSTYPE='S' THEN MAX(DECODE(POS.PBS,1,ABS(POS.PPRTQ),0)) ELSE SUM(DECODE(POS.PBS,1,ABS(POS.PPRTQ),0)) END,
how can I achieve above requirement ? Con anyone help me.
Thanks and Regards
DG
Hi All,
Below is my query:
SELECT
CASE WHEN p_reportid IN ('POS_RV_SN','POS_PB') THEN POS.PACCT
ELSE POS.PACCT || '-' || DECODE(POS.SYSTEMCODE,'GMI1','1', 'GMI2','2', 'GMI3','4', 'GMI4','3', '0') ||POS.PFIRM|| NVL(POS.POFFIC,'000') END,
CASE WHEN p_reportid IN ('POS_RV_SN','POS_PB') THEN POS.PACCT||POS.PCUSIP||DECODE(POS.PBS,1,'+',2,'-')
ELSE POS.PFIRM||POS.POFFIC||POS.PACCT||POS.PCUSIP||DECODE(POS.PBS,1,'+',2,'-') END,POS.SYSTEMCODE,CASE WHEN POS.PSTYPE='S' THEN POS.PSYMBL ELSE POS.PFC END,POS.PEXCH||DECODE(POS.PSUBEX,'<NULL>',''),
POS.PCURSY,
CASE WHEN POS.PSBCUS IS NULL THEN SUBSTR(POS.PCTYM,5,2) || SUBSTR(POS.PCTYM,1,4) ELSE POS.PSBCUS || SUBSTR(POS.PCTYM,5,2) || SUBSTR(POS.PCTYM,1,4) END ,
NVL(POS.PSUBTY,'F') ,POS.PSTRIK,*SUM(DECODE(POS.PBS,1,ABS(POS.PPRTQ),0)) ,SUM(DECODE(POS.PBS,2,ABS(POS.PPRTQ),0))* ,
POS.PCLOSE,SUM(POS.PMKVAL) ,
TO_CHAR(CASE WHEN INSTR(POS.PUNDCP,'.') > 0 OR LENGTH(POS.PUNDCP) < 15 THEN POS.PUNDCP ELSE TO_CHAR(TO_NUMBER(POS.PUNDCP) / 100000000) END),
POS.UBS_ID,POS.BBG_EXCHANGE_CODE,POS.BBG_TICKER ,POS.BBG_YELLOW_KEY,POS.PPCNTY,POS.PMULTF,TO_CHAR(POS.BUSINESS_DATE,'YYYYMMDD'),
POS.SOURCE_GMI_LIB,
--DECODE(POS.SYSTEMCODE,'GMI1','euro','GMI2','namr','GMI3','aust','GMI4','asia','POWERBASE','aust','SINACOR','namr',POS.SYSTEMCODE),
DECODE(p_reportid,'RVPOS_SING','euro','RVPOS_AUSTDOM','aust','RVPOS_AUSTEOD','euro','RVPOS_GLBLAPAC','asia','POS_RV_SN','namr','POS_PB','aust',POS.SYSTEMCODE),
POS.RIC,
CASE WHEN PSUBTY = 'S' THEN POS.TYPE ELSE NULL END,
DECODE(POS.UBS_ID,NULL,POS.PCUSP2,POS.ISIN),POS.UNDERLYING_BBG_TICKER,POS.UNDERLYING_BBG_EXCHANGE,POS.PRODUCT_CLASSIFICATION,
CASE WHEN PSUBTY = 'S' THEN POS.PSDSC2 ELSE NULL END,
CASE WHEN PSUBTY = 'S' THEN C.SSDSC3 ELSE NULL END,
NVL(C.SSECID,POS.PCUSIP),
NULL,
POS.PYSTMV,
POS.PMINIT,
POS.PEXPDT,
CASE WHEN POS.PSUBTY='S' THEN SUBSTR(C.ZDATA2,77,1) ELSE NULL END,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL
FROM POSITIONS_WRK POS LEFT OUTER JOIN
(SELECT * FROM CDS_PRODUCTS CP INNER JOIN FUTURE_MASTER FM ON
(CP.STRXCH=FM.ZEXCH AND CP.SFC=FM.ZFC AND CP.BUSINESS_DATE = FM.BUSINESS_DATE )) C ON POS.PCUSIP = C.SCUSIP
AND NVL(POS.PCUSP2,'X') = NVL(C.SCUSP2,'X')
WHERE
POS.PEXCH NOT IN ('A1','A2','A3','B1','B3','C2','D1','H1','K1','L1','M1','M3','P1','S1')
AND (POS.PSBCUS IS NOT NULL OR POS.PCTYM IS NOT NULL OR POS.PSTYPE ='S')
AND POS.BUSINESS_DATE = run_date_char
GROUP BY
POS.UBS_ID,POS.SYSTEMCODE,POS.RECIPIENTCODE,POS.BUSINESS_DATE,POS.PACCT,POS.PFIRM,POS.POFFIC,POS.PCUSIP,POS.PBS,CASE WHEN POS.PSTYPE='S' THEN POS.PSYMBL ELSE POS.PFC END,
POS.PEXCH,POS.PSUBEX,POS.PCURSY,
CASE WHEN POS.PSBCUS IS NULL THEN SUBSTR(POS.PCTYM,5,2) || SUBSTR(POS.PCTYM,1,4) ELSE POS.PSBCUS || SUBSTR(POS.PCTYM,5,2) || SUBSTR(POS.PCTYM,1,4) END,
NVL(POS.PSUBTY,'F') ,POS.PSTRIK,POS.PCLOSE,TO_CHAR(CASE WHEN INSTR(POS.PUNDCP,'.') > 0 OR LENGTH(POS.PUNDCP) < 15 THEN POS.PUNDCP ELSE TO_CHAR(TO_NUMBER(POS.PUNDCP) / 100000000) END),
POS.BBG_EXCHANGE_CODE,POS.BBG_TICKER,POS.BBG_YELLOW_KEY,POS.PPCNTY,POS.PMULTF,POS.PSUBTY,POS.SOURCE_GMI_LIB,RIC,
CASE WHEN PSUBTY = 'S' THEN POS.TYPE ELSE NULL END,
DECODE(POS.UBS_ID,NULL,POS.PCUSP2,POS.ISIN),POS.UNDERLYING_BBG_TICKER,POS.UNDERLYING_BBG_EXCHANGE,POS.PRODUCT_CLASSIFICATION,
CASE WHEN PSUBTY = 'S' THEN POS.PSDSC2 ELSE NULL END,
CASE WHEN PSUBTY = 'S' THEN C.SSDSC3 ELSE NULL END,
NVL(C.SSECID,POS.PCUSIP),
POS.PYSTMV,
POS.PMINIT,
POS.PEXPDT,
CASE WHEN PSUBTY = 'S' THEN SUBSTR(C.ZDATA2,77,1) ELSE NULL END;Now, could you plz help me in replacing the bold text in the query with the requirement.
Thanks and Rgds
DG
Edited by: BluShadow on 16-May-2011 09:39
added {noformat}{noformat} tags. Please read: {message:id=9360002} for details on how to post code/data
Similar Messages
-
Facing problem while using aggregate functions.
I am trying to use aggregate functions such as sum, count in my CQL query.
It is not giving me an error but i am unable to get the correct output out of that.
Query is:
<?xml version="1.0" encoding="UTF-8"?>
<wlevs:config xmlns:wlevs="http://www.bea.com/ns/wlevs/config/application"
xmlns:jdbc="http://www.oracle.com/ns/ocep/config/jdbc">
<processor>
<name>APL_EFW_CostEvent_Processor</name>
<rules>
<view id="CostEventView"
schema="eventName eventType eventId opportunityStatusId opportunity_cost APL_Event_Inbound"><![CDATA[
SELECT X.eventName, X.eventType,
X.eventId, X.opportunityStatusId,
X.opportunity_cost,X.APL_Event_Inbound
from APL_EFW_Master_Inbound_Channel
XMLTable (
'/' PASSING BY VALUE APL_EFW_Master_Inbound_Channel.APL_Event_Inbound as "."
COLUMNS
eventId char(256) PATH 'fn:data(Event/EventHeader/eventId)',
eventName char(256) PATH 'fn:data(Event/EventHeader/eventName)',
eventType char(256) PATH 'fn:data(Event/EventHeader/eventType)',
opportunityStatusId char(256) PATH 'fn:data(Event/ApplicationDataArea/opportunity/opportunity_details/opportunity_status)',
opportunity_cost char(256) PATH 'fn:data(Event/ApplicationDataArea/opportunity/opportunity_details/opportunity_cost)',
APL_Event_Inbound xmltype path '/'
) AS X
]>
</view>
<query id="CostEventQuery">
<![CDATA[
SELECT
XMLELEMENT("opportunity",
XMLELEMENT("cost_opportunity",
XMLFOREST(X.opportunity_cost))) as APL_Event_Inbound
FROM CostEventView
MATCH_RECOGNIZE (
MEASURES
A.opportunity_cost as opportunity_cost
PATTERN (B A+) within 30000 milliseconds
DEFINE
A as sum(A.opportunity_cost)> 1000 )
as X
]]>
</query>
</rules>
</processor>
</wlevs:config>
The problem i am getting is when the value is getting compared with the opportunity cost rather than with the sum of oppCost
A as sum(A.opportunityCost)>1000
A as A.opportunityCost >1000
Both the cases are treated as same .It would help if you could provide sample input data and associated output that illustrates the problem
-
Problem while using aggregate functions in EJB QL 2.1
Hai all,
I am using aggregate function as follows
select max(c.id) from customer as c
for this iam selected check box EJB QL 2.1 in persistent.xml
this is validated by nwds, but while deploying server raising error like ejb ql syntax error.
Actually according to EJB QL 2.1 this is a valid query, what i need to do for run this same query .
Anybody please help me in this regard
Regards
SomarajuBeevin
Both two are not even validated,
but with the first one as select max(c.id) from customer as c , in this case it is validated but while deploying it is error as , object must be return
But when i saw the ejb2.1 specification we can write this type of queries also ?
Is it problem with was any thing
Regards
Somaraju -
How to use Substring function with Case statement.
Hello Everyone,
I have one requirement where I have to use substring function on the field for report criteria.
E.G.
I have Branch Name Field where I have all branch names information, Now some of the branch names are too big with some extension after the name .
now i want to substing it but the character length varies for each branch.
so is there any way where we can use case statement where we can define that if branch name character are exceeding some value then substing it with this length.Try something like this:
CASE WHEN LENGTH(tablename.Branch_Name) > n THEN SUBSTRING(...) ELSE tablename.Branch_Name END
where n is the number of characters you want to start the break. -
Can use aggregate functions inside cursor
Hi
Can i use aggergate functiions inside cursors because i was getting the below error message when try to execute the below procedure.
I need following thins to implement procedure ,When pass 100 through parameter ,that will check the total count on the table if matches delete the records from table .
create or replace procedure p12(p_count number) is
cnt number :=0;
tot number :=0;
cursor c1 is select count(1),c2,rowid from t1 where
group by c2,rowid;
begin
for y in c1
loop
delete from t1 where row_id=y.rowid;
cnt:=y.cnt+1;
tot:=y.tot+1;
if mod(y.cnt,100)=0
then
dbms_output.put_line(y.tot||'total records deleted');
end if;
end loop;
end;
4/14 PL/SQL: SQL Statement ignored
4/55 PL/SQL: ORA-00936: missing expression
Thanksinadvance
MRI was still getting the error
create or replace procedure p12(p_count number) is
cnt number :=0;
tot number :=0;
cursor c1 is select count(1),c2,rowid from t1
where c2=100
group by c2,rowid;
begin
for y in c1
loop
delete from t1 where rowid=y.rowid;
cnt:=y.cnt+1;
tot:=y.tot+1;
if mod(y.cnt,100)=0
then
dbms_output.put_line(y.tot||'total records');
end if;
end loop;
end;
LINE/COL ERROR
11/1 PL/SQL: Statement ignored
11/8 PLS-00302: component 'CNT' must be declared
12/1 PL/SQL: Statement ignored
12/8 PLS-00302: component 'TOT' must be declared
13/1 PL/SQL: Statement ignored -
Error in using aggregate function in Outer Query in Siebel Analytics
Hi,
When I am using aggregate function in outer query in Siebel Analytics I am facing error.
Error Codes: OPR4ONWY:U9IM8TAC:OI2DL65P:OI2DL65P
State: HY000. Code: 10058. [NQODBC] [SQL_STATE: HY000] [nQSError: 10058] A general error has occurred. [nQSError: 59111] The SQL statement must include a GROUP BY clause. (HY000)
Bellow is the code.
SELECT test1.username saw_0, test1.desg saw_1,COUNT (test2.querydate) saw_2
FROM (SELECT POSITION.CBL username,
POSITION.CBP desg
FROM "CM"
WHERE (POSITION.BPTCD = 'Marketing')
AND (POSITION.EDate =TIMESTAMP '1899-01-01 00:00:00'
) test1,
(SELECT users.UN username,
measures."Query Count" querycount,
measures."Max Total Time" secs,
topic.db dashboardname,
"Query Time".DATE querydate
FROM "Plan"
WHERE (topic."Dashboard Name" IN ('DS'))) test2
WHERE test2.username = LOWER (test1.username)
AND test2.dashboardname = 'DS'
GROUP BY test1.username, test1.desgShould your query be a valid SQL query?
I can't think that the query you have would be valid in a SQL plus window.
Chris -
How to use Aggregate Functions during Top N analysis?
Say i want to find top 5 highest salaries and their totals and average. In that case how to use aggregate functions. Please give me an example on this.
Regards,
Renu
Message was edited by:
user642387Hi,
Yes, you can do that with aggregate functions.
First, do a sub-query to retrieve all the salaries (in descending order), then say "WHERE ROWNUM <= 5" in the main query. Use the aggregate SUM and AVG functions in the main query.
Analytic functions are easier to use for jobs like this, once you get familiar with them. If you're not leaving the field this month, then it's probably worthwhile for you to get familiar with analytic functions. -
SQL Expression in decode function or case statement?
Can I put SQL expressions in decode function or case statement?
For example,
select le.profile, decode( select count(1) from profile_data where NAME= 'XXXX_AFTER' and object_id = le.profile, 0, 'N', 'Y')
from element le;
or
select le.profile, case WHEN ( select count(1) from profile_data where NAME= 'XXXX_AFTER' and object_id = le.profile) = 0 THEN 'N'
ELSE 'Y'
from element le;
None of the above work.
Can anyone tell me how to make it work?
Is there any workaround?
Thanks,
JYou simply needed and END to your CASE statement;
SQL> with profile_data as (
select 'XXXX_AFTER' name, 1 object_id from dual),
element as (
select 1 profile from dual union all
select 2 from dual)
select le.profile,
case WHEN ( select count(1) from profile_data where NAME= 'XXXX_AFTER' and object_id = le.profile) = 0
THEN 'N'
ELSE 'Y'
END new_col
from element le
PROFILE N
1 Y
2 N -
Using Aggregate function in queries
Hi all,
Please take a look on this query and suggest me why i'm getting the error..
This is my simple query using aggregate function in it..
SELECT T1.NAME, T1.DESCRIPTION, SUM(T2.QUANTITY)
FROM TABLE1 T1, TABLE2 T2
WHERE T1.ID=T2.ID
GROUP BY T1.NAME, T1.DESCRIPTION
Above query added with a sub-query in the select segment..
SELECT T1.NAME, T1.DESCRIPTION, SUM(T2.QUANTITY), (SELECT AVG(T3.PRICE) FROM TABLE1 TT1, TABLE3 T3 WHERE TT1.ID=T3.ID AND TT1.ID=T1.ID) AV_PRICE
FROM TABLE1 T1, TABLE2 T2
WHERE T1.ID=T2.ID
GROUP BY T1.NAME, T1.DESCRIPTION
When i add a sub-query which has aggregate function in it, i'm getting the 'ORA-00979: not a GROUP BY expression' error.What is your DB Version. Your query works without any issue in my DB. I used WITH clause to create the sample data. The query highlighted in BLUE is the actual query.
SQL> select * from v$version where rownum = 1;
BANNER
Oracle Database 10g Enterprise Edition Release 10.2.0.5.0 - 64bi
SQL> with table1
2 as
3 (
4 select 1 id, 'karthick' name, 'user name' description from dual
5 ),
6 table2
7 as
8 (
9 select 1 id, 100 quantity from dual
10 ),
11 table3
12 as
13 (
14 select 1 id, 10 price from dual
15 )
16 select t1.name
17 , t1.description
18 , sum(t2.quantity)
19 , (
20 select avg(t3.price)
21 from table1 tt1
22 , table3 t3
23 where tt1.id = t3.id
24 and tt1.id = t1.id
25 ) av_price
26 from table1 t1
27 , table2 t2
28 where t1.id = t2.id
29 group
30 by t1.name
31 , t1.description;
NAME DESCRIPTI SUM(T2.QUANTITY) AV_PRICE
karthick user name 100 10
SQL> -
How to use analytical functions inside a mapping
Hello everybody. Here Isend you a trick that we are using for two years.
If you want to use a function (for instance :ROW_NUMBER() OVER (PARTITION BY ... ORDER BY ...)) inside a mapping you must create an expression with in the INGRP1 the fields you are going to use in the window function and in the OUTGRP1 the function you want. Create an Out -attribute with the expressión and link it to a distinct operator (using a "distinct" you encapsulte the sql and you will be able to use the function inside a filter - in the where clause.). The distinct can eliminate some register (depends on the function). If you validate the expression an error will appear (don't worry about that, the mapping will be ok).
But there is a limitation, you will not be able tou sum over, min over, max over (it detect that they are aggregator functions). Other limitation: the debugger doesn't run with this kind of functions.
Please publish this information on "The Warehouse Builder Utility Exchange". Mi email is [email protected] (if you need more information)It is possible to add SUM, MIN, MAX functions also to OWB releases prior to Paris - You have to put them in double quotes - write "MIN" "MAX" "SUM" etc. The rest of the rules (adding cut-off operator after the expression - as DISTINCT, or UNION ALL) is analogical to with ROW_NUMBER()
That means You can create mapping with following functionality:
SELECT
sum (salary) over (partition by DEPARTMENT) department_salary,
salary,
employee_id,
employee_name
FROM employees_salaries
writing it this way in OWB:
SELECT
"SUM" (salary) over (partition by DEPARTMENT) department_salary,
salary,
employee_id,
employee_name
FROM employees_salaries
Regards,
Martin -
Using java function in select statement
Hi,
I am trying to use java function in select statement.
public class ClassA{
private static String MyConst = "foo";
public static String functionA(){
return MyConst;
in my query I have:
select
ClassA.functionA() AS id,
groupId AS newID,
from
myChannel[now]
ClassA is part of the application (no need to import).
I get and error of Invalid Expression on ClassA.functionA().
I also tried to declare the function in the processor element:
<wlevs:processor id="proc">
<wlevs:function function-name="A" exec-methode="functionA">
<bean class="mtPackage.ClassA"/>
</wlevs:function>
<wlevs:processor>
but then I get a different error in the processor XML file: "An InvocationTargetException was encoutered while attemting to register the user defind function A. The message was null"
What am I missing here?Hi,
From the above description, you have tried two manners to call method functionA() in the user defined class ClassA. One uses java cartridge manner directly and the other try to use user defined function manner.
For the java cartridge manner, the following CQL query should work if the ClassA is really included in the OEP app. I have done similar test before, it works.
select
ClassA.functionA() AS id,
groupId AS newID,
from
myChannel[now]
For user defined function manner, I think two things you need to change:
1. Need to declare the function in the EPN assembly file(under META-INF/spring/), not component configuration file(under META-INF/wlevs/). The following is an example:
<wlevs:processor id="proc">
<wlevs:function function-name="A" exec-methode="functionA">
<bean class="mtPackage.ClassA"/>
</wlevs:function>
</wlevs:processor>
2. Call the user defined function in the CQL query in the component configuration file under processor. For example:
select A() from myChannel
Regards,
XiYing -
Oracle:how to use max() function in case expression
how to use max() function in case expression, Please explain with any example
Hope this helps and should be self explanatory
with t as
(select 1 col,100 col2 from dual union
select 2 ,100 from dual union
select 2 ,200 from dual union
select 3,100 from dual union
select 3,200 from dual )
select col, case when max(col2)=100 then 'with 100 range'
when max(col2)=200 then 'with 200 range' end from t group by col -
How to use aggregate function with Date
Hi All,
I have a group of date from that is it possible to Max and Min of date.
I have tried like this but its errored out <?MIN (current-group()/CREATION_DATE)?>.
I have also tried like this but it doesnt works
<?xdoxslt:minimum(CREATION_DATE)?>
Is it possible to use aggregate function with date values.
Thanks & Regards
SrikkanthHi KAVI PRIYA,
if date is not in cannonical format, how can we change it in BI publisher, then how to calcualte minimum and as well as maximum.
please advise me,
Thanks,
Sri -
Using aggregate function count
hi experts,
I want to select 2 fields from a table and put into structure (gw_konzs.) and with respect to that i want to use aggregate function count(*) for knowing how many rows has been selected ? and i dont want to use select & endselect
please help regarding this ...........
for example:
select konts ltext from ztab into corresponding fields of gw_konzs.
select count(*) from kna1 into gw_konzs
where konzs = gw_konzs-konzs.
append gw_konzs to gt_konzs
endselect.hi experts,
I want to select 2 fields from a table and put into structure and i want to select based on the where condition i dont want to use select & endselect
please help regarding this ...........
rewarded if useful
for example:
gw_detail & gw_konzs is structure
gt_detail & gt_konzsis internal table
select kunnr name1 from kna1 into corresponding fields of gw_detail where konzs = gw_konzs-konzs.
gw_detail-konzs = gw_konzs-konzs.
append gw_detail-konzs to gt_detail-konzs
endselect. -
How to use aggregate functions into Analytical functions
Can we use aggregate functions into analytical functions?
Please provide one example.
Smiles.HI Learner6
for information:
Aggregate Functions
Analytic Functions
for practic:
ORACLE-BASE - Analytic Functions
Thank you
Maybe you are looking for
-
IMessage connected to wifi doesn't work for iPhone and ipad
My ipad and iPhone worked great with iMessage on wifi until one week ago. As far as I'm aware nothing has changed. So, I'm not sure what happened. I've checked for updates on both devices and on iMessage and everything is currently updated to the lat
-
Error message (-48) when syncing game to ipod nano 3G
Is there anyone that knows how to fix this problem? I finally got my game to show up on itunes but when I try to sync it to my ipod it keeps saying " iTunes could not sync the game "Sims Bowling" to the ipod because an error occurred (-48). This is s
-
How to disable save as option in forefox when we right click on the image?
Hi Folks, Can somebody help to disable save as option when i right click on the image which is showing in the browser.
-
Mp4 files (.m4v) from Handbrake won't open in Quicktime.
Suddenly, about a week ago, whenever I try to open these files in QuickTime, I receive a message saying the required codec is missing. Yet, these same files will open in iTunes. In Quicktime, when I click on "Find out more," I'm taken to an article
-
Default Territory setup in 11.5.10
while installing Oracle apps...we come across... select internationalization Settings Screen...which describes... (a) Base Language (b) Default Territory... I am in Middle East...I am using Base Language as ENGLISH... What Territory I have to give he