Analytical Function in Oracle 8.1.5
I am using the following sql
select empno,ename,count(*) over()
from emp;
But it shows the error
ORA-00923: FROM keyword not found where expected
I am using Oracle 8.1.5
pls help me
Analytical function were introduced in ORACLE 8iBut only for the Enterprise Edition. If the OP has the Standard Edition they can't use Analytic functions.
Cheers, APC
Similar Messages
-
I have a situation where i have partitioned a record set. If in any of the partition on that recordset, the value of one field(field name- status) is '45' i need to order the result of that partition by- 'outdate' desc, 'receiveddate' desc and order the other partition by 'key' desc,' sequence' desc, 'outdate' desc.
So the query looks like -
select row_number() over (partition by key order by sequence) RowNo, key, seq, status, outdate, receivedate from table1 where .........
order by ????
RowNo Key Seq status outdate receiveddate
1 200 0 24 9/13/2009 9/12/2009
2 200 1 23 9/10/2009 9/09/2009
3 200 2 24 9/09/2009 9/08/2009
1 210 0 24 9/13/2009 9/12/2009
2 210 1 * 45 * 9/09/2009 9/08/2009
3 210 2 24 9/10/2009 9/09/2009
So i need to get the query that will order the first set of partition by 'key' desc,' sequence' desc, 'outdate' desc and the Second set of partition (since the status of '45' exist in second partition) by 'outdate' desc, 'receiveddate' desc .
The output of the query should look like
RowNo Key Seq status outdate receiveddate
1 200 0 24 9/13/2009 9/12/2009
2 200 1 23 9/10/2009 9/09/2009
3 200 2 24 9/09/2009 9/08/2009
1 210 0 24 9/13/2009 9/12/2009
2 210 2 24 9/10/2009 9/09/2009
3 210 1 *45 * 9/09/2009 9/08/2009
I am not sure if this is possible using Analytical function.
I would really appreciate if any one can light me on this.
Thanks in advanceHi,
Welcome to the forum!
You can use analytic functions in the ORDER BY clause.
I don't have your tables, so I'll use scott.emp to illustrate.
The following query sorts by deptno first. After that, the sort order for departments that contain at least one Salesman is:
(a) job
(b) ename
Deptno=30 happens to be the only department with a Salesman, so it is the only one sorted as above.
The other departements will be sorted by
(a) sal
(b) job
SELECT deptno
, ename
, job
, sal
FROM scott.emp
ORDER BY deptno
, CASE
WHEN COUNT ( CASE
WHEN job = 'SALESMAN'
THEN 1
END
) OVER (PARTITION BY deptno) > 0
THEN ROW_NUMBER () OVER ( PARTITION BY deptno
ORDER BY job
, ename
ELSE ROW_NUMBER () OVER ( PARTITION BY deptno
ORDER BY sal
, job
END
;Output:
. DEPTNO ENAME JOB SAL
10 MILLER CLERK 1300
10 CLARK MANAGER 2450
10 KING PRESIDENT 5000
20 SMITH CLERK 800
20 ADAMS CLERK 1100
20 JONES MANAGER 2975
20 SCOTT ANALYST 3000
20 FORD ANALYST 3000
30 JAMES CLERK 950
30 BLAKE MANAGER 2850
30 ALLEN SALESMAN 1600
30 MARTIN SALESMAN 1250
30 TURNER SALESMAN 1500
30 WARD SALESMAN 1250
In the small set of sample data you posted, the results you want can be obtained simply using
ORDER BY key
, outdate DESCI assume that's just a coincidence.
If you need help, post some sample data that really requires looking at the status column to get the right results. Post the data in some executable form, such as CREATE TABLE and INSERT statements, olr, as Salim did, a WITH clause. (Perhaps you can just add or change a couple of rows in the sample data Salim already posted.) -
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 -
Advantages and disadvantages of Analytical function
Plz list out the advantages and disadvantages of normal queries and queries using analytical function (Performance wise)
I'm not sure how you wish to compare?
Analytical functions give you functionality that cannot otherwise be achieved easily in a lot of cases. They can introduce some performance degredation to a query but you have to compare on a query by query basis to determine if analytical functions or otherwise are the best solution for the issue. If it were as simple as saying that analytical functions are always slower than doing it without analytical functions, then Oracle wouldn't bother introducing them into the language. -
Running Sum without analytic function
Hi
I have data like below
Create table Test (Name Varchar(30),M Int, Y Int, Val Int);
Insert into Test Values ('A',1,2011,2);
Insert into Test Values ('A',2,2011,2);
Insert into Test Values ('A',3,2011,2);
Insert into Test Values ('A',4,2011,2);
Insert into Test Values ('A',5,2011,2);
Insert into Test Values ('A',6,2011,2);
Insert into Test Values ('A',7,2011,2);
Insert into Test Values ('A',8,2011,2);
Insert into Test Values ('A',9,2011,2);
Insert into Test Values ('A',10,2011,2);
Insert into Test Values ('A',11,2011,2);
Insert into Test Values ('A',12,2011,2);
Insert into Test Values ('A',1,2012,2);
Insert into Test Values ('A',2,2012,2);
Insert into Test Values ('A',3,2012,2);
Insert into Test Values ('A',4,2012,2);
Insert into Test Values ('A',5,2012,2);
Insert into Test Values ('A',6,2012,2);
Insert into Test Values ('A',7,2012,2);
Now based on above data I need to calculate running sum for past 18 Months. Condition is I can not use analytic function or Oracle specific SQL functions (for portability).
I tries following SQL but it dint work
select Name,rnk, SUM(val) from (
SELECT a.Name,a.m,a.Y,b.val, count(*) rnk
from Test a, Test b
where (a.Name=b.Name and (a.M <= b.M and a.Y<= b.Y))
group by a.Name,a.Y,a.m
order by a.Name,a.Y,a.m
) abc
group By Name,rnk
Order by Name,rnk
Can some one give suggastion.Hi,
I don't see what your query or your desired results have to do with the last 18 months. Is the task here to show for a given month (July, 2012, for example) the total of the 18 months ending in that month (February, 2011 through July, 2012 in this case) for the same name? If so:
SELECT c.name, c.y, c.m
, SUM (p.val) AS running_total
FROM test c
JOIN test p ON ( ((12 * c.y) + c.m)
- ((12 * p.y) + p.m)
) BETWEEN 0 AND 17
GROUP BY c.name, c.y, c.m
ORDER BY c.name, c.y, c.m
;Output:
NAME Y M RUNNING_TOTAL
A 2011 1 2
A 2011 2 4
A 2011 3 6
A 2011 4 8
A 2011 5 10
A 2011 6 12
A 2011 7 14
A 2011 8 16
A 2011 9 18
A 2011 10 20
A 2011 11 22
A 2011 12 24
A 2012 1 26
A 2012 2 28
A 2012 3 30
A 2012 4 32
A 2012 5 34
A 2012 6 36
A 2012 7 36 -
Replacing Oracle's FIRST_VALUE and LAST_VALUE analytical functions.
Hi,
I am using OBI 10.1.3.2.1 where, I guess, EVALUATE is not available. I would like to know alternatives, esp. to replace Oracle's FIRST_VALUE and LAST_VALUE analytical functions.
I want to track some changes. For example, there are four methods of travel - Air, Train, Road and Sea. Would like to know traveler's first method of traveling and the last method of traveling in an year. If both of them match then a certain action is taken. If they do not match, then another action is taken.
I tried as under.
1. Get Sequence ID for each travel within an year per traveler as Sequence_Id.
2. Get the Lowest Sequence ID (which should be 1) for travels within an year per traveler as Sequence_LId.
3. Get the Highest Sequence ID (which could be 1 or greater than 1) for travels within an year per traveler as Sequence_HId.
4. If Sequence ID = Lowest Sequence ID then display the method of travel as First Method of Travel.
5. If Sequence ID = Highest Sequence ID then display the method of travel as Latest Method of Travel.
6. If First Method of Travel = Latest Method of Travel then display Yes/No as Match.
The issue is cells could be blank in First Method of Travel and Last Method of Travel unless the traveler traveled only once in an year.
Using Oracle's FIRST_VALUE and LAST_VALUE analytical functions, I can get a result like
Traveler | Card Issue Date | Journey Date | Method | First Method of Travel | Last Method of Travel | Match?
ABC | 01/01/2000 | 04/04/2000 | Road | Road | Air | No
ABC | 01/01/2000 | 15/12/2000 | Air | Road | Air | No
XYZ | 01/01/2000 | 04/05/2000 | Train | Train | Train | Yes
XYZ | 01/01/2000 | 04/11/2000 | Train | Train | Train | Yes
Using OBI Answers, I am getting something like this.
Traveler | Card Issue Date | Journey Date | Method | First Method of Travel | Last Method of Travel | Match?
ABC | 01/01/2000 | 04/04/2000 | Road | Road | <BLANK> | No
ABC | 01/01/2000 | 15/12/2000 | Air | <BLANK> | Air | No
XYZ | 01/01/2000 | 04/05/2000 | Train | Train | <BLANK> | No
XYZ | 01/01/2000 | 04/11/2000 | Train | <BLANK> | Train | No
Above, for XYZ traveler the Match? clearly shows a wrong result (although somehow it's correct for traveler ABC).
Would appreciate if someone can guide me how to resolve the issue.
Many thanks,
Manoj.
Edited by: mandix on 27-Nov-2009 08:43
Edited by: mandix on 27-Nov-2009 08:47Hi,
Just to recap, in OBI 10.1.3.2.1, I am trying to find an alternative way to FIRST_VALUE and LAST_VALUE analytical functions used in Oracle. Somehow, I feel it's achievable. I would like to know answers to the following questions.
1. Is there any way of referring to a cell value and displaying it in other cells for a reference value?
For example, can I display the First Method of Travel for traveler 'ABC' and 'XYZ' for all the rows returned in the same column, respectively?
2. I tried RMIN, RMAX functions in the RDP but it does not accept "BY" clause (for example, RMIN(Transaction_Id BY Traveler) to define Lowest Sequence Id per traveler). Am I doing something wrong here? Why can a formula with "BY" clause be defined in Answers but not the RPD? The idea is to use this in Answers. This is in relation to my first question.
Could someone please let me know?
I understand that this thread that I have posted is related to something that can be done outside OBI, but still would like to know.
If anything is not clear please let me know.
Thanks,
Manoj. -
Grouping error in Oracle's analytic function PERCENTILE_CONT()
Hi,
I have a question regarding the usage of Oracle's analytic function PERCENTILE_CONT(). The underlying time data in the table is of hourly granularity and I want to fetch average, peak values for the day along with 80th percentile for that day. For the sake of clarification I am only posting relevant portion of the query.
Any idea how to rewrite the query and achieve the same objective?
SELECT TRUNC (sdd.ts) AS ts,
max(sdd.maxvalue) AS max_value, avg(sdd.avgvalue) AS avg_value,
PERCENTILE_CONT(0.80) WITHIN GROUP (ORDER BY sdd.avgvalue ASC) OVER (PARTITION BY pm.sysid,trunc(sdd.ts)) as Percentile_Cont_AVG
FROM XYZ
WHERE
XYZ
GROUP BY TRUNC (sdd.ts)
ORDER BY TRUNC (sdd.ts)
Oracle Error:
ERROR at line 5:
ORA-00979: not a GROUP BY expressionYou probably mixed up the aggregate and analytical versin of PERCENTILE_CONT.
The below should work, but i dont know if it produces the desireed results.
SELECT TRUNC (sdd.ts) AS ts,
max(sdd.maxvalue) AS max_value, avg(sdd.avgvalue) AS avg_value,
PERCENTILE_CONT(0.80) WITHIN GROUP (ORDER BY sdd.avgvalue ASC) as Percentile_Cont_AVG
FROM XYZ
sorry, what is this where clause for??
WHERE
XYZ
GROUP BY TRUNC (sdd.ts)
ORDER BY TRUNC (sdd.ts) Edited by: chris227 on 26.03.2013 05:45 -
Oracle Analytic function tuning
Hi all,
Oracle Database 10g Enterprise Edition Release 10.2.0.3.0 - 64bi
PL/SQL Release 10.2.0.3.0 - Production
CORE 10.2.0.3.0 Production
TNS for IBM/AIX RISC System/6000: Version 10.2.0.3.0 - Productio
NLSRTL Version 10.2.0.3.0 - Production
I have a query which has analytic function uses large space on temporary tablespace resulting in direct path temp read and direct path temp write wait events taking too much time. Is there any way to tune such query?
Thanks in advance.user9074365 wrote:
Hi all,
Oracle Database 10g Enterprise Edition Release 10.2.0.3.0 - 64bi
PL/SQL Release 10.2.0.3.0 - Production
CORE 10.2.0.3.0 Production
TNS for IBM/AIX RISC System/6000: Version 10.2.0.3.0 - Productio
NLSRTL Version 10.2.0.3.0 - Production
I have a query which has analytic function uses large space on temporary tablespace resulting in direct path temp read and direct path temp write wait events taking too much time. Is there any way to tune such query?
With your version of Oracle, and high-volumes of data going through analytic function, it's likely that this blog note applies. You may need an upgrade or special patch. http://jonathanlewis.wordpress.com/2009/09/07/analytic-agony/
Regards
Jonathan Lewis -
Help with Oracle Analytic Function scenario
Hi,
I am new to analytic functions and was wondering if someone could help me with the data scenario below. I have a table with the following data
COLUMN A COLUMN B COLUMN C
13368834 34323021 100
13368835 34438258 50
13368834 34438258 50
13368835 34323021 100
The output I want is
COLUMN A COLUMN B COLUMN C
13368834 34323021 100
13368835 34438258 50
A simple DISTINCT won't give me the desired output so i was wondering if there is any way that I can get the result using ANALYTIC FUNCTIONS and DISTINCT ..
Any help will be greatly appreciated.
Thanks.Hi,
Welcome to the forum!
Whenever you have a question, please post your sample data in a form that people can use to re-create the problem and test their solutions.
For example:
CREATE TABLE table_x
( columna NUMBER
, columnb NUMBER
, columnc NUMBER
INSERT INTO table_x (columna, columnb, columnc) VALUES (13368834, 34323021, 100);
INSERT INTO table_x (columna, columnb, columnc) VALUES (13368835, 34438258, 50);
INSERT INTO table_x (columna, columnb, columnc) VALUES (13368834, 34438258, 50);
INSERT INTO table_x (columna, columnb, columnc) VALUES (13368835, 34323021, 100);Do you want something that works in your version or Oracle? Of course you do! So tell us which version that is.
How do you get the results that you want? Explain what each row of output represents. It looks like
the 1st row contains the 1st distinct value from each column (where "first" means descending order for columnc, and ascending order for the others),
the 2nd row contains the 2nd distinct value,
the 3rd row contains the 3rd distinct value, and so on.
If that's what you want, here's one way to get it (in Oracle 9 and up):
WITH got_nums AS
SELECT columna, columnb, columnc
, DENSE_RANK () OVER (ORDER BY columna ) AS a_num
, DENSE_RANK () OVER (ORDER BY columnb ) AS b_num
, DENSE_RANK () OVER (ORDER BY columnc DESC) AS c_num
FROM table_x
SELECT MAX (a.columna) AS columna
, MAX (b.columnb) AS columnb
, MAX (c.columnc) AS columnc
FROM got_nums a
FULL OUTER JOIN got_nums b ON b.b_num = a.a_num
FULL OUTER JOIN got_nums c ON c.c_num = COALESCE (a.a_num, b.b_num)
GROUP BY COALESCE (a.a_num, b.b_num, c.c_num)
ORDER BY COALESCE (a.a_num, b.b_num, c.c_num)
;I've been trying to find a good name for this type of query. The best I've heard so far is "Prix Fixe Query", named after the menus where you get a choice of soups (listed in one column), appetizers (in another column), main dishes (in a 3rd column), and so on. The items on the first row don't necessaily have any relationship to each other.
The solution does not assume that there are the same number of distinct items in each column.
For example, if you add this row to the sample data:
INSERT INTO table_x (columna, columnb, columnc) VALUES (13368835, 34323021, 99);which is a copy of the last row, except that there is a completely new value for columnc, then the output is:
` COLUMNA COLUMNB COLUMNC
13368834 34323021 100
13368835 34438258 99
50starting in Oracle 11, you can also do this with an unpivot-pivot query. -
Oracle Analytic Function Issue
Hi, I have created a simple table contaning 3 columns and 3 records.
Insert into MYTABLE (ID, AMOUNT, RESULT) Values (1, 1, 1);
Insert into MYTABLE (ID, AMOUNT, RESULT) Values (2, 4, 1);
Insert into MYTABLE (ID, AMOUNT, RESULT) Values (3, 7, 0);
COMMIT;
I can SUM the AMOUNT using the analytic functions as
SELECT ID, AMOUNT, RESULT, SUM(AMOUNT) OVER() as TOTAL
FROM MYTABLE;
ID AMOUNT RESULT TOTAL
1 1 1 12
2 4 1 12
3 7 0 12
What I want to be able to do is summing the AMOUNTs by RESULT in this case 0 and 1.
To get the following result, how should I rewrite the query?
ID AMOUNT RESULT TOTAL RESULT_0_TOTAL RESULT_1_TOTAL
1 1 1 12 7 5
2 4 1 12 7 5
3 7 0 12 7 5SELECT ID, AMOUNT, RESULT
, SUM(CASE WHEN RESULT = 0 THEN AMOUNT ELSE 0 END) OVER() as TOTAL_0
, SUM(CASE WHEN RESULT = 1 THEN AMOUNT ELSE 0 END) OVER() as TOTAL_1
FROM MYTABLE; -
BO4 - Oracle Analytic Function within agg_aware measure
Hi,
We can use an analytic function within idt tool and when in a measures by itself it parses ok.
However when include in an agg_aware measure it gives an ORA-00937 not a single group group function.
When create queries with the agg_aware measure behaves ok, generates sql ok and runs ok - please advise.
ThanksNice, am I coming in here to read the English docs again?
-
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. -
Need some kind of Analytical Function
Hi Oracle experts
I need a little help from you experts. I have a PARTY table as listed below
The existing data
Party key ID_INTERNAL EID BID
1 11111 123
1 11111 321
1 22222 321 899
1 66666 ------ 888
New records comes
I have to assign a party key to each record based on which attribute is matching
Now the situation is as new records comes.
New records comes
ID_INTERNAL EID BID
22222 555
44444 555
89898 ------ 888
If I match on ID_INTERNAL I may not be able to match ID_INTERNAL 44444 and 89898 and if I match EID or BID the same situation.
Is thera any analytical function which helps me assigning a party key to all the recoords. ALl the above records should be assigned PARTY KEY 1 only.
Please help
Thanks
RajeshJustin
My main goal is to assign a party key from existing set of records to the new records which are being selected/inserted. I have to write my algoritum in such a way that the new values should match their value in existing records.
Example
my first new record has a value of 11111 under ID_INTERNAL and in the same record it has a value of 555 under EID attribute. so based on matching algoritum for ID INTERNAL it will be assigned existing party key 1.
Similarly second new record has a value of 87777 under ID INTERNAL and has a value of 555 under EID and this ID INTERNAL does not exists in the target table. but the value of 555 is available under EID attribute so I have to write algoritum based on EID.
Now the delima is my target table is as follows
Party key PARTYID PARTYNAME
1 11111 ITSID
1 123 EID
1 321 EID
Now when new records come I have to write match algortium for ID_INTERNAL to PARTYID for Partyname='ITSID'
Once matched this record ID INTERNAL=11111 and EID =555 assigned a party key=1. So after first record the output table slooks like
Party key PARTYID PARTYNAME
1 11111 ITSID
1 123 EID
1 321 EID
1 555 EID
Same way for second new record where the values are ID_INTERNAL=87777 and EID=555. I have to write match algortium based on EID because the EID value of 555 already exists in target tabel with party key.
SO after second record the target table will look like
Party key PARTYID PARTYNAME
1 11111 ITSID
1 123 EID
1 321 EID
1 555 EID
1 87777 ITSID
So this is how I have to solve this match algoritum.
Please help me if you need any information I will be glad to provide you all.
Thanks
Regards
Rajesh -
Completion of data series by analytical function
I have the pleasure of learning the benefits of analytical functions and hope to get some help
The case is as follows:
Different projects gets funds from different sources over several years, but not from each source every year.
I want to produce the cumulative sum of funds for each source for each year for each project, but so far I have not been able to do so for years without fund for a particular source.
I have used this syntax:
SUM(fund) OVER(PARTITION BY project, source ORDER BY year ROWS UNBOUNDED PRECEDING)
I have also experimented with different variations of the window clause, but without any luck.
This is the last step in a big job I have been working on for several weeks, so I would be very thankful for any help.If you want to use Analytic functions and if you are on 10.1.3.3 version of BI EE then try using Evaluate, Evaluate_aggr that support native database functions. I have blogged about it here http://oraclebizint.wordpress.com/2007/09/10/oracle-bi-ee-10133-support-for-native-database-functions-and-aggregates/. But in your case all you might want to do is have a column with the following function.
SUM(Measure BY Col1, Col2...)
I have also blogged about it here http://oraclebizint.wordpress.com/2007/10/02/oracle-bi-ee-101332-varying-aggregation-based-on-levels-analytic-functions-equivalence/.
Thanks,
Venkat
http://oraclebizint.wordpress.com -
Analytic function to retrieve a value one year ago
Hello,
I'm trying to find an analytic function to get a value on another row by looking on a date with Oracle 11gR2.
I have a table with a date_id (truncated date), a flag and a measure. For each date, I have at least one row (sometimes 2), so it is gapless.
I would like to find analytic functions to show for each date :
sum of the measure for that date
sum of the measure one week ago
sum of the measure one year ago
As it is gapless I managed to do it the week doing a group by date in a subquery and using a LAG with offset set to 7 on top of it (see below).
However I'm struggling on how to do that for the data one year ago as we might have leap years. I cannot simply set the offset to 365.
Is it possible to do it with a RANGE BETWEEN window clause? I can't manage to have it working with dates.
Week :LAG with offset 7
SQL Fiddle
or
create table daily_counts
date_id date,
internal_flag number,
measure1 number
insert into daily_counts values ('01-Jan-2013', 0, 8014);
insert into daily_counts values ('01-Jan-2013', 1, 2);
insert into daily_counts values ('02-Jan-2013', 0, 1300);
insert into daily_counts values ('02-Jan-2013', 1, 37);
insert into daily_counts values ('03-Jan-2013', 0, 19);
insert into daily_counts values ('03-Jan-2013', 1, 14);
insert into daily_counts values ('04-Jan-2013', 0, 3);
insert into daily_counts values ('05-Jan-2013', 0, 0);
insert into daily_counts values ('05-Jan-2013', 1, 1);
insert into daily_counts values ('06-Jan-2013', 0, 0);
insert into daily_counts values ('07-Jan-2013', 1, 3);
insert into daily_counts values ('08-Jan-2013', 0, 33);
insert into daily_counts values ('08-Jan-2013', 1, 9);
commit;
select
date_id,
total1,
LAG(total1, 7) OVER(ORDER BY date_id) total_one_week_ago
from
select
date_id,
SUM(measure1) total1
from daily_counts
group by date_id
order by 1;
Year : no idea?
I can't give a gapless example, would be too long but if there is a solution with the date directly :
SQL Fiddle
or add this to the schema above :
insert into daily_counts values ('07-Jan-2012', 0, 11);
insert into daily_counts values ('07-Jan-2012', 1, 1);
insert into daily_counts values ('08-Jan-2012', 1, 4);
Thank you for your help.
FloydHi,
Sorry, I;m not sure I understand the problem.
If you are certain that there is at least 1 row for every day, then you can be sure that the GROUP BY will produce exactly 1 row per day, and you can use LAG (total1, 365) just like you already use LAG (total1, 7).
Are you concerned about leap years? That is, when the day is March 1, 2016, do you want the total_one_year_ago column to reflect March 1, 2015, which was 366 days earlier? If that case, use
date_id - ADD_MONTHS (date_id, -12)
instead of 365.
LAG only works with an exact number, but you can use RANGE BETWEEN with other analytic functions, such as MIN or SUM:
SELECT DISTINCT
date_id
, SUM (measure1) OVER (PARTITION BY date_id) AS total1
, SUM (measure1) OVER ( ORDER BY date_id
RANGE BETWEEN 7 PRECEDING
AND 7 PRECEDING
) AS total1_one_week_ago
, SUM (measure1) OVER ( ORDER BY date_id
RANGE BETWEEN 365 PRECEDING
AND 365 PRECEDING
) AS total1_one_year_ago
FROM daily_counts
ORDER BY date_id
Again, use date arithmetic instead of the hard-coded 365, if that's an issue.
As Hoek said, it really helps to post the exact results you want from the given sample data. You're miles ahead of the people who don't even post the sample data, though.
You're right not to post hundreds of INSERT statements to get a year's data. Here's one way to generate sample data for lots of rows at the same time:
-- Put a 0 into the table for every day in 2012
INSERT INTO daily_counts (date_id, measure1)
SELECT DATE '2011-12-31' + LEVEL
, 0
FROM dual
CONNECT BY LEVEL <= 366
Maybe you are looking for
-
While loading the data from XML source I am getting following error ORA-00972: identifier is too long I understand that some of the Column name are more then 30 char (Oracle Limit) inmy source but I like to know what needs to be modified in the IKM t
-
Receivables Customer Statement Issue
Hi, While running customer statements the total amout due for the month doesnot considering the unapplied receipt amount for that customer. For example the customer statement due is $400 and there is a unapplied amount $100 for this customer . But st
-
Reoprt execution time out for particular Plant
Hi GURUS, I am having ECC6.0 with MSSQL2005 combination here. One of my Sales register report executes fine with local plants but for plant which is about 2000 Km from here it gets timed out when executed. So i ran that report in background. It succe
-
EU Vendor with several VAT numbers
Dear SAP experts, I face the problem when posting the vendor invoice if my vendor has several VAT numbers. I am not able to display/edit the field "Reporting country" (RF05L-EGMLD) on the document. Subsequently, when running EU Sales List report, the
-
Ni scope measurement time delay
使用niscope measurement 测量 time delay ,如何使测量更加准确,现在测出来的值的浮动范围很大,另,能不能提供其测量的具体算法编程