Aggregate function max
hi evrybdy
can we use MAX aggregate function to pick a field of type char from database table ...if yes thn how ..?
plzzz ans soon .
thank u .
No. Max is only for numbers. ( even it will not work for NUMC type )
OPENSQL statements will not work as ASCII format.
Cheers
Kothand
Similar Messages
-
Select with aggregate function MAX
Hi,
Can I write a query like:
select field1 field2 MAX( field3 )
from z_table
into table itab
for all entries in itab2
where field1 = itab2-field1
and field2 = itab2-field2.
In the above case all the 3 fields are key fields.
Thanks in advance.Hi,
I did try it.. but got an error like: "The field "z_table~field2" from the select list is missing in the GROUP BY clause" but my aggregate funtion is used only for the field3..... So, I wanted to know if anybody has come across such a query....
Thanks, -
Help with aggregate function max query
I have a large database which stores daily oil life, odo readings from thousands of vehicles being tested but only once a month.
I am trying to grab data from one month where only the EOL_READ = 0 and put all of those values into the previous month's query where EOL_READ = anything.
Here is the original query which grabs everything
(select distinct vdh.STID, vdh.CASE_SAK,
max(to_number(decode(COMMAND_ID,'EOL_READ',COMMAND_RESULT,-100000))) EOL_READ,
max(to_number(decode(COMMAND_ID,'ODO_READ',COMMAND_RESULT,-100000))) ODO_READ,
max(to_number(decode(COMMAND_ID,'OIL_LIFE_PREDICTION',COMMAND_RESULT,-100000))) OIL_LIFE_PREDICTION
from veh_diag_history vdh, c2pt_data_history cdh
where vdh.veh_diag_history_sak = cdh.veh_diag_history_sak
and cdh.COMMAND_ID in ('EOL_READ','ODO_READ','OIL_LIFE_PREDICTION')
and vdh.LOGICAL_TRIGGER_SAK = 3
and cdh.CREATED_TIMESTAMP =vdh.CREATED_TIMESTAMP
and cdh.CREATED_TIMESTAMP >= to_date('03/01/2007 12:00:00 AM','mm/dd/yyyy HH:MI:SS AM')
and cdh.CREATED_TIMESTAMP < to_date('04/01/2007 12:00:00 AM','mm/dd/yyyy HH:MI:SS AM')
group by vdh.STID, vdh.case_sak
having count(distinct command_id) = 3
order by OIL_LIFE_PREDICTION)
which gives 5 columns:
STID, CASE_SAK, EOL_READ, ODO_READ, and OIL_LIFE_PREDICTION
and gives me about 80000 rows returned for the above query
I only want one reading per month, but sometimes I get two.
STID is the unique identifier for a vehicle.
I tried this query:
I tried this query which nests one request within the other and SQL times out every time:
select distinct vdh.STID, vdh.CASE_SAK,
max(to_number(decode(COMMAND_ID,'EOL_READ',COMMAND_RESULT,-100000))) EOL_READ,
max(to_number(decode(COMMAND_ID,'ODO_READ',COMMAND_RESULT,-100000))) ODO_READ,
max(to_number(decode(COMMAND_ID,'OIL_LIFE_PREDICTION',COMMAND_RESULT,-100000))) OIL_LIFE_PREDICTION
from veh_diag_history vdh, c2pt_data_history cdh
where vdh.veh_diag_history_sak = cdh.veh_diag_history_sak
and cdh.COMMAND_ID in ('EOL_READ','ODO_READ','OIL_LIFE_PREDICTION')
and vdh.LOGICAL_TRIGGER_SAK = 3
and cdh.CREATED_TIMESTAMP =vdh.CREATED_TIMESTAMP
and cdh.CREATED_TIMESTAMP >= to_date('02/01/2007 12:00:00 AM','mm/dd/yyyy HH:MI:SS AM')
and cdh.CREATED_TIMESTAMP < to_date('03/01/2007 12:00:00 AM','mm/dd/yyyy HH:MI:SS AM')
group by vdh.STID, vdh.case_sak
having count(distinct command_id) = 3
and vdh.stid in (select distinct vdh.STID, vdh.CASE_SAK,
max(to_number(decode(COMMAND_ID,'EOL_READ',COMMAND_RESULT,-100000))) EOL_READ,
max(to_number(decode(COMMAND_ID,'ODO_READ',COMMAND_RESULT,-100000))) ODO_READ,
max(to_number(decode(COMMAND_ID,'OIL_LIFE_PREDICTION',COMMAND_RESULT,-100000))) OIL_LIFE_PREDICTION
from veh_diag_history vdh, c2pt_data_history cdh
where vdh.veh_diag_history_sak = cdh.veh_diag_history_sak
and cdh.COMMAND_ID in ('EOL_READ','ODO_READ','OIL_LIFE_PREDICTION')
and vdh.LOGICAL_TRIGGER_SAK = 3
and cdh.CREATED_TIMESTAMP =vdh.CREATED_TIMESTAMP
and cdh.CREATED_TIMESTAMP >= to_date('03/01/2007 12:00:00 AM','mm/dd/yyyy HH:MI:SS AM')
and cdh.CREATED_TIMESTAMP < to_date('04/01/2007 12:00:00 AM','mm/dd/yyyy HH:MI:SS AM')
group by vdh.STID, vdh.case_sak
having count(distinct command_id) = 3
and (max(to_number(decode(COMMAND_ID,'EOL_READ',COMMAND_RESULT,-100000)))) = 0)
order by OIL_LIFE_PREDICTION
so in summary I am trying to select values from the previous month only from those stids where this month's value for EOL_READ = 0
Any ideas.....please help.You can use row_number() within each stid and each month to determine the first read of each month. Then you can use lag() to get the previous month's reading of the current month's reading is zero.
with all_data as (
select stid,
case_sak,
eol_read,
timestamp,
row_number() over (partition by stid, trunc(timestamp,'mm') order by timestamp) AS rn
from veh_diag_history
select stid,
case_sak,
case
when eol_read = 0
then lag(eol_read) over (partition by stid order by timestamp)
else eol_read
end as eol_read
from all_data
where rn = 1; -
Self Join and Aggregate Functions
Hi all,
I am trying a query in TOAD where I need to use an aggregate function MAX and a self join using a subquery. Its working fine when there is no aggregate function but when I tried to use the MAX function then its running for infi time. Is it because of the invalid joins? or because of the usage of the self join and aggregate func?
The query contains some other tables too....
Any one please help....
Thanks in advance,
GToad will bring back a limited set of rows and present them to you giving the impression that the work is done. Adding an aggregate function requires the entire resultset to be traversed.
Yes, post the 2 queries to verify what I am saying. -
Aggregate functions like 'max' in XQuery..
Hello,
I am trying to find the max of one of the fields in the table using XQuery aggregate function: fn:max as described in the below URL:
http://e-docs.bea.com/aldsp/docs30/xquery/sql_pushdown.html
Here is what I have:
declare $lineNo as xs:int :=
for $pd in ns4:getAllPalletDtls()
group $pd/LINE_NO as $line_group
by 1
return fn:max($line_group);
However, it's giving me error at line # 3 above:
Invalid expression: Expecting AS, found '/'
I am getting this error in ALDSP3.0.1 Aqualogic Data Service Studio.
Could someone please let me know what is wrong with the above code.
Thanks a lot,
Mamta"group" expects to be followed by a variable (not an expression). If you add a 'let' with your expression you can create the following which is more inline with your original query
declare $lineNo as xs:int :=
for $pd in ns4:getAllPalletDtls()
let $lineNo := $pd/LINE_NO
group $lineNo as $line_group
by 1
return fn:max($line_group); -
Does EJBQL in JDeveloper support max aggregate function ?
Does EJBQL in JDeveloper support max aggregate function ?
I can't get it to work using select max(o.time) from MyObject o; It always returns the MyObject to me.
Anybody knows how to do this ?
Thanks.No one knows how exactly this works in JDeveloper 11g? As I see it, it's most likely a bug. For one, JDeveloper already supports plenty of databases and enforces some limitations on the useability of these databases where necessary (for instance, JDBC Positioning instead of Oracle's way of binding for the bind variables in a query). However, this enforcement does not happen with the WHERE clause of the view criteria in MySQL when you choose an operator that concatenates strings together in the WHERE clause, such as "contains". You'd expect it's smart enough to use different syntax for concatenating the strings, depending on the database (e.g., CONCAT for MySQL, || for Oracle). Second, lack of this enforcement should not break functionality, and here it does (searching with wildcards in LOV is just not possible declaratively with MySQL), which resembles a bug in the software.
For now, I've completely rewritten the LOV in Java, so it's not declarative anymore, but programmatic. It works, resembles searching with "contains" and it stays that way, despite of the higher difficulty in maintenance, until this issue can be solved declaratively, by any means: via a patch release or via a workaround. I hope someone can provide me (and quite possibly others facing this issue) with insight upon this.
Thanks in advance. -
Trying to create a Histogram type/object for aggregate functions
Hi,
I am trying to create an aggregate function that will return a histogram
type.
It doesn't have to be an object that is returned, I don't mind returning
a string but I would like to keep the associative array (or something
else indexed by varchar2) as a static variable between iterations.
I started out with the SecondMax example in
http://www.csis.gvsu.edu/GeneralInfo/Oracle/appdev.920/a96595/dci11agg.htm#1004821
But even seems that even a simpler aggregate function like one strCat
below (which works) has problems because I get multiple permutations for
every combination. The natural way to solve this would be to create an
associative array as a static variable as part of the Histogram (see
code below). However, apparently Oracle refuses to accept associate
arrays in this context (PLS-00355 use of pl/sql table not allowed in
this context).
If there is no easy way to do the histogram quickly can we at least get
something like strCat to work in a specific order with a "partition by
... order by clause"? It seems that even with "PARALLEL_ENABLE"
commented out strCat still calls merge for function calls like:
select hr,qtr, count(tzrwy) rwys,
noam.strCat(cnt) rwycnt,
noam.strCat(tzrwy) config,
sum(cnt) cnt, min(minscore) minscore, max(maxscore) maxscore from
ordrwys group by hr,qtr
Not only does this create duplicate entries in the query result like
"A,B,C" and "A,C,B" it seems that the order in rwycnt and config are not
always the same so a user can not match the results based on their
order.
The difference between my functions and functions like sum and the
secondMax demonstrated in the documentation is that secondMax does not
care about the order in which it gets its arguments and does not need to
maintain an ordered set in order to return the correct results. A good
example of a built in oracle function that does care about all its
arguments and probably has to maintain a similar data structure to the
one I want is the PERCTILE_DISC function. If you can find the code for
that function (or something like it) and forward a reference to me that
in itself would be very helpful.
Thanks,
K.Dingle
CREATE OR REPLACE type Histogram as object
-- TYPE Hist10 IS TABLE OF pls_integer INDEX BY varchar2(10),
-- retval hist10;
-- retval number,
retval noam.const.hist10,
static function ODCIAggregateInitialize (sctx IN OUT Histogram)
return number,
member function ODCIAggregateIterate (self IN OUT Histogram,
value IN varchar2) return number,
member function ODCIAggregateTerminate (self IN Histogram,
returnValue OUT varchar2,
flags IN number) return number,
member function ODCIAggregateMerge (self IN OUT Histogram,
ctx2 IN Histogram) return number
CREATE OR REPLACE type body Histogram is
static function ODCIAggregateInitialize(sctx IN OUT Histogram) return
number is
begin
sctx := const.Hist10();
return ODCIConst.Success;
end;
member function ODCIAggregateIterate(self IN OUT Histogram, value IN
varchar2)
return number is
begin
if self.retval.exist(value)
then self.retval(value):=self.retval(value)+1;
else self.retval(value):=1;
end if;
return ODCIConst.Success;
end;
member function ODCIAggregateTerminate(self IN Histogram,
returnValue OUT varchar2,
flags IN number)
return number is
begin
returnValue := self.retval;
return ODCIConst.Success;
end;
member function ODCIAggregateMerge(self IN OUT Histogram,
ctx2 IN Histogram) return number is
begin
i := ctx2.FIRST; -- get subscript of first element
WHILE i IS NOT NULL LOOP
if self.retval.exist(ctx2(i))
then self.retval(i):=self.retval(i)+ctx2.retval(i);
else self.retval(value):=ctx2.retval(i);
end if;
i := ctx2.NEXT(i); -- get subscript of next element
END LOOP;
return ODCIConst.Success;
end;
end;
CREATE OR REPLACE type stringCat as object
retval varchar2(16383), -- concat of all value to now varchar2, --
highest value seen so far
static function ODCIAggregateInitialize (sctx IN OUT stringCat)
return number,
member function ODCIAggregateIterate (self IN OUT stringCat,
value IN varchar2) return number,
member function ODCIAggregateTerminate (self IN stringCat,
returnValue OUT varchar2,
flags IN number) return number,
member function ODCIAggregateMerge (self IN OUT stringCat,
ctx2 IN stringCat) return number
CREATE OR REPLACE type body stringCat is
static function ODCIAggregateInitialize(sctx IN OUT stringCat) return
number is
begin
sctx := stringCat('');
return ODCIConst.Success;
end;
member function ODCIAggregateIterate(self IN OUT stringCat, value IN
varchar2)
return number is
begin
if self.retval is null
then self.retval:=value;
else self.retval:=self.retval || ',' || value;
end if;
return ODCIConst.Success;
end;
member function ODCIAggregateTerminate(self IN stringCat,
returnValue OUT varchar2,
flags IN number)
return number is
begin
returnValue := self.retval;
return ODCIConst.Success;
end;
member function ODCIAggregateMerge(self IN OUT stringCat,
ctx2 IN stringCat) return number is
begin
self.retval := self.retval || ctx2.retval;
return ODCIConst.Success;
end;
end;
CREATE OR REPLACE FUNCTION StrCat (input varchar2) RETURN varchar2
-- PARALLEL_ENABLE
AGGREGATE USING StringCat;GraphicsConfiguration is an abstract class. You would need to subclass it. From the line of code you posted, it seems like you are going about things the wrong way. What are you trying to accomplish? Shouldn't this question be posted in the Swing or AWT forum?
-
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 -
EJB 3.0: EJBQL aggregate functions not working properly
Hi!
We are experiencing problems when using aggregate functions within EJBQL queries. In my case, we have to use max() function to retrieve the max start_date from a specific table. In order to do this, we wrote the following code:
String strQuery = "select max(o.dt_fim) from Questionario o";
Query queryTeste = em.createQuery(strQuery);
List x = queryTeste.getResultList();
OBS: dt_fim is an Oracle 10g datetime column, with its corresponding entity attribute in Questionario entity class.
AFAIK, the query above should return only one record (object instance) containing the date value, right? However, it brougth a list of the entity objects, corresponding to all records in the table, as we had just run "select o from Questionario o".
Examining the server log we can see this behavior:
[TopLink Fine]:2006.09.12 01:47:55.526--
ServerSession(7119662)--Connection(7048401)--Thread(Thread[ApplicationServerThread-0,5,RequestThreadGroup])
--SELECT ID_QUEST, DT_INICIO, DT_FIM, DS_QUEST, DT_CRIACAO, TP_QUEST, NM_QUEST, COD_SEGMENTO, ID_GRUPO FROM TBBOP_QUEST
Are anyone experiencing this issue?
Is there any workaround?
Any help would be much appreciated, since we need to deploy the application for user testing asap.
Thanks in advance.
Best regards,
Gustavo
PS: We are using JDeveloper 10.1.3.0.4(SU4)
Message was edited by:
Gustavo Lopes SobralThe 10.1.3.0 versions of JDeveloper contain our early preview of EJB 3.0 JPA functionality. One limitation is that the query language is not complete (released before the spec was finalized).
You can use TopLink Essentials (JPA Reference implementation) whihc contains a complete compliant implementation of the final specification.
http://otn.oracle.com/jpa
The 10.1.3.1 JDeveloper preview ships with TopLink Essentials.
Doug -
Why doesn't PIVOT clause work with COLLECT aggregate function in 11g ?
Hello all !
I am really puzzled as to what is considered an aggregate function in the context of the PIVOT clause in 11g.
I have been toying with quite a few things related to collections lately and this arose as an aside :
CREATE TABLE TEST_COLL
NODE_ID VARCHAR2(15 CHAR) NOT NULL,
NODE_VALUE VARCHAR2(45 CHAR) NOT NULL,
NODE_LEVEL NUMBER(1) NOT NULL
CREATE OR REPLACE TYPE TREE_NODE AS OBJECT
NODE_KEY VARCHAR2( 15 CHAR),
NODE_NAME VARCHAR2(127 CHAR)
CREATE OR REPLACE TYPE TREE_NODES AS TABLE OF TREE_NODE NOT NULL;At this stage I am sure we all agree that the query
SELECT NODE_LEVEL,
CAST(COLLECT(TREE_NODE(NODE_ID, NODE_VALUE)) AS TREE_NODES) AS NODES
FROM TEST_COLL
GROUP BY NODE_LEVEL;is perfectly valid as the COLLECT function is an aggregate function according to the [Official Documentation|http://docs.oracle.com/cd/E11882_01/server.112/e10592/functions031.htm#i1271564]
But then, one of the following two queries should work
SELECT CAST(REGION_NODES AS TREE_NODES) AS REGIONS,
CAST(DEPARTMENT_NODES AS TREE_NODES) AS DEPARTMENTS,
CAST(AREA_NODES AS TREE_NODES) AS AREAS,
CAST(CENTRE_NODES AS TREE_NODES) AS CENTRES
FROM (SELECT NODE_LEVEL, TREE_NODE(NODE_ID, NODE_VALUE) AS NODE
FROM TREE_COLL
PIVOT (COLLECT(NODE) FOR NODE_LEVEL IN (1 AS REGION_NODES,
2 AS DEPARTMENT_NODES,
3 AS AREA_NODES,
4 AS CENTRE_NODES
or (better)
SELECT REGION_NODES AS REGIONS,
DEPARTMENT_NODES AS DEPARTMENTS,
AREA_NODES AS AREAS,
CENTRE_NODES AS CENTRES
FROM (SELECT NODE_LEVEL, TREE_NODE(NODE_ID, NODE_VALUE) AS NODE
FROM TREE_COLL
PIVOT (CAST(COLLECT(NODE) AS TREE_NODES) FOR NODE_LEVEL IN (1 AS REGION_NODES,
2 AS DEPARTMENT_NODES,
3 AS AREA_NODES,
4 AS CENTRE_NODES
);yet, both fail with
ORA-56902: expect aggregate function inside pivot operationInvestigating further, I found the same behaviour when using XMLAGG as the aggregate function in the PIVOT clause.
Is this normal ? And if it is, is there any other way to achieve the result I was anticipating ?
My version is
SQL> SELECT BANNER FROM V$VERSION;
BANNER
Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 - 64bit Production
PL/SQL Release 11.2.0.3.0 - Production
CORE 11.2.0.3.0 Production
TNS for 64-bit Windows: Version 11.2.0.3.0 - Production
NLSRTL Version 11.2.0.3.0 - ProductionThanks in advance
Best Regards
PhilipMost likely a bug. But you can bypass it by using any other aggregate making sure group consists of a single row and apply collect to a pivoted value. Yes, the cost is double aggregation. And also there is another cost - you would need to create MAP member function otherwise aggreations like MAX/MIN, etc. will not work:
CREATE OR REPLACE TYPE TREE_NODE AS OBJECT
NODE_KEY VARCHAR2( 15 CHAR),
NODE_NAME VARCHAR2(127 CHAR),
map member function f return varchar2
Type created.
CREATE OR REPLACE TYPE BODY TREE_NODE AS
map member function f return varchar2 is
begin
return NODE_NAME;
end f;
end;
Type body created.
CREATE OR REPLACE TYPE TREE_NODES AS TABLE OF TREE_NODE NOT NULL
Type created.
SQL> select *
2 from test_coll
3 /
NODE_ID NODE_VALUE NODE_LEVEL
1 A 1
2 B 2
3 C 3
4 D 4
5 E 1
6 F 2
7 G 3
8 H 4
8 rows selected.
SQL> Now:
SELECT CAST(COLLECT(REGION_NODES) AS TREE_NODES) AS REGIONS,
CAST(COLLECT(DEPARTMENT_NODES) AS TREE_NODES) AS DEPARTMENTS,
CAST(COLLECT(AREA_NODES) AS TREE_NODES) AS AREAS,
CAST(COLLECT(CENTRE_NODES) AS TREE_NODES) AS CENTRES
FROM (
SELECT ROWID RID,
NODE_LEVEL,
TREE_NODE(NODE_ID, NODE_VALUE) AS NODE
FROM TEST_COLL
PIVOT (MAX(NODE) FOR NODE_LEVEL IN (
1 AS REGION_NODES,
2 AS DEPARTMENT_NODES,
3 AS AREA_NODES,
4 AS CENTRE_NODES
REGIONS(NODE_KEY, NODE_NAME) DEPARTMENTS(NODE_KEY, NODE_NAME) AREAS(NODE_KEY, NODE_NAME) CENTRES(NODE_KEY, NODE_NAME)
TREE_NODES(TREE_NODE('1', 'A'), TREE_NODE('5', 'E')) TREE_NODES(TREE_NODE('6', 'F'), TREE_NODE('2', 'B')) TREE_NODES(TREE_NODE('7', 'G'), TREE_NODE('3', 'C')) TREE_NODES(TREE_NODE('8', 'H'), TREE_NODE('4', 'D'))
SQL> SY. -
Aggregate Function in SQL subquery
Hello,
I am trying to use the following syntax and it is saying I can't use an aggregate function in a subquery. I can't use a GROUP BY in this case because if another field in the project table (such as status) is different, that project will show up twice.
So in this case I am using this syntax to show the most recent quote within the project.
SELECT PROJECT.*, QUOTE.QuoteDate, QUOTE.QuoteCode
FROM PROJECT LEFT JOIN QUOTE ON PROJECT.ProjectID = QUOTE.ProjectID
WHERE QUOTE.QuoteDate=(SELECT Max(Q.QuoteDate) FROM QUOTE Q WHERE Q.ProjectID = PROJECT.ProjectID);
My goal here is to show the most recent quote within each project (there can be multiple revisions of a quote within each project). I want to show other fields such as the status of the quote, but if the status is different between quotes, the GROUP BY on that
field will cause it to be listed more than once. All I want to show is the most recent quote for each project.
Let me know if this isn't clear.
Thanks.Try the below querySELECT P1.projectID,p1.QuoteDate, Q1.QuoteCode,p1.*
FROM PROJECT P1 inner join (SELECT Q.ProjectID,Max(Q.QuoteDate) QD FROM QUOTE Q group by Q.ProjectID) Q1 on Q1.ProjectID = P1.ProjectID and Q1.QD=P1.QuoteDate-Prashanth -
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 -
Aggregate function for a report that has sets
My report combines similar analysis into sets. I need to apply aggregate function such as Max() to the resulting columns. I tried but Analytics doesn't like that. Can someone tell me if this is supported or not? If not, what would be a workaround?
Have you tried placing the results into a pivot table then assigning this to display MAX results
-
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
DGHi 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 -
Aggregate functions for internal table.
Hi Gurus,
I'm just wondering whether we can use aggregate functions for internal table. I wanted to know the max value for a particular column in my internal table. How can I achieve this easily and quickly.
Thanks,
David.sort the table on which you want the maximum value and read it. Please find below example code.
TYPES : BEGIN OF ty_mvke,
matnr TYPE mvke-matnr,
vkorg TYPE mvke-vkorg,
vtweg TYPE mvke-vtweg,
AUMNG type mvke-AUMNG,
END OF ty_mvke.
DATA : wa_mvke TYPE ty_mvke,
it_mvke TYPE STANDARD TABLE OF ty_mvke.
data : temp_AUMNG type AUMNG.
START-OF-SELECTION.
SELECT matnr vkorg vtweg AUMNG FROM mvke INTO
table it_mvke.
sort it_mvke by AUMNG.
read TABLE it_mvke into wa_mvke INDEX 1.
if sy-subrc = 0.
move wa_mvke-AUMNG to temp_AUMNG.
write : temp_AUMNG.
endif.
Thanks,
Sreekala.
Maybe you are looking for
-
I have used FF for about a year on my Dell Inspiron 15 and now every time I open it the resolution changes on me. If I close FF everything goes back to normal resolution. No problems with other browsers just FF. I went into the properties and tried t
-
How to get the number of resultSets opened ?
Hi, As I have a memory leak in my application, I wonder if there is a simple way to get the number of statements and resultsets opened to determine if it dramatically increases. Thanks.
-
Upgrading to camera raw 6.7
I get an error message when I try to upgrade CS5 to camera raw 6.7 for the mac. Has anyone faced this issue?
-
I'd like to determine what non-Latin characters can be displayed on my handset. Using the encoding UTF-16BE does not throw an exception and I can create Latin characters and basic symbols. However when I try Greek, Hebrew, and such the generated stri
-
Warning: Bogus Helvetica Font May Appear in System/Library/Fonts
I received an unreadable email message from a Windows-using friend recently. The text was a jumble of fractions. Things got weirder later that day when I noticed the same font corruption on Amazon.com and a few other sites that evidently use Helvetic