Analytic Functions - Need resultset only in one select
Hello Experts,
Problem Definition: Using Analytic Function, get Total sales for the Product P1 and Customer C1 [Total sales for the customer itself] in one line. I want to restrict the ResultSet of the query to Product P1, please look at the data below, queries and problems..
Data
Customer Product Qtr Sales
C1 P1 19991 100.00
C1 P1 19992 125.00
C1 P1 19993 175.00
C1 P1 19994 300.00
C1 P2 19991 100.00
C1 P2 19992 125.00
C1 P2 19993 175.00
C1 P2 19994 300.00
C2 P1 19991 100.00
C2 P1 19992 125.00
C2 P1 19993 175.00
C2 P1 19994 300.00
Problem, I want to display....
Customer Product ProdSales CustSales
C1 P1 700 1400
But Without using outer query, i.e. please look below for the query that returns this reult with two select, I want this result in one query only..
Select * From ----*** want to avoid this... ***----
(Select Customer,Product,
Sum(Sales) ProdSales,
Sum(Sum(Sales)) Over(Partition By Customer) CustSales
From t1
Where customer='C1')
Where
Product='P1' ;
Also, I want to avoid Hard coding of P1 in the select clause....
I mean, I can do it in one shot/select, but look at the query below, it uses P1 in the select clause, which is No No!! P1 is allowed only in Where or Having ..
Select Customer,Decode(Product, 'P1','P1','P1') Product,
Decode(Product,'P1',Sales,0) ProdSales,
Sum(Sum(Sales)) Over (Partition By Customer ) CustSales
From t1
Where customer='C1' ;
This will get me what I want, but as I said earlier, I want to avoid using P1 in the
Select clause..
Goal is to Avoid using
1-> Two Select/Outer Query/In Line Views
2-> Product 'P1' in the Select clause...No hard coded product name in the select clause and group by clause..
Thanks
-Dhaval
Select * From ----*** want to avoid this... ***----
(Select Customer,Product,
Sum(Sales) ProdSales,
Sum(Sum(Sales)) Over(Partition By Customer)
CustSales
From t1
Where customer='C1')
Where
Product='P1' ;
Goal is to Avoid using
1-> Two Select/Outer Query/In Line ViewsWhy?
Similar Messages
-
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 -
Analytical function need to work based on previous rows result.
My data are sorted based on sartdatetime column.but i need the sum(value) based on below condition.
Condition :
i need to check with current row with previous rows
if they are same then i need to sum the value with previous row.
else i need to start the summation.
My data like below:
Rownum Code Sartdatetime Value
1 4619 12/9/2012 11:00 219
2 5344 12/9/2012 14:39 27
3 4619 12/9/2012 15:11 20
4 4619 12/9/2012 19:33 14
5 4619 12/9/2012 20:53 16
6 6851 12/9/2012 21:21 1
7 4619 12/9/2012 21:22 35
8 5623 12/10/2012 1:59 4
9 5623 12/10/2012 2:03 3
10 5623 12/10/2012 2:06 5
11 4619 12/10/2012 2:17 1
12 5623 12/10/2012 2:18 5
13 5623 12/10/2012 2:25 2
14 5623 12/10/2012 2:27 2
15 4619 12/10/2012 2:29 30
Eg:
Take the first row. For that no previous rows. So we directly added the value like below.
Rownum Code Sartdatetime Value sum(val)
1 4619 12/9/2012 11:00 219 219
take the seconds row, we can check the code value (5344)with first row value (4619). the values are different . So our summation like.
2 5344 12/9/2012 14:39 27 27
for third row , we can check the code value (4619)with second row value (5344). the values are different . So our summation like.
3 4619 12/9/2012 15:11 20 20
for fourth row we can check the code value (4619)with second row value (4619). the values are same. So our summation like.
4 4619 12/9/2012 19:33 14 34 - The sum of previous row result.
5 th row also same value so
5 4619 12/9/2012 20:53 16 50T he sum of previous row result.
then the same value come again in 7 th row so here we need to start the summation like below.
7 4619 12/9/2012 21:22 35 35.
i want the result like below.
Rownum Code Sartdate time Value Expected result
1 4619 12/9/2012 11:00 219 219
2 5344 12/9/2012 14:39 27 27
3 4619 12/9/2012 15:11 20 20
4 4619 12/9/2012 19:33 14 34
5 4619 12/9/2012 20:53 16 50
6 6851 12/9/2012 21:21 1 1
7 4619 12/9/2012 21:22 35 35
8 5623 12/10/2012 1:59 4 4
9 5623 12/10/2012 2:03 3 7
10 5623 12/10/2012 2:06 5 12
11 4619 12/10/2012 2:17 1 1
12 5623 12/10/2012 2:18 5 5
13 5623 12/10/2012 2:25 2 7
14 5623 12/10/2012 2:27 2 9
15 4619 12/10/2012 2:29 30 30
Please helpHi,
Welcome to the forum!
Here's one way to do what you want:
WITH got_grp_id AS
SELECT code, starttime, value
, ROW_NUMBER () OVER ( ORDER BY starttime ) AS r_num
, ROW_NUMBER () OVER ( ORDER BY starttime )
- ROW_NUMBER () OVER ( PARTITION BY code
ORDER BY starttime
) AS grp_id
FROM table_x
SELECT r_num
, code
, starttime
, value
, SUM (value) OVER ( PARTITION BY code, grp_id
ORDER BY starttime
) AS sum_value
FROM got_grp_id
ORDER BY starttime
;For an explanation of the Fixed Difference technique, see {message:id=9953384} and/or {message:id=9957164}
Whenever you have a problem, please post CREATE TABLE and INSERT statements for your same data. For example:
CREATE TABLE table_x
( code NUMBER (4)
, starttime DATE UNIQUE
, value NUMBER
INSERT INTO table_x (code, starttime, value)
VALUES (4619, TO_DATE ( '12/9/2012 11:00', 'MM/DD/YYYY HH24:MI'),
219);
INSERT INTO table_x (code, starttime, value)
VALUES (5344, TO_DATE ( '12/9/2012 14:39', 'MM/DD/YYYY HH24:MI'),
27);
INSERT INTO table_x (code, starttime, value)
VALUES (4619, TO_DATE ( '12/9/2012 15:11', 'MM/DD/YYYY HH24:MI'),
20);
INSERT INTO table_x (code, starttime, value)
VALUES (4619, TO_DATE ( '12/9/2012 19:33', 'MM/DD/YYYY HH24:MI'),
14);
INSERT INTO table_x (code, starttime, value)
VALUES (4619, TO_DATE ( '12/9/2012 20:53', 'MM/DD/YYYY HH24:MI'),
16);
INSERT INTO table_x (code, starttime, value)
VALUES (6851, TO_DATE ( '12/9/2012 21:21', 'MM/DD/YYYY HH24:MI'),
1);
INSERT INTO table_x (code, starttime, value)
VALUES (4619, TO_DATE ( '12/9/2012 21:22', 'MM/DD/YYYY HH24:MI'),
35);
INSERT INTO table_x (code, starttime, value)
VALUES (5623, TO_DATE ( '12/10/2012 1:59', 'MM/DD/YYYY HH24:MI'),
4);
INSERT INTO table_x (code, starttime, value)
VALUES (5623, TO_DATE ('12/10/2012 2:03' , 'MM/DD/YYYY HH24:MI' ),
3);
INSERT INTO table_x (code, starttime, value)
VALUES (5623, TO_DATE ( '12/10/2012 2:06', 'MM/DD/YYYY HH24:MI'),
5);
INSERT INTO table_x (code, starttime, value)
VALUES (4619, TO_DATE ( '12/10/2012 2:17', 'MM/DD/YYYY HH24:MI'),
1);
INSERT INTO table_x (code, starttime, value)
VALUES (5623, TO_DATE ( '12/10/2012 2:18', 'MM/DD/YYYY HH24:MI'),
5);
INSERT INTO table_x (code, starttime, value)
VALUES (5623, TO_DATE ( '12/10/2012 2:25', 'MM/DD/YYYY HH24:MI'),
2);
INSERT INTO table_x (code, starttime, value)
VALUES (5623, TO_DATE ('12/10/2012 2:27' , 'MM/DD/YYYY HH24:MI' ),
2);
INSERT INTO table_x (code, starttime, value)
VALUES (4619, TO_DATE ( '12/10/2012 2:29', 'MM/DD/YYYY HH24:MI'),
30);
COMMIT; See the forum FAQ {message:id=9360002}
Output:
R_NUM CODE STARTTIME VALUE SUM_VALUE
1 4619 12/9/2012 11:00 219 219
2 5344 12/9/2012 14:39 27 27
3 4619 12/9/2012 15:11 20 20
4 4619 12/9/2012 19:33 14 34
5 4619 12/9/2012 20:53 16 50
6 6851 12/9/2012 21:21 1 1
7 4619 12/9/2012 21:22 35 35
8 5623 12/10/2012 01:59 4 4
9 5623 12/10/2012 02:03 3 7
10 5623 12/10/2012 02:06 5 12
11 4619 12/10/2012 02:17 1 1
12 5623 12/10/2012 02:18 5 5
13 5623 12/10/2012 02:25 2 7
14 5623 12/10/2012 02:27 2 9
15 4619 12/10/2012 02:29 30 30Edited by: Frank Kulash on Dec 24, 2012 6:44 AM
Added sample data. -
Analytic function and aggregate function
What are analytic function and aggregate function. What is difference between them?
hi,
Analytic Functions :----------
Analytic functions compute an aggregate value based on a group of rows. They differ from aggregate functions in that they return multiple rows for each group. The group of rows is called a window and is defined by the analytic_clause. For each row, a sliding window of rows is defined. The window determines the range of rows used to perform the calculations for the current row. Window sizes can be based on either a physical number of rows or a logical interval such as time.
Analytic functions are the last set of operations performed in a query except for the final ORDER BY clause. All joins and all WHERE, GROUP BY, and HAVING clauses are completed before the analytic functions are processed. Therefore, analytic functions can appear only in the select list or ORDER BY clause.
Analytic functions are commonly used to compute cumulative, moving, centered, and reporting aggregates.
Aggregate Functions :----------
Aggregate functions return a single result row based on groups of rows, rather than on single rows. Aggregate functions can appear in select lists and in ORDER BY and HAVING clauses. They are commonly used with the GROUP BY clause in a SELECT statement, where Oracle Database divides the rows of a queried table or view into groups. In a query containing a GROUP BY clause, the elements of the select list can be aggregate functions, GROUP BY expressions, constants, or expressions involving one of these. Oracle applies the aggregate functions to each group of rows and returns a single result row for each group.
If you omit the GROUP BY clause, then Oracle applies aggregate functions in the select list to all the rows in the queried table or view. You use aggregate functions in the HAVING clause to eliminate groups from the output based on the results of the aggregate functions, rather than on the values of the individual rows of the queried table or view.
let me know if you are feeling any problem in understanding.
thanks.
Edited by: varun4dba on Jan 27, 2011 3:32 PM -
Analytic function - two answers in one single select
create table atest(eid varchar2(10),ukey varchar2(10),tname varchar2(40));
insert into atest values ('eid1','ukey1','tname1');
insert into atest values ('eid2','ukey1','tname2');
insert into atest values ('eid3','ukey2','tname3');
insert into atest values ('eid4','ukey2','tname3');
insert into atest values ('eid5','ukey3','tname4');
commit;I need one single sql, from which I want to find out 2 things
1. find out those ukey values for which we have multiple eids (ukey1, ukey2 in our example)
2. next, once we identify those ukeys with multiple eids, out of those, find those ukeys which have non unique tnames (in our example ukey1)
I would like to do it using analytic functions, so far tried the following
select eid, ukey,
(case when count(distinct a.eid) over (partition by (a.ukey)) > 1 then
-- do something here, above will give ukey1, ukey2, but how to identify those ukeys with non unique tnames ?
else
end) tsource
from atest a;user650888 wrote:
can the same be achieved with analytics ? Yes, but why? Anyway:
select ukey
from (
select ukey,
count(distinct eid) over(partition by ukey) eid_cnt,
count(distinct tname) over(partition by ukey) tname_cnt,
row_number() over(partition by ukey order by 1) rn
from atest
where eid_cnt > 1
and tname_cnt > 1
and rn = 1
UKEY
ukey1
SQL> SY. -
Hi,
I have a problem using analytic function: when I execute this query
SELECT TSIUPSITE, TSIUPCEAN , TSIUPDATE, sum(TSIUPCA) TSIUPCA, TSIUPCTVA,TSIUPP4N,TSIUPPIEC,
sum(TSIUPQTE) TSIUPQTE,sum(TSIUPQTEP) TSIUPQTEP, TSIUPMDIU,TSIUPMDar,
sum(TSIUPCRIU) TSIUPCRIU,sum(TSIUPCRAR) TSIUPCRAR, trunc(TSIUPDCRE) TSIUPDCRE ,trunc(TSIUPDMAJ) TSIUPDMAJ ,
TSIUPUTIL,TSIUPTRT,TSIUPNERR,TSIUPMESS,
TSIUPTMVT,TSIUPSMAN, TSIUPMOTIF, sum(TSIUPMHT) TSIUPMHT, 0 vtanfisc,
TSIUPDATEVERIF,TSIUPNSEQ,TSIUPCINV ,count(*) over (partition by TSIUPSITE,TSIUPCEAN,TSIUP_TRT ) CONTA_ARTICOLO
FROM TST_FLIISR_VTEREMART
WHERE 1=1 --TSIUP_TRT = 1
AND TSIUPDATE=to_date('27082012','ddmmyyyy')
and TSIUP_NTRX =172
AND TSIUPSITE = 10025
AND TSIUPCEAN = '8012452018825'
GROUP BY TSIUPSITE, TSIUPCEAN , TSIUPDATE, TSIUPCTVA,TSIUPP4N,TSIUPPIEC,
TSIUPMDIU,TSIUPMDar, trunc(TSIUPDCRE),trunc(TSIUPDMAJ),TSIUPUTIL,TSIUPTRT,TSIUPNERR,TSIUPMESS,
TSIUPTMVT,TSIUPSMAN, TSIUPMOTIF, 0,
TSIUPDATEVERIF,TSIUPNSEQ,TSIUPCINV
ORDER BY TSIUPSITE,TSIUPDATE ;
I have the error ORA-00979: not a GROUP BY expression related to TSIUP_TRT field,infact, if I execute this one
SELECT TSIUPSITE, TSIUPCEAN , TSIUPDATE, sum(TSIUPCA) TSIUPCA, TSIUPCTVA,TSIUPP4N,TSIUPPIEC,
sum(TSIUPQTE) TSIUPQTE,sum(TSIUPQTEP) TSIUPQTEP, TSIUPMDIU,TSIUPMDar,
sum(TSIUPCRIU) TSIUPCRIU,sum(TSIUPCRAR) TSIUPCRAR, trunc(TSIUPDCRE) TSIUPDCRE ,trunc(TSIUPDMAJ) TSIUPDMAJ ,
TSIUPUTIL,TSIUPTRT,TSIUPNERR,TSIUPMESS,
TSIUPTMVT,TSIUPSMAN, TSIUPMOTIF, sum(TSIUPMHT) TSIUPMHT, 0 vtanfisc,
TSIUPDATEVERIF,TSIUPNSEQ,TSIUPCINV ,count(*) over (partition by TSIUPSITE,TSIUPCEAN ) CONTA_ARTICOLO
FROM TST_FLIISR_VTEREMART
WHERE 1=1 --TSIUP_TRT = 1
AND TSIUPDATE=to_date('27082012','ddmmyyyy')
and TSIUP_NTRX =172
AND TSIUPSITE = 10025
AND TSIUPCEAN = '8012452018825'
GROUP BY TSIUPSITE, TSIUPCEAN , TSIUPDATE, TSIUPCTVA,TSIUPP4N,TSIUPPIEC,
TSIUPMDIU,TSIUPMDar, trunc(TSIUPDCRE),trunc(TSIUPDMAJ),TSIUPUTIL,TSIUPTRT,TSIUPNERR,TSIUPMESS,
TSIUPTMVT,TSIUPSMAN, TSIUPMOTIF, 0,
TSIUPDATEVERIF,TSIUPNSEQ,TSIUPCINV
ORDER BY TSIUPSITE,TSIUPDATE ;
I have no problem. Now the difference between TSIUPCEAN ( or TSIUPSITE) and TSIUP_TRT is that TSIUP_TRT is not in Group By clause, but, to be honest, I don't know why I have this problem using using an analitic function.
Thanks for helpHi,
I think you are not using analytic function properly.
Analytical functions will execute for each row. Where as Group BY will execute for groups of data.
See below example for you reference.
Example 1:
-- Below query displays number of employees for each department. Since we have used analytical function for each row you are getting the number of employees based on the department id.
SQL> SELECT e.department_id,count(*) OVER (PARTITION BY e.department_id) cnt_analytic
2 FROM employees e
3 WHERE e.department_id IN (10,20,30);
DEPARTMENT_ID CNT_ANALYTIC
10 1
20 2
20 2
30 6
30 6
30 6
30 6
30 6
30 6
9 rows selected.
Example 2:
-- Since I have used GROUP BY clause I'm getting only single row for each department.
SQL> SELECT e.department_id, count(*) cnt_group
2 FROM employees e
3 WHERE e.department_id IN (10,20,30)
4 GROUP BY e.department_id;
DEPARTMENT_ID CNT_GROUP
10 1
20 2
30 6Finally, what I'm trying to explain you is - If you use Analytical function with GROUP BY clause, the query will not give the menaing ful result set.
See below
SQL> SELECT e.department_id,count(*) OVER (PARTITION BY e.department_id) cnt_analytic, count(*) cnt_grp
2 FROM employees e
3 WHERE e.department_id IN (10,20,30)
4 GROUP BY e.department_id;
DEPARTMENT_ID CNT_ANALYTIC CNT_GRP
10 1 1
20 1 2
30 1 6 -
How to import only procedures, functions, and packages only
Hi everybody...
How to import only procedures, functions, and packages only from one user to another user.
Thanks in advance
palThanks for your reply...i did the following..can you please help from this how to proceed
---------------- getcode.sql -------------------------------
set feedback off
set heading off
set termout off
set linesize 1000
set trimspool on
set verify off
spool &1..sql
prompt set define off
select decode( type||'-'||to_char(line,'fm99999'),
'PACKAGE BODY-1', '/'||chr(10),
null) ||
decode(line,1,'create or replace ', '' ) ||
text text
from user_source
where name = upper('&&1')
order by type, line;
prompt /
prompt set define on
spool off
set feedback on
set heading on
set termout on
set linesize 100
------------------- getallcode.sql -------------------------
set termout off
set heading off
set feedback off
set linesize 50
spool c:\xtmpx.sql
select '@getcode ' || object_name
from user_objects
where object_type in ( 'PROCEDURE', 'FUNCTION', 'PACKAGE' )
spool off
spool c:\getallcode_INSTALL
select '@' || object_name
from user_objects
where object_type in ( 'PROCEDURE', 'FUNCTION', 'PACKAGE' )
spool off
set heading on
set feedback on
set linesize 130
set termout on
@xtmpx.sql
I did this one user and created the xtmpx.sql
and getallcode_INSTALL.LST.
From this, how to proceed to run these in another user.
Thanks,
Pal -
To use "analytic function" at "recursive with clause"
http://docs.oracle.com/cd/E11882_01/server.112/e26088/statements_10002.htm#i2077142
The recursive member cannot contain any of the following elements:
・An aggregate function. However, analytic functions are permitted in the select list.
OK I will use analytic function at The recursive member :-)
SQL> select * from v$version;
BANNER
Oracle Database 11g Release 11.2.0.1.0 - Production
PL/SQL Release 11.2.0.1.0 - Production
CORE 11.2.0.1.0 Production
TNS for 32-bit Windows: Version 11.2.0.1.0 - Production
NLSRTL Version 11.2.0.1.0 - Production
SQL> with rec(Val,TotalRecCnt) as(
2 select 1,1 from dual
3 union all
4 select Val+1,count(*) over()
5 from rec
6 where Val+1 <= 5)
7 select * from rec;
select * from rec
ERROR at line 7:
ORA-32486: unsupported operation in recursive branch of recursive WITH clauseWhy ORA-32486 happen ?:|Hi Aketi,
It works in 11.2.0.2, so it is probably a bug:
select * from v$version
BANNER
Oracle Database 11g Enterprise Edition Release 11.2.0.2.0 - 64bit Production
PL/SQL Release 11.2.0.2.0 - Production
CORE 11.2.0.2.0 Production
TNS for IBM/AIX RISC System/6000: Version 11.2.0.2.0 - Production
NLSRTL Version 11.2.0.2.0 - Production
with rec(Val,TotalRecCnt) as(
select 1,1 from dual
union all
select Val+1,count(*) over()
from rec
where Val+1 <= 5)
select * from rec
VAL TOTALRECCNT
1 1
2 1
3 1
4 1
5 1 Regards,
Bob -
Analytic function for grouping?
Hello @all
10gR2
Is it possible to use an analytic function for grouping following (example-)query:
SELECT job, ename, sal,
ROW_NUMBER() OVER(PARTITION BY job ORDER BY empno) AS no,
RANK() OVER(PARTITION BY job ORDER BY NULL) AS JobNo
FROM emp;The output is following:
JOB ENAME SAL NO JOBNO
ANALYST SCOTT 3000 1 1
ANALYST FORD 3000 2 1
CLERK SMITH 818 1 1
CLERK ADAMS 1100 2 1
CLERK JAMES 950 3 1
CLERK MILLER 1300 4 1
MANAGER Müller 1000 1 1
MANAGER JONES 2975 2 1
....The JobNo should increase group by job and ename; my desired output should be looking like...:
JOB ENAME SAL NO JOBNO
ANALYST SCOTT 3000 1 1
ANALYST FORD 3000 2 1
CLERK SMITH 818 1 2
CLERK ADAMS 1100 2 2
CLERK JAMES 950 3 2
CLERK MILLER 1300 4 2
MANAGER Müller 1000 1 3
MANAGER JONES 2975 2 3
MANAGER BLAKE 2850 3 3
MANAGER CLARK 2450 4 3
PRESIDENT KING 5000 1 4
SALESMAN ALLEN 1600 1 5
SALESMAN WARD 1250 2 5
SALESMAN MARTIN 1250 3 5
SALESMAN TURNER 1500 4 5How can I achieve this?This, perhaps?
with emp as (select 1 empno, 'ANALYST' job, 'SCOTT' ename, 3000 sal from dual union all
select 2 empno, 'ANALYST' job, 'FORD' ename, 3000 sal from dual union all
select 3 empno, 'CLERK' job, 'SMITH' ename, 818 sal from dual union all
select 4 empno, 'CLERK' job, 'ADAMS' ename, 1100 sal from dual union all
select 5 empno, 'CLERK' job, 'JAMES' ename, 950 sal from dual union all
select 6 empno, 'CLERK' job, 'MILLER' ename, 1300 sal from dual union all
select 7 empno, 'MANAGER' job, 'Müller' ename, 1000 sal from dual union all
select 8 empno, 'MANAGER' job, 'JONES' ename, 2975 sal from dual union all
select 9 empno, 'MANAGER' job, 'BLAKE' ename, 2850 sal from dual union all
select 10 empno, 'MANAGER' job, 'CLARK' ename, 2450 sal from dual union all
select 11 empno, 'PRESIDENT' job, 'KING' ename, 5000 sal from dual union all
select 12 empno, 'SALESMAN' job, 'ALLEN' ename, 1600 sal from dual union all
select 13 empno, 'SALESMAN' job, 'WARD' ename, 1250 sal from dual union all
select 14 empno, 'SALESMAN' job, 'MARTIN' ename, 1250 sal from dual union all
select 15 empno, 'SALESMAN' job, 'TURNER' ename, 1500 sal from dual)
select job, ename, sal,
row_number() over(partition by job order by empno) no,
dense_rank() over(order by job) jobno
from emp
JOB ENAME SAL NO JOBNO
ANALYST SCOTT 3000 1 1
ANALYST FORD 3000 2 1
CLERK SMITH 818 1 2
CLERK ADAMS 1100 2 2
CLERK JAMES 950 3 2
CLERK MILLER 1300 4 2
MANAGER Müller 1000 1 3
MANAGER JONES 2975 2 3
MANAGER BLAKE 2850 3 3
MANAGER CLARK 2450 4 3
PRESIDENT KING 5000 1 4
SALESMAN ALLEN 1600 1 5
SALESMAN WARD 1250 2 5
SALESMAN MARTIN 1250 3 5
SALESMAN TURNER 1500 4 5 -
Restrict Query Resultset which uses Analytic Function
Gents,
Problem Definition: Using Analytic Function, get Total sales for the Product P1
and Customer C1 [Total sales for the customer itself] in one line.
I want to restrict the ResultSet of the query to Product P1,
please look at the data below, queries and problems..
Data
Customer Product Qtr Sales
C1 P1 19991 100.00
C1 P1 19992 125.00
C1 P1 19993 175.00
C1 P1 19994 300.00
C1 P2 19991 100.00
C1 P2 19992 125.00
C1 P2 19993 175.00
C1 P2 19994 300.00
C2 P1 19991 100.00
C2 P1 19992 125.00
C2 P1 19993 175.00
C2 P1 19994 300.00
Problem, I want to display....
Customer Product ProdSales CustSales
C1 P1 700 1400
But Without using outer query, i.e. please look below for the query that
returns this reult with two select, I want this result in one query only..
Select * From ----*** want to avoid this... ***----
(Select Customer,Product,
Sum(Sales) ProdSales,
Sum(Sum(Sales)) Over(Partition By Customer) CustSales
From t1
Where customer='C1')
Where
Product='P1' ;
Also, I want to avoid Hard coding of P1 in the select clause....
I mean, I can do it in one shot/select, but look at the query below, it uses
P1 in the select clause, which is No No!! P1 is allowed only in Where or Having ..
Select Customer,Decode(Product, 'P1','P1','P1') Product,
Decode(Product,'P1',Sales,0) ProdSales,
Sum(Sum(Sales)) Over (Partition By Customer ) CustSales
From t1
Where customer='C1' ;
This will get me what I want, but as I said earlier, I want to avoid using P1 in the
Select clause..
Goal is to Avoid using
1-> Two Select/Outer Query/In Line Views
2-> Product 'P1' in the Select clause...
Thanks
-Dhaval RasaniaI don't understand goal number 1 of not using an inline view.
What is the harm? -
Need analytic function suggestion
Hi,
I need advice related to analytic ( I think ) function in Oracle 9.
create table testx ( id number, arr number, fore number, actual number, result_x number, is_first number);
insert into testx values ( 1, null, null, 12, null , 0 );
insert into testx values ( 2, null, null, 14 , null, 0 );
insert into testx values ( 3, 4, 5, 16, 16, 1 );
insert into testx values ( 4, 5, 5, 18, 16, 0 );
insert into testx values ( 5, 5, 5, 20, 16, 0 );
insert into testx values ( 6, 5, 5, 22, 16, 0 );
insert into testx values ( 7, 5, 5, 24, 16, 0 );
insert into testx values ( 8, 5, 5, 25, 16, 0 );
insert into testx values ( 9, 5, 8, 25, 13, 0 );
insert into testx values ( 10, 5, 8, 21, 10, 0 );
insert into testx values ( 11, 5, 8, 19, 7, 0 );
insert into testx values ( 12, 5, 8, 18, 4, 0 );
I need ONE level query ( no subqueries ) which will calculate value stored in RESULT_X column.
Rule for calculation is:
1. when arr and fore columns are available first time then result_x = actual ( row with id = 3)
2. in other case result_x = (previous value of result_x + arr - fore )
3. order of records is stored in id column
I have problem with calculating previous value of result_x since it should be available in next row calculation and dependents on other columns values.
Thanks for help,
Regards,
PiotrHi, Piotr,
This produces the results you requested:
SELECT testx.*
, SUM ( CASE
WHEN is_first = 1
THEN result_x
ELSE arr - fore
END
) OVER (ORDER BY id) AS computed_result_x
FROM testx
ORDER BY id
;This relies on the fact that there is only one row where is_first=1, and that all the earlier rows have NULL as arr or fore.
If that's not the case in your real data, then I don't think it's possible in SQL without sub-queries. Why can't you use a sub-query?
The problem is that rows up to the one with is_first=1 have to be treated differently from rows after that point, so ithe CASE expression might need to know if a given row is before or after the one with is_first=1. If you need an analytic function to determine that, then you need a sub-query, becuase analytic functions can not be nested.
You could use MODEL or a recursive WITH clause to get the results you want, but they require sub-querries. -
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 -
I need to be able to select multiple emails and foward them in one email
I have to foward emails; however, only current way is one at a time. I need to be able to select <n> emails, and then forward them to one or more people- but they want only one email with the other emails appearing in email as attached documents that they open by selecting one of the emails sent to them- need arisen as need to be reimbursed for business trip and they do not want multiple emails- only one email. I only havevan iPad and could do this crummy Outlook but can figure out how to do on iPad...
Well, just remember, this is only the second generation iPad and tablets are not really a true replacement for a fully functioning computer. As they evolve over time, more and more functionality will be built in.........
-
Help needed with analytical function
I want to get the employee details of the highest and 2nd highest salaried employee in a particular department. But also the department should have more than 1 employee.
I tried the query and it gave me proper results. But I'm wondering if there is some other alternative than using the subquery.
Here is the table and the result query :
with t as
select 1 emp_id,3 mgr_id,'Rajesh' emp_name,3999 salary,677 bonus,'HR' dpt_nme from dual union
select 2 ,3 ,'Gangz',4500,800,'Finance' from dual union
select 3 ,4 ,'Sid',8000,12000,'IT' from dual union
select 4 ,null,'Ram',5000,677,'HR' from dual union
select 5 ,4,'Shyam',6000,677,'IT' from dual union
select 6 ,4 ,'Ravi',9000,12000,'IT' from dual
select * from
(select emp_id, mgr_id, emp_name, dpt_nme, salary, row_number() over (partition by dpt_nme order by salary desc) rn from t where dpt_nme in
(select dpt_nme from t group by dpt_nme having count(*) > 1)) where rn < 3Hi,
You need a sub-query, but you don't need more than that.
Here's one way to eliminate the extra sub-query:
WITH got_analytics AS
SELECT emp_id, mgr_id, emp_name, dpt_nme, salary
, ROW_NUMBER () OVER ( PARTITION BY dpt_nme
ORDER BY salary DESC
) AS rn
, COUNT (*) OVER ( PARTITION BY dpt_nme
) AS dpt_cnt
FROM t
SELECT emp_id, mgr_id, emp_name, dpt_nme, salary
, rn
FROM got_analytics
WHERE rn < 3
AND dpt_cnt > 1
;Analytic functions are computed after the WHERE clause is applied. Since we need to use the results of the analytic ROW_NUMBER function in a WHERE clause, that means we'll have to compute ROW_NUMBER in a sub-query, and use the results in the WHERE clause of the main query. We can call the analytic COUNT function in the same sub-query, and use its results in the same WHERE clause of the main query.
What results would you want if there's a tie for the 2nd highest salary in some department? For example, if you add this row to your sample data:
select 7 ,3 ,'Sunil',8000,12000,'IT' from dual union? You may want to use RANK instead of ROW_NUMBER. -
Select value of most occurances - analytical function?!
Hi ...
I've get stuck with a "little" problem.
I try to provide some testing code for this:
CREATE TABLE a1 (
id NUMBER(8),
val NUMBER(6),
title VARCHAR2(16),
CONSTRAINT test_pk PRIMARY KEY(id)
INERT INTO a1 (id, val, title) VALUES (1,12,'c');
INERT INTO a1 (id, val, title) VALUES (2,13,'b');
INERT INTO a1 (id, val, title) VALUES (3,13,'a');
INERT INTO a1 (id, val, title) VALUES (4,13,'a');
INERT INTO a1 (id, val, title) VALUES (5,42,'a');
INERT INTO a1 (id, val, title) VALUES (6,42,'b');
INERT INTO a1 (id, val, title) VALUES (7,42,'b');Actually the table is much bigger ;) But this should be ok for this question. It already exist a query like:
SELECT
count(*) -- just an example
FROM
a1
GROUP BY
val
-- should return 1,3,3 (for the groups val=12, val=13,val=42)Now it is nessecary to select a title for each group (specified by group by). And there the title which occurs the most in this group should be selected. For this example this are 'c' for the group val=12 .. 'a' for the group val=13 and finally 'b' for the group val=42.
I tried to use some anayltical function, but I#m not able to get this to work - may be because I never used analytical functions before. If I try something I mostly get an error: Keyword FROM not at expected position (ORA-00923). I searched for some tutorial/howto documentations where my problem is handled but without success. So I guess the syntax and the way to understand analytical functions is not as easy as it semms to be ...
title OVER ( ORDER BY count(*) ROWS | RANGE BETWEEN 1 AND 1 ) <-- that would by logical for my brain, but not for oracles ;-)
Can somebody help?
Thanks!Hi folks,
thanks for the variuos answers! Weekend is over, so we should work on ...
I tried some examples you gave me. And I decide to provide more detailes information! At first, if the amount of 'a' and 'b' is equal it doesn't matter which one will be returned (so it can be undefined, if that makes thing easier). I will now paste the original query I work with and I add some comments for you - so you can find the lines which should be changed to return the most occurances-value.
If you think it makes sense to Provide some create-table ddl an (maybe as csv file) some data, tell me how i can do that (I think its not an option to post ~ Mio rows as Inserts here).
The select-query I want to manipulate - this is no more related to our test-table 'a1' !! For Example let us look on the rows where 'A' and 'drm_' is selected - starts at line 7!
SELECT
box_id,
schedule_id,
fixsecs_down(MIN(acqtime),600),
COUNT(*), -- each row in rfm_meas_hr represents one frame of measuremnt-data, so this represents the number of frames received in this block
-- instead of 'A' the most occurance of the col 'rpro' should be selected here
'A',
-- like above, but from the column 'rdmo'
'drm_',
-- below this some other cols are calculated/selected, not important here
FLOOR(MEDIAN(rfre)),
ROUND(AVG(rdbv),2),
ROUND(SUM(POWER(rdbv,2)),2),
ROUND(MAX(rdbv),2),
ROUND(MIN(rdbv),2),
ROUND(SUM(rsnr)/SUM(nframes),2),
ROUND(SUM(POWER(rsnr,2)),2),
ROUND(MAX(rsnr),2),
ROUND(MIN(rsnr),2),
ROUND( AVG(rsta_sync),2), -- rsta_sync
ROUND(SUM(POWER(rsta_sync,2)),2), -- rsta_sync_s
ROUND( MIN(rsta_sync),2), -- rsta_sync_min
ROUND( MAX(rsta_sync),2), -- rsta_sync_max
ROUND( AVG(rsta_fac),2), -- rsta_facc
ROUND(SUM(POWER(rsta_fac,2)),2), -- rsta_fac_s
ROUND( MIN(rsta_fac),2), -- rsta_fac_min
ROUND( MAX(rsta_fac),2), -- rsta_fac_max
ROUND( AVG(rsta_sdc),2), -- rsta_sdc
ROUND(SUM(POWER(rsta_sdc,2)),2), -- rsta_sdc_s
ROUND( MIN(rsta_sdc),2), -- rsta_sdc_min
ROUND( MAX(rsta_sdc),2), -- rsta_sdc_max
ROUND( AVG(rsta_audio),2), -- rsta_audio
ROUND(SUM(POWER(rsta_audio,2)),2), -- rsta_audio_s
ROUND( MIN(rsta_audio),2), -- rsta_audio_min
ROUND( MAX(rsta_audio),2), -- rsta_audio_max
MIN(rser), TODO: most occurances
MIN(rtty_stream0), -- TODO: most occurances
MIN(rtty_stream1), -- TODO: most occurances
MIN(rtty_stream2), -- TODO: most occurances
MIN(rtty_stream3), -- TODO: most occurances
ROUND(AVG(NVL(rafs_error/nullif(rafs_au,0),1))*SUM(rafs_au)/NULLIF(SUM(rafs_au),0),2), -- rafs
ROUND( SUM( POWER( NVL(rafs_error/nullif(rafs_au,0),1),2))*SUM(rafs_au)/NULLIF(SUM(rafs_au),0) ,2), -- rafs_s
ROUND(MIN(rafs_error/ NULLIF(rafs_au,0)),2), -- rafs_min
ROUND(MAX(NVL(rafs_error/NULLIF(rafs_au,0),1) )*SUM(rafs_au)/NULLIF(SUM(rafs_au),0),2), -- rafs_max
SUM(robm_A),
SUM(robm_B),
SUM(robm_C),
SUM(robm_D),
SUM(robm_E),
ROUND(SUM(rwmf) / SUM(nframes),2), -- rwmf
ROUND(SUM(POWER(rwmf,2)),2), -- rwmf_s
ROUND(MIN(rwmf),2), -- rwmf_min
ROUND(MAX(rwmf),2), -- rwmf_max
ROUND(SUM(rwmm) / SUM(nframes),2), -- rwmm
ROUND(SUM(POWER(rwmm,2)),2), -- rwmm_s
ROUND(MIN(rwmm),2), -- rwmm_min
ROUND(MAX(rwmm),2), -- rwmm_max
ROUND(SUM(rmer) / SUM(nframes),2), -- rmer
ROUND(SUM(POWER(rmer,2)),2), -- rmer_s
ROUND(MIN(rmer),2), -- rmer_min
ROUND(MAX(rmer),2), -- rmer_max
ROUND(SUM(RBP0_ERRS+ RBP1_ERRS+ RBP2_ERRS+ RBP3_ERRS) / NULLIF(SUM(RBP0_BITS+ RBP1_BITS+ RBP2_BITS+ RBP3_BITS),0) ,10), -- ber
ROUND(SUM(POWER( (RBP0_ERRS+ RBP1_ERRS+ RBP2_ERRS+ RBP3_ERRS) / NULLIF((RBP0_BITS+ RBP1_BITS+ RBP2_BITS+ RBP3_BITS),0) ,2)),10), -- ber_s
ROUND(MIN(RBP0_ERRS+ RBP1_ERRS+ RBP2_ERRS+ RBP3_ERRS) / NULLIF(MIN(RBP0_BITS+ RBP1_BITS+ RBP2_BITS+ RBP3_BITS),0) ,10), -- ber_min
ROUND(MAX(RBP0_ERRS+ RBP1_ERRS+ RBP2_ERRS+ RBP3_ERRS) / NULLIF(MAX(RBP0_BITS+ RBP1_BITS+ RBP2_BITS+ RBP3_BITS),0) ,10), -- ber_max
ROUND(AVG(rdop),2), -- rdop
ROUND(SUM(POWER(rdop,2) ),2), -- rdop_s
ROUND(MIN(rdop),2), -- rdop_min
ROUND(MAX(rdop),2), -- rdop_max
ROUND(AVG(rdel90),2), -- rdel90
ROUND(SUM(POWER(rdel90,2) ),2), -- rdel90_s
ROUND(MIN(rdel90),2), -- rdel90_min
ROUND(MAX(rdel90),2), -- rdel90_max
ROUND(AVG(rdel95),2), -- rdel95
ROUND(AVG(rdel99),2), -- rdel99
null AS reslevel
FROM
-- select the data where this should be calculated
SELECT
FROM
rfm_meas_hr
WHERE
acqtime < fixsecs_down(to_timestamp('07-01-2011 14:00:00,00','dd-mm-yyyy hh24:mi:ss,ff'),600)
AND (reslevel IS NULL OR reslevel=10)
-- group the selected data and executes the calculation given by SELECT-statement
GROUP BY
-- group the data into 10min packages, indicated by its timestamp
to_char( EXTRACT(MONTH FROM acqtime)*100000 + EXTRACT(DAY FROM acqtime)*1000 + EXTRACT(HOUR FROM acqtime)*10 + floor(EXTRACT(MINUTE FROM acqtime)/10) ),
schedule_id,
box_id
HAVING
SUM(nframes)>15
;I chould say: I can add indexes if nessecary! At the moment there is one on (acqtime, reslevel) as this improves the access speed. But the query above, executed on typical data already takes 5-7 sec.
Please let me know if you need any more information.
Regards!
Maybe you are looking for
-
X-Fi XtremeMusic + Creative Headphones HQ-2300D. Digital Outpu
Have just gotten the 5. HQ-2300D headphone, and am unable to get them working in Dolby Digital or DTS mode with my SB X-Fi XtremeMusic. What is the correct way to connect these headphones to my SB so i can get digital sound form the headphones? Am i
-
o.k. I need some expert help. This is too simple of a problem. My iweb appears where it should on the web, at: http://web.mac.com[yourmembername] However when I open up my iweb, the site that is listed was just a test site. I went into my idisk and t
-
Do I need to unload the GroupWise agents if I am installing WebAccess on its own server?
Hi, I ma installing GW Web Access into our GW703 tree and the install is prompting me to unload all agents on the machine to which I am installing. As I am installing onto a separate server, I am assuming I can leave my PO, GWIA and MTA running? Can
-
What is Profile realm in multi site
Hi All, What is a realm mean in ATG??? What is the role of Profile realm in Multi site?? I have gone through the ATG docs, but i was not able to understand the exact meaning. A profile can be associated to a site using profile realm. what does it
-
How to uninstall Lion Server?
I accidentally downloaded Lion Server on my laptop. I need to uninstall this. Anyone know how to uninstall?