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
);
Similar Messages
-
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 -
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. -
TABLE T1(R1_ID,R2,R3)
insert into t1 values(63,800,'1/1/2005')
insert into t1 values(64,841,'1/1/2005')
insert into t1 values(64,862,'1/1/2006')
insert into t1 values(64,879,'4/1/2007')
insert into t1 values(64,952,'4/1/2008')
insert into t1 values(64,980,'2/1/2009')
insert into t1 values(64,1010,'2/1/2010')
insert into t1 values(64,1041,'2/1/2011')
insert into t1 values(66,841,'1/1/2005')
insert into t1 values(66,862,'1/1/2006')
insert into t1 values(66,879,'4/1/2007')
insert into t1 values(66,952,'4/1/2008')
insert into t1 values(66,980,'2/1/2009')
insert into t1 values(66,1010,'2/1/2010')
insert into t1 values(66,1042,'2/1/2011')
insert into t1 values(67,841,'1/1/2005')
insert into t1 values(67,862,'1/1/2006')
insert into t1 values(67,879,'4/1/2007')
insert into t1 values(67,952,'4/1/2008')
insert into t1 values(67,980,'2/1/2009')
insert into t1 values(67,1009,'2/1/2010')
insert into t1 values(67,1035,'2/1/2011')
insert into t1 values(112,3660,'1/1/2005')
insert into t1 values(112,3806,'1/1/2006')
insert into t1 values(112,4500,'8/1/2006')
insert into t1 values(112,7280,'3/1/2007')
insert into t1 values(112,8600,'2/1/2008')
insert into t1 values(112,8818,'5/1/2008')
insert into t1 values(112,9170,'2/1/2009')
insert into t1 values(112,9489,'2/1/2010')
insert into t1 values(112,9778,'2/1/2011')
insert into t1 values(537,7000,'11/27/2005')
insert into t1 values(537,7000,'12/1/2005')
SELECT distinct R1_ID,MAX(R2) OVER(PARTITION BY R1_ID),MAX(R3) OVER(PARTITION BY R1_ID)
FROM T1
order by R1_ID
I WANT MAX OF R2 & R3 IN ONE SQL STATEMENT. IS THAT POSSIBLE?
ALTHOUGH I CAN WRITE A SUBQUERY WITH R3 TO GET THE RESULT.with temp as(
SELECT R1_ID,R2,R3,
row_number() over(partition by R1_ID
order by R2 desc,R3 desc) rn
FROM T1)
select * from tmp where rn=1 order by R1_ID -
Moving sum using date intervals - analytic functions help
let's say you have the following set of data:
DATE SALES
09/02/2012 100
09/02/2012 50
09/02/2012 10
09/02/2012 1000
09/02/2012 20
12/02/2012 1000
12/02/2012 1100
14/02/2012 1000
14/02/2012 100
15/02/2012 112500
15/02/2012 13500
15/02/2012 45000
15/02/2012 1500
19/02/2012 1500
20/02/2012 400
23/02/2012 2000
27/02/2012 4320
27/02/2012 300000
01/03/2012 100
04/03/2012 17280
06/03/2012 100
06/03/2012 100
06/03/2012 4320
08/03/2012 100
13/03/2012 1000
for each day i need to know the sum of the sales in the present and preceding 5 days (calendar) [not five rows].
What qurey could i use???
Please help!Hi.
Here's one way.
WITH data AS
SELECT TO_DATE('09/02/2012','DD/MM/YYYY') d, 100 n FROM DUAL UNION ALL
SELECT TO_DATE('09/02/2012','DD/MM/YYYY') d, 50 n FROM DUAL UNION ALL
SELECT TO_DATE('09/02/2012','DD/MM/YYYY') d, 10 n FROM DUAL UNION ALL
SELECT TO_DATE('09/02/2012','DD/MM/YYYY') d, 1000 n FROM DUAL UNION ALL
SELECT TO_DATE('09/02/2012','DD/MM/YYYY') d, 20 n FROM DUAL UNION ALL
SELECT TO_DATE('12/02/2012','DD/MM/YYYY') d, 1000 n FROM DUAL UNION ALL
SELECT TO_DATE('12/02/2012','DD/MM/YYYY') d, 1100 n FROM DUAL UNION ALL
SELECT TO_DATE('14/02/2012','DD/MM/YYYY') d, 1000 n FROM DUAL UNION ALL
SELECT TO_DATE('14/02/2012','DD/MM/YYYY') d, 100 n FROM DUAL UNION ALL
SELECT TO_DATE('15/02/2012','DD/MM/YYYY') d, 112500 n FROM DUAL UNION ALL
SELECT TO_DATE('15/02/2012','DD/MM/YYYY') d, 13500 n FROM DUAL UNION ALL
SELECT TO_DATE('15/02/2012','DD/MM/YYYY') d, 45000 n FROM DUAL UNION ALL
SELECT TO_DATE('15/02/2012','DD/MM/YYYY') d, 1500 n FROM DUAL UNION ALL
SELECT TO_DATE('19/02/2012','DD/MM/YYYY') d, 1500 n FROM DUAL UNION ALL
SELECT TO_DATE('20/02/2012','DD/MM/YYYY') d, 400 n FROM DUAL UNION ALL
SELECT TO_DATE('23/02/2012','DD/MM/YYYY') d, 2000 n FROM DUAL UNION ALL
SELECT TO_DATE('27/02/2012','DD/MM/YYYY') d, 4320 n FROM DUAL UNION ALL
SELECT TO_DATE('27/02/2012','DD/MM/YYYY') d, 300000 n FROM DUAL UNION ALL
SELECT TO_DATE('01/03/2012','DD/MM/YYYY') d, 100 n FROM DUAL UNION ALL
SELECT TO_DATE('04/03/2012','DD/MM/YYYY') d, 17280 n FROM DUAL UNION ALL
SELECT TO_DATE('06/03/2012','DD/MM/YYYY') d, 100 n FROM DUAL UNION ALL
SELECT TO_DATE('06/03/2012','DD/MM/YYYY') d, 100 n FROM DUAL UNION ALL
SELECT TO_DATE('06/03/2012','DD/MM/YYYY') d, 4320 n FROM DUAL UNION ALL
SELECT TO_DATE('08/03/2012','DD/MM/YYYY') d, 100 n FROM DUAL UNION ALL
SELECT TO_DATE('13/03/2012','DD/MM/YYYY') d, 1000 n FROM DUAL
days AS
SELECT TO_DATE('2012-02-01','YYYY-MM-DD')+(LEVEL-1) d
FROM DUAL
CONNECT BY LEVEL <= 60
totals_per_day AS
SELECT dy.d,SUM(NVL(dt.n,0)) total_day
FROM
data dt,
days dy
WHERE
dy.d = dt.d(+)
GROUP BY dy.d
ORDER BY 1
SELECT
d,
SUM(total_day) OVER
ORDER BY d
RANGE BETWEEN 5 PRECEDING AND CURRENT ROW
) AS five_day_total
FROM totals_per_day;
2012-02-01 00:00:00 0
2012-02-02 00:00:00 0
2012-02-03 00:00:00 0
2012-02-04 00:00:00 0
2012-02-05 00:00:00 0
2012-02-06 00:00:00 0
2012-02-07 00:00:00 0
2012-02-08 00:00:00 0
2012-02-09 00:00:00 1180
2012-02-10 00:00:00 1180
2012-02-11 00:00:00 1180
2012-02-12 00:00:00 3280
2012-02-13 00:00:00 3280
2012-02-14 00:00:00 4380
2012-02-15 00:00:00 175700
2012-02-16 00:00:00 175700
2012-02-17 00:00:00 175700
2012-02-18 00:00:00 173600
2012-02-19 00:00:00 175100
2012-02-20 00:00:00 174400
2012-02-21 00:00:00 1900
2012-02-22 00:00:00 1900
2012-02-23 00:00:00 3900
2012-02-24 00:00:00 3900
2012-02-25 00:00:00 2400
2012-02-26 00:00:00 2000
2012-02-27 00:00:00 306320
2012-02-28 00:00:00 306320
2012-02-29 00:00:00 304320
2012-03-01 00:00:00 304420
2012-03-02 00:00:00 304420
2012-03-03 00:00:00 304420
2012-03-04 00:00:00 17380
2012-03-05 00:00:00 17380
2012-03-06 00:00:00 21900
2012-03-07 00:00:00 21800
2012-03-08 00:00:00 21900
2012-03-09 00:00:00 21900
2012-03-10 00:00:00 4620
2012-03-11 00:00:00 4620
2012-03-12 00:00:00 100
2012-03-13 00:00:00 1100
2012-03-14 00:00:00 1000
2012-03-15 00:00:00 1000
2012-03-16 00:00:00 1000
2012-03-17 00:00:00 1000
2012-03-18 00:00:00 1000
2012-03-19 00:00:00 0
2012-03-20 00:00:00 0
2012-03-21 00:00:00 0
2012-03-22 00:00:00 0
2012-03-23 00:00:00 0
2012-03-24 00:00:00 0
2012-03-25 00:00:00 0
2012-03-26 00:00:00 0
2012-03-27 00:00:00 0
2012-03-28 00:00:00 0
2012-03-29 00:00:00 0
2012-03-30 00:00:00 0
2012-03-31 00:00:00 0Hope this helps.
Regards. -
Analytical function help needed
hi i'm using oracle 10g.
CREATE TABLE test100(
hcim VARCHAR2(10 BYTE),
bcim VARCHAR2(10 BYTE),
num VARCHAR2(6 BYTE),
mindate varchar2(10 byte))
insert into test100 values ('03217979','03236915','76120F','10/1/2006')
insert into test100 values ('03217979','03236916','76121F','10/1/2006')
insert into test100 values ('03217979','03236917','76122F','10/1/2006')
insert into test100 values ('03217979','03236918','76123F',null)
insert into test100 values ('03217979','03236919','76124F','11/1/2009')
SELECT hcim
, bcim
, num
, mindate
, Max(TO_DATE(mindate,'MM/DD/YYYY')) OVER (PARTITION BY hcim) AS mindate1
FROM test100
;output:
03217979 03236915 76120F 10/1/2006 11/1/2009
03217979 03236916 76121F 10/1/2006 11/1/2009
03217979 03236919 76124F 11/1/2009 11/1/2009
03217979 03236918 76123F 11/1/2009
03217979 03236917 76122F 10/1/2006 11/1/2009how can i show null in mindate1 column since one of the date value in mindate has a null. Only if there is no nulls then i need to show max(mindate) in mindate1
Thanks in advanceHi,
Thanks for posting the CREATE TABLE and INSERT statements; that's very helpful.
Do you mean you want mindate1 to be NULL on every row for that hcim, because at least one row in that hcim had a NULL mindate? It would help if you posted the exact results you want. (I was typing this message before your message, clarifying this point, was posted.) It would also help to have a couple of different hcims in the sample data, at least one with a NULL mindate, and another where mindate is never NULL.
I think you want something like this:
SELECT hcim
, bcim
, num
, mindate
, FIRST_VALUE ( TO_DATE (mindate, 'MM/DD/YYYY')
) OVER ( PARTITION BY hcim
ORDER BY TO_DATE (mindate, 'MM/DD/YYYY') DESC NULLS FIRST
) AS mindate1
FROM test100
;Output:
HCIM BCIM NUM MINDATE MINDATE1
03217979 03236916 76121F 10/1/2006
03217979 03236915 76120F 10/1/2006
03217979 03236917 76122F 10/1/2006
03217979 03236919 76124F 11/1/2009
03217979 03236918 76123FStoring dates in a VARCHAR2 column is a really bad idea. Why not use a DATE column? Coding will be simpler, errors will be fewer, and execution will be faster.
Edited by: Frank Kulash on Nov 11, 2011 4:53 PM -
How does the analytical function RANK OVER() work inoracle??
Hi,
Can i get the information about how oracle internally handles RANK over() ?http://tahiti.oracle.com
http://asktom.oracle.com -
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,
VamsiSQL> 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. -
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. -
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 -
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 -
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... -
About FIRST_ROW analytic function; can anyone help?
Hi everyone,
Can anyone help me with this simple query?
Let's suppose I have this query (the with clause contains some data):
WITH T AS (
SELECT 'TEST' as COL1, 1 as COL2, 'z' as COL3 FROM dual
UNION ALL
SELECT 'TEST', 2, 'y' FROM dual
UNION ALL
SELECT 'TEST', 2, 'h' FROM dual
SELECT FIRST_VALUE(COL1) OVER (PARTITION BY COL1), COL2, COL3
FROM T;I would like to have only the first row returned. I was thinking that with FIRST_VALUE it will be possible, but it returns 3 records.
So can anyone help me to have only the first record returned?
TEST 1 zThis is just a simple example. In reality I have thousands of records. I need to get only the first record based on the name (TEST in this example). We don't really care about the other columns.
Thanks for your help,user13117585 wrote:
I would like to have only the first row returned. I was thinking that with FIRST_VALUE it will be possible, but it returns 3 records. Analytic functions don't filter rows, they just calculate values from some part of the result set.
Aggregating is the most efficient way of doing this query:
SQL> WITH T AS (
2 SELECT 'TEST' as COL1, 1 as COL2, 'z' as COL3 FROM dual
3 UNION ALL
4 SELECT 'TEST', 2, 'y' FROM dual
5 UNION ALL
6 SELECT 'TEST', 2, 'h' FROM dual
7 )
8 select col1
9 , min(col2) col2
10 , max(col3) keep (dense_rank first order by col2) col3
11 from t
12 group by col1
13 /
COL1 COL2 C
TEST 1 z
1 row selected.Regards,
Rob. -
Incorrect syntax errors in sql server function. Please help.
This is my sql server function:
create function dbo.CleanTheStringAdv (@theString nvar
char(500), @CleanMode as int)
returns nvarchar(500)
begin
--define variables
declare @strAlphaNumeric nvarchar(500)
declare @i int
declare @strChar nvarchar(500)
declare @CleanedString nvarchar(500)
--initiate values
set @CleanedString =''
set @theString = @theString + ''
SET @i = 1
--Determine if we are looking for numeric values only or numbers and letters
return case @CleanMode
WHEN 1
THEN @strAlphaNumeric = '0123456789'
WHEN 2
THEN @strAlphaNumeric = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
WHEN 3
THEN @strAlphaNumeric = '|0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
else 0
end
--Search through chars in the string passed to the function
while (@i <= Len(@theString))
begin
@strChar = substring(@theString, @i, 1)
If (charindex(@strAlphaNumeric, @strChar) <>0)
--if the current char being reviewed is valid then add it to the new string
begin
@CleanedString = @CleanedString + @strChar
end
end
return @CleanedString
and below are the syntax errors I see when I press F5:
Msg 102, Level 15, State 1, Procedure CleanTheStringAdv, Line 24
Incorrect syntax near '='.
Msg 102, Level 15, State 1, Procedure CleanTheStringAdv, Line 39
Incorrect syntax near '@strChar'.
Msg 102, Level 15, State 1, Procedure CleanTheStringAdv, Line 43
Incorrect syntax near '@CleanedString'.
Any help is highly appreciated.....Here is the full code for the function:
create function dbo.CleanTheStringAdv (@theString nvarchar(500), @CleanMode as int)
returns nvarchar(500)
as
begin
--define variables
declare @strAlphaNumeric nvarchar(500)
declare @i int
declare @strChar nvarchar(500)
declare @CleanedString nvarchar(500)
--initiate values
set @CleanedString =''
set @theString = @theString + ''
SET @i = 1
--Determine if we are looking for numeric values only or numbers and letters
SET @strAlphaNumeric = case @CleanMode
WHEN 1
THEN '0123456789'
WHEN 2
THEN '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
WHEN 3
THEN '|0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
else 0
end
--Search through chars in the string passed to the function
while (@i <= Len(@theString))
begin
set @strChar = substring(@theString, @i, 1);
If (charindex(@strAlphaNumeric, @strChar) <>0) --if the current char being reviewed is valid then add it to the new string
begin
SET @CleanedString = @CleanedString + @strChar;
end
END
return @CleanedString;
END
Maybe you are looking for
-
24" 2.93GHz V. 3.06GHZ
I am looking to get a new iMac and was curious about something: I will not be doing anything super hardcore with it, just photo editing and maybe some video editing. Would the 2.93 do the trick or would I need the 3.06? Would I even notice a differen
-
IMac G5 problem...Need help
This is my first time posting to the IMac G5 discussions, and I hope someone here can really help. Something weird is happening to my computer when it is being turned on. Nothing on the screen is displayed, and there is a constant beeping sound. Plus
-
Hi, I publish messages to JMS topic on WLS. In the payload, there is a property operatorName whose value is in: Tom,Jim,Andy,Alex and I configured the jms client with the message selector as: operatorName='Tom' However, I found that the jms client ca
-
Problem with valign="top" in safari & FF?
Hi, I have been tearing my hair out trying to work this out, I have never come across this before and can't figure out a solution.. Have a look here: http://www.chrislorddesigns.com/graphic_and_web_design_portfolio.html the descriptions next to websi
-
Menu, Button, Menuitem backgroundcolor
is there any way to change the menu , button , menuItem background color, i know there is hastable for maintaing ui component color. and one can get the that defaults value using UIManager.getDefaults(). which returns a hastable . and you can set the