Regardding SQL Analytical Functions
Hi ,
I have a table test with the data as follows:
Sample Data:
s1 s2
=========================================
10 aaa,bbb
20 ccc,ddd
I would like to display the output as:
s1 s2
==========================
10 aaa
10 bbb
20 ccc
20 ddd
Thanks in Advance,
Vamsi
SQL> with t
2 as (select 1 no,'A,A,A,B' str from dual
3 union all
4 select 2 no,'A,B,C' str from dual
5 union all
6 select 3 no,'A,A,A,A,A,A,A,A' str from dual)
7 select no,regexp_substr(str,'[^,]+', 1, rno) list
8 from t, (select rownum rno
9 from (select max(NVL( LENGTH( REGEXP_REPLACE( str, '[^,]+', NULL ) ), 0 )) len
10 from t)
11 connect by level <= len+1) l
12 where regexp_substr(str,'[^,]+', 1, rno) is not null
13 order by no, rno
14 /
NO LIST
1 A
1 A
1 A
1 B
2 A
2 B
2 C
3 A
3 A
3 A
3 A
NO LIST
3 A
3 A
3 A
3 A
15 rows selected.
Similar Messages
-
SQL Analytic Functions - Licencing
Hi,
I am not sure, so I ask. Are SQL Analytical Functions (LEAD, LAG, construction OVER (PARTITION BY ORDER BY)) available in all versions of Oracle 11g? Especially Oracle 11g Standard Edition?
dinoAnalytic Functions were an EE feature in 8i and moved into the SE edition when 9i was released. To my knowledge Oracle hasn't changed anything in its licensing scheme regarding this feature. As a sidenote: analytic functions are also available in the XE edition.
C. -
SQL Analytical Functions in 9i
Hi
I am trying to determine of the SQL Analytical Functions in Oracle 9i like LAG, NTILE, PERCENT_RANK etc are part of Enterprise without OLAP installed.
They will be very useful to a set of queries I am trying to build but I don't want to add OLAP to the mix if I can help it as the customer won't be OLAP enabled
CheersHi
I am trying to determine of the SQL Analytical Functions in Oracle 9i like LAG, NTILE, PERCENT_RANK etc are part of Enterprise without OLAP installed.
They will be very useful to a set of queries I am trying to build but I don't want to add OLAP to the mix if I can help it as the customer won't be OLAP enabled
Cheers -
Does sql analytic function help to determine continuity in occurences
We need to solve this problem in a sql statement.
imagine a table test with two columns
create table test (id char(1), begin number, end number);
and these values
insert into test('a',1, 2);
insert into test('a',2,3);
insert into test('a',3,4);
insert into test('a',7,10);
insert into test('a',10,15);
insert into test('b',5,9);
insert into test('b',9,21);
insert into test('c',1,5);
our goal is to determine continuity in number sequence between begin and end attributes for a same id and determine min and max number from these contuinity chains.
The result may be
a, 1, 4
a, 7, 15
b, 5, 21
c, 1, 5
We test some analytic functions like lag, lead, row_number, min, max, partition by, etc to search a way to identify row set that represent a continuity but we didn't find a way to identify (mark) them so we can use min and max functions to extract extreme values.
Any idea is really welcome !Here is our implementation in a real context for example:
insert into requesterstage(requesterstage_i, requester_i, t_requesterstage_i, datefrom, dateto )
With ListToAdd as
(Select distinct support.requester_i,
support.datefrom,
support.dateto
from support
where support.datefrom < to_date('01.01.2006', 'dd.mm.yyyy')
and support.t_relief_i = t_relief_ipar.fgetflextypologyclassitem_i(t_relief_ipar.fismedicalexpenses)
and not exists
(select null
from requesterstage
where requesterstage.requester_i = support.requester_i
and support.datefrom < nvl(requesterstage.dateto, support.datefrom + 1)
and nvl(support.dateto, requesterstage.datefrom + 1) > requesterstage.datefrom)
ListToAddAnalyzed_1 as
(select requester_i,
datefrom,
dateto,
decode(datefrom,lag(dateto) over (partition by requester_i order by datefrom),0,1) data_set_start
from ListToAdd),
ListToAddAnalyzed_2 as
(select requester_i,
datefrom,
dateto,
data_set_start,
sum(data_set_start) over(order by requester_i, datefrom ) data_set_id
from ListToAddAnalyzed_1)
select requesterstage_iseq.nextval,
requester_i,
t_requesterstage_ipar.fgetflextypologyclassitem_i(t_requesterstage_ipar.fisbefore2006),
datefrom,
decode(sign(nvl(dateto, to_date('01.01.2006', 'dd.mm.yyyy')) -to_date('01.01.2006', 'dd.mm.yyyy')), 0, to_date('01.01.2006', 'dd.mm.yyyy'), -1, dateto, 1, to_date('01.01.2006', 'dd.mm.yyyy'))
from ( select requester_i
, min(datefrom) datefrom
, max(dateto) dateto
From ListToAddAnalyzed_2
group by requester_i, data_set_id
); -
Help with analytical function (ora 9...)
Hi everyone, is there a way to fill some missing numbers based on what have come before and after that missing number by starttime and based on how many missing data are between? ... by "simple" select? I know how to do that just theoreticly with simple math commands, but is there a way to apply them in sql (analytical functions)?
Thanks in advance for any ideas !
The missing number on line 17 could be calculated as 339+(1/2)*(356-339) = 347,5
The missing number on line 23 could be calculated as 355+(1/3)*(292-355) = 334
The missing number on line 24 could be calculated as 355+(2/3)*(292-355) = 313
rownumber + temp_table (starttime,data_column)
15 23.5.2007 16:15 , 258
16 23.5.2007 16:30 , 339
17 23.5.2007 16:45 ,
18 23.5.2007 17:00 , 356
19 23.5.2007 17:15 , 373
20 23.5.2007 17:30 , 355
21 23.5.2007 17:45 , 363
22 23.5.2007 18:00 , 355
23 23.5.2007 18:15 ,
24 23.5.2007 18:30 ,
25 23.5.2007 19:00 , 292
26 23.5.2007 19:15 , 295
THANKS
Message was edited by:
dusooWay too late, but I wouldn't let my effort go unpublished ;-)
SQL> create table temp_table
2 as
3 select 15 rownumber, to_date('23.5.2007 16:15','dd.mm.yyyy hh24:mi') starttime, 258 data_column from dual union all
4 select 16, to_date(' 23.5.2007 16:30','dd.mm.yyyy hh24:mi'), 339 from dual union all
5 select 17, to_date(' 23.5.2007 16:45','dd.mm.yyyy hh24:mi'), null from dual union all
6 select 18, to_date(' 23.5.2007 17:00','dd.mm.yyyy hh24:mi'), 356 from dual union all
7 select 19, to_date(' 23.5.2007 17:15','dd.mm.yyyy hh24:mi'), 373 from dual union all
8 select 20, to_date(' 23.5.2007 17:30','dd.mm.yyyy hh24:mi'), 355 from dual union all
9 select 21, to_date(' 23.5.2007 17:45','dd.mm.yyyy hh24:mi'), 363 from dual union all
10 select 22, to_date(' 23.5.2007 18:00','dd.mm.yyyy hh24:mi'), 355 from dual union all
11 select 23, to_date(' 23.5.2007 18:15','dd.mm.yyyy hh24:mi'), null from dual union all
12 select 24, to_date(' 23.5.2007 18:30','dd.mm.yyyy hh24:mi'), null from dual union all
13 select 25, to_date(' 23.5.2007 19:00','dd.mm.yyyy hh24:mi'), 292 from dual union all
14 select 26, to_date(' 23.5.2007 19:15','dd.mm.yyyy hh24:mi'), 295 from dual
15 /
Tabel is aangemaakt.
SQL> with t as
2 ( select t.*
3 , max(case when data_column is not null then rownumber end) over (order by rownumber) lowerbound
4 , last_value(data_column ignore nulls) over (order by rownumber) prevvalue
5 , min(case when data_column is not null then rownumber end) over (order by rownumber desc) upperbound
6 , last_value(data_column ignore nulls) over (order by rownumber desc) nextvalue
7 from temp_table t
8 )
9 select rownumber
10 , starttime
11 , case
12 when data_column is not null then data_column
13 else prevvalue * ((upperbound - rownumber) / (upperbound - lowerbound))
14 + nextvalue * ((rownumber - lowerbound) / (upperbound - lowerbound))
15 end data_column
16 from t
17 order by rownumber
18 /
ROWNUMBER STARTTIME DATA_COLUMN
15 23-05-2007 16:15:00 258
16 23-05-2007 16:30:00 339
17 23-05-2007 16:45:00 347,5
18 23-05-2007 17:00:00 356
19 23-05-2007 17:15:00 373
20 23-05-2007 17:30:00 355
21 23-05-2007 17:45:00 363
22 23-05-2007 18:00:00 355
23 23-05-2007 18:15:00 334
24 23-05-2007 18:30:00 313
25 23-05-2007 19:00:00 292
26 23-05-2007 19:15:00 295
12 rijen zijn geselecteerd.Regards,
Rob. -
Disco -- Any analytical functions for comparisons
Hi:
I'm wondering if there are any analytical function to help me out with comparisons? Users often need to displays totals based on date ranges, and show the difference between the two totals, as well as percent change.
For example, a workbook would show the comparison of cases and dollars, for 2002 vs. 2003. Currently, my solution for this is to create DECODE calculations based on year and type (cases or dollars), and perform the
comparisons in separate calculations. I'd like to know if Discoverer already has a function that would handle some of this, and reduce the number of DECODES and separate calculations the users have to create...
Thanks,
Subramanyam
Sr. Technical Consultant
Oracle DirectHello
You can use the Oracle database SQL analytic function to perform comparison and window based calculations. Example: LAG, LEAD, RANK, etc...
Please consult the Oracle 9.2 database documentation as well as Discoverer documentation for examples and syntax.
Regards
Discoverer Product Management -
OLAP Expression Analytical Functions and NA Values
Hello,
I am trying to use the SUM and MAX functions over a hierarchy where there are potentially NA values. I believe in OLAP DML, the natural behavior is to skip these values. Can a skip be accomplished with either the SUM or MAX OLAP Expression Syntax functions?
Cheers!Pre-requisites:
===============
Time dimension with level=DAY.... i have restricted data to 1 month approx.. 20100101 to 20100201 (32 days).
Measure of interest - a (say)
Time Dimension attribute which indicates WEEKDAY.... if you have END_DATE attribute with date datatype so we can extract the DAY (MON/TUE/WED/...) from it and decipher wkday/wkend status for DAY.
Sort time as per END_DATE ..
Take care of other dimensions during testing... restrict all other dimensions of cube to single value. Final formula would be independent of other dimensions but this helps development/testing.
Step 1:
======
"Firm up the required design in olap dml
"rpr down time
" w 10 heading 't long' time_long_description
" w 10 heading 't end date' time_end_date
" w 20 heading 'Day Type' convert(time_end_date text 'DY')
" a
NOTE: version 1 of moving total
" heading 'moving minus 2 all' movingtotal(a, -2, 0, 1, time status)
" w 20 heading 'Day Type' convert(time_end_date text 'DY')
" heading 'a wkday' if convert(time_end_date text 'DY') ne 'SAT' and convert(time_end_date text 'DY') ne 'SUN' then a else na
NOTE: version 2 of moving total
" heading 'moving minus 2 wkday' movingtotal(a, -2, 0, 1, time convert(time_end_date text 'DY') ne 'SAT' and convert(time_end_date text 'DY') ne 'SUN')
" w 20 heading 'Day Type' convert(time_end_date text 'DY')
" heading 'a wkday non-na' if convert(time_end_date text 'DY') ne 'SAT' and convert(time_end_date text 'DY') ne 'SUN' and a ne na then a else na
NOTE: version 3 of moving total
" heading 'moving minus 2 wkday non-na' movingtotal(a, -2, 0, 1, time convert(time_end_date text 'DY') ne 'SAT' and convert(time_end_date text 'DY') ne 'SUN' and a ne na)
OLAP DML Command:
rpr down time w 10 heading 't long' time_long_description w 10 heading 't end date' time_end_date w 20 heading 'Day Type' convert(time_end_date text 'DY') a heading 'moving minus 2 all' movingtotal(a, -2, 0, 1, time status) w 20 heading 'Day Type' convert(time_end_date text 'DY') heading 'a wkday' if convert(time_end_date text 'DY') ne 'SAT' and convert(time_end_date text 'DY') ne 'SUN' then a else na heading 'moving minus 2 wkday' movingtotal(a, -2, 0, 1, time convert(time_end_date text 'DY') ne 'SAT' and convert(time_end_date text 'DY') ne 'SUN') w 20 heading 'Day Type' convert(time_end_date text 'DY') heading 'a wkday non-na' if convert(time_end_date text 'DY') ne 'SAT' and convert(time_end_date text 'DY') ne 'SUN' and a ne na then a else na heading 'moving minus 2 wkday non-na' movingtotal(a, -2, 0, 1, time convert(time_end_date text 'DY') ne 'SAT' and convert(time_end_date text 'DY') ne 'SUN' and a ne na)
Step 2:
======
"Define additional measure to contain the required/desired formula implementing the business requirements (version 3 above)
" create formula AF1 which points to last column... i.e. OLAP_DML_EXPRESSION
dfn af1 formula movingtotal(a, -2, 0, 1, time convert(time_end_date text 'DY') ne 'SAT' and convert(time_end_date text 'DY') ne 'SUN' and a ne na)
"NOTE: Do this via AWM using calculated member with template type = OLAP_DML_EXPRESSION so that the cube view for cube contains a column for measure AF1
OLAP DML Command:
rpr down time w 10 heading 't long' time_long_description w 10 heading 't end date' time_end_date w 20 heading 'Day Type' convert(time_end_date text 'DY') a heading 'a wkday non-na' if convert(time_end_date text 'DY') ne 'SAT' and convert(time_end_date text 'DY') ne 'SUN' and a ne na then a else na heading 'moving minus 2 wkday non-na (AF1)' af1
->
Step 3:
=======
Extend Oracle OLAP with regular SQL functionality like SQL ANALYTICAL functions to fill up the gaps for intermediate week days like DAY_20100104 (TUE), DAY_20100105 (WED) etc.
Use: SQL Analytical Function LAST_VALUE() in query.. i.e. in report or query.. dont use AF1 but use LAST_VALUE(af1).... as below pseudo-code:
LAST_VALUE(cube_view.af1) over (partition by <product, organization, ... non-time dimensions> order by <DAY_KEY_Col> range unbounded preceeding and current row)
HTH
Shankar -
Hi all,
I want an help in creating my SQL query to extract the data described below:
I have one table example test containing data like below:
ID Desc Status
1 T1 DEACTIVE
2 T2 ACTIVE
3 T3 SUCCESS
4 T4 DEACTIVE
The thing i want to do is selecting all lines with ACTIVE status in this table but is there is no ACTIVE status, my query will give me the last line with DEACTIVE status.
Can I do this in one query by using analytical function for example, if yes can yiu help me on thaht query.
regards,
RaluceHi, Raluce,
Here's one way to do that:
WITH got_r_num AS
SELECT deptno, ename, job, hiredate
, ROW_NUMBER () OVER ( PARTITION BY deptno
ORDER BY job
, hiredate DESC
) AS r_num
FROM scott.emp
WHERE job IN ('ANALYST', 'CLERK')
SELECT deptno, ename, job, hiredate
FROM got_r_num
WHERE job = 'ANALYST'
OR r_num = 1
ORDER BY deptno
Since I don't have a sample version of your table, I used scott.emp to illustrate.
Output:
DEPTNO ENAME JOB HIREDATE
10 MILLER CLERK 23-JAN-82
20 SCOTT ANALYST 19-APR-87
20 FORD ANALYST 03-DEC-81
30 JAMES CLERK 03-DEC-81
This query finds all ANALYSTs in each department, regardless of how many there are. (Deptno 20 happens to have 2 ANALYSTs.) If there is no ANALYST in a department, then the most recently hired CLERK is included. (Deptnos 10 and 30 don't have any ANALYSTs.)
This "partitions", or sub-divides, the table into separate units, one for each department. In the problem you posted, it looks like you want to operate in the entire table, without sub-dividing it in any way. To do that, just omit the PARTITION BY clause in the analytic ROW_NUMBER function, like this:
WITH got_r_num AS
SELECT deptno, ename, job, hiredate
, ROW_NUMBER () OVER ( -- PARTITION BY deptno
ORDER BY job
, hiredate DESC
) AS r_num
FROM scott.emp
WHERE job IN ('ANALYST', 'CLERK')
SELECT deptno, ename, job, hiredate
FROM got_r_num
WHERE job = 'ANALYST'
OR r_num = 1
ORDER BY deptno -
SQL Query With analytical function
Hi
Below is the scenario which i am looking for in sql query using analytical functions
I/p
Col1
50
0
-150
-200
300
-100
-300
500
-100
O/p
Col1 col2
50 0
0 0
-150 -100
-200 -200
300 0
-100 0
-300 -100
500 400
-100 0Any help really appreciated
Thanks in advance
Edited by: unique on Aug 10, 2010 4:53 AM
Edited by: unique on Aug 10, 2010 4:55 AM
Edited by: unique on Aug 10, 2010 4:55 AMOh,In this case,There is OLAP solution ;-)
OLAP samples of my homepage http://www.geocities.jp/oraclesqlpuzzle/oracle-sql1-olap.html
with work(SK,Val) as(
select 1, 50 from dual union
select 2, 0 from dual union
select 3,-150 from dual union
select 4,-200 from dual union
select 5, 300 from dual union
select 6,-100 from dual union
select 7,-300 from dual union
select 8, 500 from dual union
select 9,-100 from dual)
select SK,Val,GID,
case when Val > 0
then greatest(0,sum(Val) over(partition by GID))
else Least(0,Val+greatest(0,sum(Val) over(partition by GID
order by SK rows between unbounded preceding
and 1 preceding)))
end as COL3
from (select SK,Val,
sum(greatest(0,sign(Val))) over(order by SK) as GID
from work)
order by SK;
SK VAL GID COL3
1 50 1 0
2 0 1 0
3 -150 1 -100
4 -200 1 -200
5 300 2 0
6 -100 2 0
7 -300 2 -100
8 500 3 400
9 -100 3 0 -
Analytic Functions in PL/SQL
This procedure won't compile - the word PARTITION seems to be the problem - with this error...
PLS-00103: Encountered the symbol "(" when expecting one of the following: , from
The query in the cursor runs correctly as a stand-alone query. Can analytic functions not be used in PL/SQL cursors?
Thanks.
CREATE OR REPLACE
PROCEDURE TestAnalyticFunction IS
CURSOR GetAllTransTypes_Cursor IS
select transaction_class.trans_desc,
transaction_code.trans_type ,
transaction_code.trans_code,
transaction_code.trans_code_desc,
sum(tr_tx_amt) as trans_sum,
RATIO_TO_REPORT(sum(tr_tx_amt)) OVER
(PARTITION BY transaction_code.trans_type) AS Percentage
from transaction_code,
transaction_class,
transactions
where TR_POST_DT IS NOT NULL
AND TR_POST_DT >= '01-DEC-2000'
AND TR_POST_DT <= '31-JAN-2001'
AND TRANSACTION_CODE.TRANS_CLASS = TRANSACTION_CLASS.TRANS_CLASS_ID
AND TRANSACTION_CODE.TRANS_CODE = TRANSACTIONS.TR_TX_CODE
AND TRANSACTION_CODE.TRANS_TYPE in (1,2,3,4,5,8)
group by transaction_code.trans_type,
trans_class,
trans_desc,
trans_code,
trans_code_desc
order by transaction_code.trans_type, trans_code;
TYPE TransClassDescType IS TABLE OF transaction_class.trans_desc%TYPE;
TYPE TransCodeTypeType IS TABLE OF transaction_code.trans_type%TYPE;
TYPE TransCodeCodeType IS TABLE OF transaction_code.trans_code%TYPE;
TYPE TransCodeDescType IS TABLE OF transaction_code.trans_code_desc%TYPE;
TYPE TotalType IS TABLE OF NUMBER(14,2);
TYPE TotalPctType IS TABLE OF NUMBER(6, 2);
TransClassDesc TransClassDescType;
TransCodeType TransCodeTypeType;
TransCodeCode TransCodeCodeType;
TransCodeDesc TransCodeDescType;
Total TotalType;
TotalPct TotalPctType;
BEGIN
OPEN GetAllTransTypes_Cursor;
FETCH GetAllTransTypes_Cursor BULK COLLECT INTO TransClassDesc,TransCodeType,TransCodeCode,TransCodeDesc,
Total, TotalPct;
CLOSE GetAllTransTypes_Cursor;
END TestAnalyticFunction;
nullSome functions just don't seem to work in PL/SQL even though they work fine in SQL*Plus.
Two such functions I found were NVL2 and RATIO_TO_REPORT.
Have no clue why yet.
<BLOCKQUOTE><font size="1" face="Verdana, Arial, Helvetica">quote:</font><HR>Originally posted by Dale Johnson ([email protected]):
This procedure won't compile - the word PARTITION seems to be the problem - with this error...
PLS-00103: Encountered the symbol "(" when expecting one of the following: , from
The query in the cursor runs correctly as a stand-alone query. Can analytic functions not be used in PL/SQL cursors?
Thanks.
CREATE OR REPLACE
PROCEDURE TestAnalyticFunction IS
CURSOR GetAllTransTypes_Cursor IS
select transaction_class.trans_desc,
transaction_code.trans_type ,
transaction_code.trans_code,
transaction_code.trans_code_desc,
sum(tr_tx_amt) as trans_sum,
RATIO_TO_REPORT(sum(tr_tx_amt)) OVER
(PARTITION BY transaction_code.trans_type) AS Percentage
from transaction_code,
transaction_class,
transactions
where TR_POST_DT IS NOT NULL
AND TR_POST_DT >= '01-DEC-2000'
AND TR_POST_DT <= '31-JAN-2001'
AND TRANSACTION_CODE.TRANS_CLASS = TRANSACTION_CLASS.TRANS_CLASS_ID
AND TRANSACTION_CODE.TRANS_CODE = TRANSACTIONS.TR_TX_CODE
AND TRANSACTION_CODE.TRANS_TYPE in (1,2,3,4,5,8)
group by transaction_code.trans_type,
trans_class,
trans_desc,
trans_code,
trans_code_desc
order by transaction_code.trans_type, trans_code;
TYPE TransClassDescType IS TABLE OF transaction_class.trans_desc%TYPE;
TYPE TransCodeTypeType IS TABLE OF transaction_code.trans_type%TYPE;
TYPE TransCodeCodeType IS TABLE OF transaction_code.trans_code%TYPE;
TYPE TransCodeDescType IS TABLE OF transaction_code.trans_code_desc%TYPE;
TYPE TotalType IS TABLE OF NUMBER(14,2);
TYPE TotalPctType IS TABLE OF NUMBER(6, 2);
TransClassDesc TransClassDescType;
TransCodeType TransCodeTypeType;
TransCodeCode TransCodeCodeType;
TransCodeDesc TransCodeDescType;
Total TotalType;
TotalPct TotalPctType;
BEGIN
OPEN GetAllTransTypes_Cursor;
FETCH GetAllTransTypes_Cursor BULK COLLECT INTO TransClassDesc,TransCodeType,TransCodeCode,TransCodeDesc,
Total, TotalPct;
CLOSE GetAllTransTypes_Cursor;
END TestAnalyticFunction;<HR></BLOCKQUOTE>
null -
Tuning sql with analytic function
Dear friends I've developed one sql :
with REP as
(select /*+ MATERIALIZE */ branch_code,
row_number() over(partition by branch_code, account order by bkg_date desc ) R,
account,
bkg_date,
lcy_closing_bal
from history t
select REP1.branch_code,
REP1.account,
REP1.bkg_date,
REP1.lcy_closing_bal,
NULL AS second,
REP2.bkg_date bkg_date2,
REP2.lcy_closing_bal lcy_closing_bal2,
NULL AS third,
REP3.bkg_date bkg_date3,
REP3.lcy_closing_bal lcy_closing_bal3
from (SELECT * FROM REP WHERE R=1) REP1, (SELECT * FROM REP WHERE R=2) REP2, (SELECT * FROM REP WHERE R=3) REP3
where
(REP1.BRANCH_CODE = REP2.BRANCH_CODE(+) AND REP1.ACCOUNT = REP2.ACCOUNT(+)) AND
(REP1.BRANCH_CODE = REP3.BRANCH_CODE(+) AND REP1.ACCOUNT = REP3.ACCOUNT(+))The point is I want to restrict (tune) REP before it used ,because , as you can see I need maximum three value from REP (where R=1,R=2,R=3) . Which analytic function and with wich options I have to use to receive only 3 values in each branch_code,account groups at the materializing time ?Radrigez wrote:
Dear friends I've developed one sql :
with REP as
from (SELECT * FROM REP WHERE R=1) REP1,
(SELECT * FROM REP WHERE R=2) REP2,
(SELECT * FROM REP WHERE R=3) REP3
where
(REP1.BRANCH_CODE = REP2.BRANCH_CODE(+) AND REP1.ACCOUNT = REP2.ACCOUNT(+)) AND
(REP1.BRANCH_CODE = REP3.BRANCH_CODE(+) AND REP1.ACCOUNT = REP3.ACCOUNT(+))
The first step is to put your subquery (which doesn't need to be materialized) into an inline view and restrict the result set on r in (1,2,3) as suggested by thtsang - you don't need to query the same result set three times.
Then you're looking at a simple pivot operation (assuming the number of rows you want per branch and account is fixed). If you're on 11g search the manuals for PIVOT, on earlier versions you can do this with a decode() or case() operator.
Step 1 (which could go into another factored subquery) would be something like:
select
branch_code, account,
case r = 1 then bkg_date end bkg_date,
case r = 1 then lcy_closing_bal end lcy_closing_bal,
case r = 2 then bkg_date end bkg_date2,
case r = 2 then lcy_closing_bal end lcy_closing_bal2,
case r = 3 then bkg_date end bkg_date3,
case r = 3 then lcy_closing_bal end lcy_closing_bal3
from
repThis gives you the eight necessary columns, but still (up to) three rows per branch/account.
Then you aggregate this (call it rep1) on branch and account.
select
branch_code, account,
max(bkg_date),
max(lcy_closing_bal),
max(bkg_date2),
max(lcy_closing_bal2),
max(bkg_date3),
max(lcy_closing_bal3)
from
rep1
group by
branch_code, account
order by
branch_code, accountRegards
Jonathan Lewis
http://jonathanlewis.wordpress.com
Author: <b><em>Oracle Core</em></b> -
Surprised that 1Z0-047 (SQL Expert) does not include Analytic Functions
Apparently, from a reading of the 1Z0-047 outline, Analytic Functions are not "in scope" for the Exam.
Comments, anyone ?
Hemant K ChitaleComments, anyone ?
There are definitely some gaps in the SQL coverage on that exam. I could see the exam being redone (and improved) for 12C much as 1Z0-144 was rewritten for 11G as a much improved version of 1Z0-147. I'd like to see them remove the individual listing of topics pulled from SQL Fundamentals. They should be replaced with either a much-compressed list or a single note that knowledge of SQL fundamentals is assumed. This might help with the level of confusion newbies have over which test they should take.
Adding all of the functions you mention would not only make the test more comprehensive but also allow the tone of the exam to be altered. Right now I consider it to be filled with questions intended to misdirect the test candidate. The questions are 'tricky' rather than 'difficult'. This is largely because SQL is inherently easy to read so the only way to make it hard *is* to write deliberately bad SQL. Adding more advanced topics would allow for more questions that don't have to be made artificially more difficult. -
Table with 2 columns pro_id,sub_ver_id. Need only 5 pro_id for each sub_ver_id
SQL> select * from test1 order by SUB_VER_ID;
PRO_ID SUB_VER_ID
1 0
2 0
3 0
4 0
5 0
6 0
10 1
15 1
16 1
11 1
12 1
PRO_ID SUB_VER_ID
13 1
14 1
11 2
12 3
I'm new to analytic function i got query as below, but not able to get an idea to limit the SRLNO to only 5 rows for each SUB_VER_ID. Any hint would be much appreciated.
select distinct sub_ver_id,pro_id, row_number () over (order by sub_ver_id) srlno
from test1 order by sub_ver_idMay be as below..
select *
from
select sub_ver_id,pro_id, row_number () over (partition by sub_ver_id order by null) srlno
from test1
) where srlno <=5 order by sub_ver_idThanks... -
Ugent: Regarding Oracle 10g Analytic functions
Hi,
EMP TABLE:
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
==================================================================
7369 SMITH CLERK 7902 12/17/1980 800 20
7499 ALLEN SALESMAN 7698 2/20/1981 1600 300 30
7521 WARD SALESMAN 7698 2/22/1981 1250 500 30
7566 JONES MANAGER 7839 4/2/1981 2975 20
7654 MARTIN SALESMAN 7698 9/28/1981 1250 1400 30
7698 BLAKE MANAGER 7839 5/1/1981 2850 30
7782 CLARK MANAGER 7839 6/9/1981 2450
7788 SCOTT ANALYST 7566 12/9/1982 3000 20
7839 KING PRESIDENT 11/17/1981 5000
7844 TURNER SALESMAN 7698 9/8/1981 1500 0 30
7876 ADAMS CLERK 7788 1/12/1983 1100 20
7900 JAMES CLERK 7698 12/3/1981 950 30
7902 FORD ANALYST 7566 12/3/1981 3000 20
7934 MILLER CLERK 7782 1/23/1982 1300
================================================================
I would like the output group by manger and the employees under that manager using analytic functions.
Output should look like:
ManagerName EMPNAME
==========================================================
KING JONES,BLAKE,CLARK
JONES SCOTT,FORD
BLAKE ALLEN,WARD,MARTIN,TURNER,JAMES
CLARK MILLER
FORD SMITH
SCOTT ADAMS
Also I would like to run this query in unix shell script in order to create a folder structure like this:
Root Folder: King -> Jones -> SCOTT -> ADAMS
-> FORD -> SMITH
-> BLAKE -> ALLEN
-> WARD
-> MARTIN
-> TURNER
-> JAMES
-> CLARK -> MILLER
On a total 14 folders should be created.
Thanks in Advance
G.Vamsi Krishna
Edited by: user10733211 on Apr 20, 2009 11:30 PMuser10633982 wrote:
hey guys can you please give your personal opinions and remarks out of this thread.
this thread is supposed to be solving query and not for chit chattingNot chit chatting and not just personal opinions, just letting you know how it works around here...
Urgent is it?
Why? Have you forgotten to do your coursework and you'll get thrown off your course if you don't hand it in today?
What makes you believe that your request for help is more important than someone else who has requested help? It's very rude to assume you are more important than somebody else, and I'm sure they would like an answer to their issue as soon as they can get one too, but they've generally been polite and not demanded that it is urgent.
Also, you assume that people giving answers are all sitting here just waiting to answer your question for you. That's not so. We're all volunteers with our own jobs to do. How dare you presume to demand our attention with urgency.
If you want help and you want it answering quickly you simply just put your issue forward and provide as much valuable information as possible.
You will find if you post on here demanding your post is urgent then most people will just ignore it, some will tell you to get lost, and some will explain to you why you shouldn't post "urgent" requests. Occasionally you may find somebody who's got nothing better to do who will actually provide you with an answer, but you really are limiting your options by not asking properly.
How can something being run against the SCOTT schema be something that is "urgent"?
For the first part of your enquiry:
SQL> ed
Wrote file afiedt.buf
1 with emps as (select ename, mgr, row_number() over (partition by mgr order by ename) as rn from emp)
2 select ename as managername
3 ,(select ltrim(sys_connect_by_path(emps.ename,','),',')
4 from emps
5 where emps.mgr = emp.empno
6 and connect_by_isleaf = 1
7 connect by rn = prior rn + 1 and mgr = prior mgr
8 start with rn = 1
9 ) as empname
10 from emp
11* where empno in (select mgr from emp)
SQL> /
MANAGERNAM EMPNAME
JONES FORD,SCOTT
BLAKE ALLEN,JAMES,MARTIN,TURNER,WARD
CLARK MILLER
SCOTT ADAMS
KING BLAKE,CLARK,JONES
FORD SMITH
6 rows selected.
SQL>As for using that output in a unix shell script to create directory structures you should consider asking in a unix forum. -
HTMLDB 1.6 and "order by" in analytic functions
In HTMLDB 1.6, oracle 10g, when i enter the string "order by" in the region source of a report of the type "sql query (pl/sql function body returning sql query", I get
1 error has occurred
* Your query can't include an "ORDER BY" clause when having column heading sorting enabled.
I understand the reason for this error, but unfortunately i need this for an analytic function:
row_number() over (partition by ... order by ...)
It seems that the check is performed by simply looking for the string "order by" in the "region source" (in fact the error fires even if that string is contained within a comment).
I know possible workarounds (eg creating a view and select'ing from it), i just wanted to let you know.
Regards
AlbertoAnother one under the 'obvious route' category:
Seems that the ORDER BY check is apparentl for ORDER<space>BY... so simply adding extra whitespace between ORDER and BY bypasses the check (at least in 2.1.0.00.39).
To make it a bit more obious that a separation is intended, an empty comment, i.e. ORDER/*/BY*, works nicely
Edited by: mcstock on Nov 19, 2008 10:29 AM
Maybe you are looking for
-
Error when trying to save in Table Layout
Hi, I am using JDeveloper 10.1.3.1.0.3984 and JHeadstart 10.1.3.1 release 10.1.3.1.26 I have some groups with Table and Form layout. In some table layout - Multi-Row Update is allowed. In those table layouts, if I want to save after update then it gi
-
Problem with the wrong host name, when accessing IR or ID
Hey all, I am trying to accessing the IR and ID at my client however the wrong host name is being called. For instance the URL should start nero.bcm.tmc.edu, but it is calling just nero. The basis guy here fixed a similar problem in the exchange prof
-
Problem with currency field on screens
hai gurus i am using currency field on screen , when i am trying to display 0.0 value on the screen it showing blank , how to display the value zero on the screen thanx in advance afzal
-
Hi All, I have the following error in my flex application. How can i solve this error? quote: Could not resolve <fc:Column3DChart> to a component implementation Thanks, Srinivas r
-
How do I add SD (Sales&Distribution) tab to Service master? I have activated the SD Checkbox against the Service category/Org status in IMG. Still, the SD tab does not appear in the Service master Request expert advise from you guys Thanks