How do I add a moving window to rank() using Oracle SQL - sql

I need to rank values over a moving time window. I got some direction from a blogpost by Tony Hasler at
https://tonyhasler.wordpress.com/2012/10/24/model-clause-use-cases/#comment-5116 but the solution of adding a windowing clause to non-windowing functions like median does not work with the rank() or percent_rank() functions which are analytic non-windowing functions.
Working example with median() function:
with a as (
select 'a' sector, trunc(sysdate) dt, 64 v from dual union all
select 'a' sector, trunc(sysdate)-1 dt, 2 from dual union all
select 'a' sector, trunc(sysdate)-2 dt, 4 from dual union all
select 'a' sector, trunc(sysdate)-3 dt, 128 from dual union all
select 'a' sector, trunc(sysdate)-4 dt, 8 from dual union all
select 'a' sector, trunc(sysdate)-5 dt, 16 from dual union all
select 'a' sector, trunc(sysdate)-6 dt, 32 from dual union all
select 'a' sector, trunc(sysdate)-7 dt, 256 from dual union all
select 'a' sector, trunc(sysdate)-8 dt, 1 v from dual union all
select 'a' sector, trunc(sysdate)-9 dt, 512 from dual union all
select 'b' sector, trunc(sysdate) dt, 3 from dual union all
select 'b' sector, trunc(sysdate)-1 dt, 27 from dual union all
select 'b' sector, trunc(sysdate)-2 dt, 9 from dual union all
select 'b' sector, trunc(sysdate)-3 dt, 81 from dual
)
select * from a
model
partition by (sector)
dimension by (dt)
measures (v, 0 mov_rank)
rules
(
mov_rank[ANY] = median(v)[dt between CV()-3 and CV()]
)
order by sector, dt
;
The example does not work if we replace median with rank() as in:
with a as (
select 'a' sector, trunc(sysdate) dt, 64 v from dual union all
select 'a' sector, trunc(sysdate)-1 dt, 2 from dual union all
select 'a' sector, trunc(sysdate)-2 dt, 4 from dual union all
select 'a' sector, trunc(sysdate)-3 dt, 128 from dual union all
select 'a' sector, trunc(sysdate)-4 dt, 8 from dual union all
select 'a' sector, trunc(sysdate)-5 dt, 16 from dual union all
select 'a' sector, trunc(sysdate)-6 dt, 32 from dual union all
select 'a' sector, trunc(sysdate)-7 dt, 256 from dual union all
select 'a' sector, trunc(sysdate)-8 dt, 1 v from dual union all
select 'a' sector, trunc(sysdate)-9 dt, 512 from dual union all
select 'b' sector, trunc(sysdate) dt, 3 from dual union all
select 'b' sector, trunc(sysdate)-1 dt, 27 from dual union all
select 'b' sector, trunc(sysdate)-2 dt, 9 from dual union all
select 'b' sector, trunc(sysdate)-3 dt, 81 from dual
)
select * from a
model
partition by (sector)
dimension by (dt)
measures (v, 0 mov_rank)
rules
(
mov_rank[ANY] = rank() over (order by v)[dt between CV()-3 and CV()]
)
order by sector, dt
;
I would appreciate any help.
Thanks.

This might be a little "old-fashioned", but you might be able to achieve an equivalent result using a self-join instead of analytics or model, as in something like:
with a as (
select 'a' sector, trunc(sysdate) dt, 64 v from dual union all
select 'a' sector, trunc(sysdate)-1 dt, 2 from dual union all
select 'a' sector, trunc(sysdate)-2 dt, 4 from dual union all
select 'a' sector, trunc(sysdate)-3 dt, 128 from dual union all
select 'a' sector, trunc(sysdate)-4 dt, 8 from dual union all
select 'a' sector, trunc(sysdate)-5 dt, 16 from dual union all
select 'a' sector, trunc(sysdate)-6 dt, 32 from dual union all
select 'a' sector, trunc(sysdate)-7 dt, 256 from dual union all
select 'a' sector, trunc(sysdate)-8 dt, 1 v from dual union all
select 'a' sector, trunc(sysdate)-9 dt, 512 from dual union all
select 'b' sector, trunc(sysdate) dt, 3 from dual union all
select 'b' sector, trunc(sysdate)-1 dt, 27 from dual union all
select 'b' sector, trunc(sysdate)-2 dt, 9 from dual union all
select 'b' sector, trunc(sysdate)-3 dt, 81 from dual
)
select
a.sector,
a.dt,
a.v,
count(case when self.v < a.v then self.v end) + 1 mov_rank
from
a,
a self
where
self.sector = a.sector
and
self.dt between a.dt - 3 and a.dt + 3
group by
a.sector,
a.dt,
a.v
order by
a.sector,
a.dt,
a.v;

Related

How to count distinct flags in the sql

BELOW IS SNIPPET OF MY DATA
Here is the sample creation code of testing.
CREATE TABLE MYGROUP ( Category,PERSON,Flag ) AS
SELECT 'Cat1','A','1' FROM DUAL
UNION ALL SELECT 'Cat1','A','0' FROM DUAL
UNION ALL SELECT 'Cat1','A','1' FROM DUAL
UNION ALL SELECT 'Cat1','B','1' FROM DUAL
UNION ALL SELECT 'Cat1','B','0' FROM DUAL
UNION ALL SELECT 'Cat2','A','0' FROM DUAL
UNION ALL SELECT 'Cat2','A','0' FROM DUAL
UNION ALL SELECT 'Cat2','A','0' FROM DUAL
UNION ALL SELECT 'Cat2','B','1' FROM DUAL
UNION ALL SELECT 'Cat2','B','1' FROM DUAL
UNION ALL SELECT 'Cat2','B','0' FROM DUAL
UNION ALL SELECT 'Cat3','X','0' FROM DUAL
UNION ALL SELECT 'Cat3','Y','0' FROM DUAL;
Desired Output:
Category - Count of Distinct Persons with Flag = 1
Cat1 - 2
Cat2 - 1
Cat3 - 0
I need to get my code in Big query to get distinct counts of persons. It shouldnt double count.
You can use conditional aggregation
SELECT
Category,
COUNT(DISTINCT CASE WHEN Flag = 1 THEN PERSON END)
FROM MYGROUP
GROUP BY Category;

convert single column to multiple rows

I have output like:
03-19-2020
03-20-2020
03-21-2020
03-22-2020
How can I change this to display like:
v_datecol1 v_datecol2 v_datecol3 ------ v_datecoln
03-19-2020 03-20-2020 03-21-2020 03-04-2020
the 4 dates i mentioned as an example i will have 25 dates from today and i need all in seperate columns like above
Try:
with dt_tab as
(
select '03-19-2020' dt from dual union
select '03-20-2020' dt from dual union
select '03-21-2020' dt from dual union
select '03-22-2020' dt from dual
)
select listagg(dt,' ') within group (order by dt) rows_as_columns from dt_tab;
Based on your comment you can play with PIVOT, for example:
with dt_tab as
(
select dt, rownum row_num from
(
select '03-19-2020' dt from dual union
select '03-20-2020' dt from dual union
select '03-21-2020' dt from dual union
select '03-22-2020' dt from dual union
select '03-23-2020' dt from dual union
select '03-24-2020' dt from dual union
select '03-25-2020' dt from dual union
select '03-26-2020' dt from dual union
select '03-27-2020' dt from dual union
select '03-28-2020' dt from dual union
select '03-29-2020' dt from dual union
select '03-30-2020' dt from dual union
select '03-31-2020' dt from dual union
select '04-01-2020' dt from dual union
select '04-02-2020' dt from dual union
select '04-03-2020' dt from dual union
select '04-04-2020' dt from dual union
select '04-05-2020' dt from dual union
select '04-06-2020' dt from dual union
select '04-07-2020' dt from dual union
select '04-08-2020' dt from dual union
select '04-09-2020' dt from dual union
select '04-10-2020' dt from dual union
select '04-11-2020' dt from dual union
select '04-12-2020' dt from dual
)
order by dt
)
select * from
(
select dt_tab.* from dt_tab
)
pivot
(
min(dt) for (row_num) in (1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25)
)
;

Optimize Group by Hour Query

Please help to optimize my query. It looks too bulky.
I guess there is a better way to work with hours (datetime) in sql
There is a table dauditnew that is populated and population datetime is stored inside auditdate column.
Query returns rows count by hour.
SELECT t.Hour, COUNT(t.Hour) as Count FROM dauditnew d,
(SELECT 0 as Hour FROM DUAL UNION
SELECT 1 FROM DUAL UNION
SELECT 2 FROM DUAL UNION
SELECT 3 FROM DUAL UNION
SELECT 4 FROM DUAL UNION
SELECT 5 FROM DUAL UNION
SELECT 6 FROM DUAL UNION
SELECT 7 FROM DUAL UNION
SELECT 8 FROM DUAL UNION
SELECT 9 FROM DUAL UNION
SELECT 10 FROM DUAL UNION
SELECT 11 FROM DUAL UNION
SELECT 12 FROM DUAL UNION
SELECT 13 FROM DUAL UNION
SELECT 14 FROM DUAL UNION
SELECT 15 FROM DUAL UNION
SELECT 16 FROM DUAL UNION
SELECT 17 FROM DUAL UNION
SELECT 18 FROM DUAL UNION
SELECT 19 FROM DUAL UNION
SELECT 20 FROM DUAL UNION
SELECT 21 FROM DUAL UNION
SELECT 22 FROM DUAL UNION
SELECT 23 FROM DUAL) t
where
d.auditdate >= TO_DATE('25.04.2017 ' || t.Hour, 'dd.mm.yyyy HH24') and
d.auditdate <= TO_DATE('25.04.2017 ' || t.Hour || '_59_59', 'dd.mm.yyyy HH24_MI_SS')
group by t.Hour
You want to start with something like this:
select trunc(d.auditdate, 'HH24') as hh, count(*)
from dauditnew d
where d.auditdate >= '2017-04-25' and d.auditdate < '2017-04-26'
group by trunc(d.auditdate, 'HH24')
order by hh;
If this misses hours, then you can use a left join with this as a subquery.

How to count consecutive duplicates in a table?

I have below question:
Want to find the consecutive duplicates
SLNO NAME PG
1 A1 NO
2 A2 YES
3 A3 NO
4 A4 YES
6 A5 YES
7 A6 YES
8 A7 YES
9 A8 YES
10 A9 YES
11 A10 NO
12 A11 YES
13 A12 NO
14 A14 NO
We will consider the value of PG column and I need the output as 6 which is the count of maximum consecutive duplicates.
It can be done with Tabibitosan method. Run this, to understand it:
with a as(
select 1 slno, 'A' pg from dual union all
select 2 slno, 'A' pg from dual union all
select 3 slno, 'B' pg from dual union all
select 4 slno, 'A' pg from dual union all
select 5 slno, 'A' pg from dual union all
select 6 slno, 'A' pg from dual
)
select slno, pg, newgrp, sum(newgrp) over (order by slno) grp
from(
select slno,
pg,
case when pg <> nvl(lag(pg) over (order by slno),1) then 1 else 0 end newgrp
from a
);
Newgrp means a new group is found.
Result:
SLNO PG NEWGRP GRP
1 A 1 1
2 A 0 1
3 B 1 2
4 A 1 3
5 A 0 3
6 A 0 3
Now, just use a group by with count, to find the group with maximum number of occurrences:
with a as(
select 1 slno, 'A' pg from dual union all
select 2 slno, 'A' pg from dual union all
select 3 slno, 'B' pg from dual union all
select 4 slno, 'A' pg from dual union all
select 5 slno, 'A' pg from dual union all
select 6 slno, 'A' pg from dual
),
b as(
select slno, pg, newgrp, sum(newgrp) over (order by slno) grp
from(
select slno, pg, case when pg <> nvl(lag(pg) over (order by slno),1) then 1 else 0 end newgrp
from a
)
)
select max(cnt)
from (
select grp, count(*) cnt
from b
group by grp
);
with test as (
select 1 slno,'A1' name ,'NO' pg from dual union all
select 2,'A2','YES' from dual union all
select 3,'A3','NO' from dual union all
select 4,'A4','YES' from dual union all
select 6,'A5','YES' from dual union all
select 7,'A6','YES' from dual union all
select 8,'A7','YES' from dual union all
select 9,'A8','YES' from dual union all
select 10,'A9','YES' from dual union all
select 11,'A10','NO' from dual union all
select 12,'A11','YES' from dual union all
select 13,'A12','NO' from dual union all
select 14,'A14','NO' from dual),
consecutive as (select row_number() over(order by slno) rr, x.*
from test x)
select x.* from Consecutive x
left join Consecutive y on x.rr = y.rr+1 and x.pg = y.pg
where y.rr is not null
order by x.slno
And you can control output with condition in where.
where y.rr is not null query returns duplicates
where y.rr is null query returns "distinct" values.
Just for completeness, here's the actual Tabibitosan method:
with sample_data as (select 1 slno, 'A1' name, 'NO' pg from dual union all
select 2 slno, 'A2' name, 'YES' pg from dual union all
select 3 slno, 'A3' name, 'NO' pg from dual union all
select 4 slno, 'A4' name, 'YES' pg from dual union all
select 6 slno, 'A5' name, 'YES' pg from dual union all
select 7 slno, 'A6' name, 'YES' pg from dual union all
select 8 slno, 'A7' name, 'YES' pg from dual union all
select 9 slno, 'A8' name, 'YES' pg from dual union all
select 10 slno, 'A9' name, 'YES' pg from dual union all
select 11 slno, 'A10' name, 'NO' pg from dual union all
select 12 slno, 'A11' name, 'YES' pg from dual union all
select 13 slno, 'A12' name, 'NO' pg from dual union all
select 14 slno, 'A14' name, 'NO' pg from dual)
-- end of mimicking a table called "sample_data" containing your data; see SQL below:
select max(cnt) max_pg_in_queue
from (select count(*) cnt
from (select slno,
name,
pg,
row_number() over (order by slno)
- row_number() over (partition by pg
order by slno) grp
from sample_data)
where pg = 'YES'
group by grp);
MAX_PG_IN_QUEUE
---------------
6
SELECT MAX(consecutives) -- Block 1
FROM (
SELECT t1.pg, t1.slno, COUNT(*) AS consecutives -- Block 2
FROM test t1 INNER JOIN test t2 ON t1.pg = t2.pg
WHERE t1.slno <= t2.slno
AND NOT EXISTS (
SELECT * -- Block 3
FROM test t3
WHERE t3.slno > t1.slno
AND t3.slno < t2.slno
AND t3.pg != t1.pg
)
GROUP BY t1.pg, t1.slno
);
The query calculates the result in following way:
Extract all couples of records that don't have a record with different value of PG in between (blocks 2 and 3)
Group them by PG value and starting SLNO value -> this counts the consecutive values for any [PG, (starting) SLNO] couple (block 2);
Extract Maximum value from query 2 (block 1)
Note that the query may be simplified if the slno field in table contains consecutive values, but this seems not your case (in your example record with SLNO = 5 is missing)
Only requiring a single aggregation query and no joins (the rest of the calculation can be done with ROW_NUMBER, LAG and LAST_VALUE):
SELECT MAX( num_before_in_queue ) AS max_sequential_in_queue
FROM (
SELECT rn - LAST_VALUE( has_changed ) IGNORE NULL OVER ( ORDER BY ROWNUM ) + 1
AS num_before_in_queue
FROM (
SELECT pg,
ROW_NUMBER() OVER ( ORDER BY slno ) AS rn,
CASE pg WHEN LAG( pg ) OVER ( ORDER BY slno )
THEN NULL
ELSE ROW_NUMBER() OVER ( ORDER BY sl_no )
END AS change
FROM table_name
)
WHERE pg = 'Y'
);
Try to use row_number()
select
SLNO,
Name,
PG,
row_number() over (partition by PG order by PG) as 'Consecutive'
from
<table>
order by
SLNO,
NAME,
PG
This is should work with minor tweaking.
--EDIT--
Sorry, partiton by PG.
The partitioning tells the row_number when to start a new sequence.

How to display Oracle query output in Matrix/Pivot format

I would like to display the output in Matrix/Pivot format of the following query.
Query :
SELECT
SUBSTR(mon, 4, 6) month,
rmbs_cd,
scdta,
cl_nm,
br_cd,
brd_nm,
prod,
prod_nm,
SUM(sale_net) sales
FROM (SELECT
LAST_DAY(x.deli_dt) mon,
x.rmbs_cd,
x.sc_cd || x.dist_cd || x.tha_cd || x.un_cd || x.cl_id scdta,
INITCAP(cl_nm) cl_nm,
a.br_cd,
brd_nm,
a.cat_cd || a.prd_cd prod,
prod_nm,
sale_cd,
Nvl(sum(a.sale_net) - sum(rt_qty * flat_rt), 0) sale_net
FROM bill_det a, bill_mas x, cl_info c, inv_brand d, inv_prod p
WHERE (a.bill_no = x.bill_no AND a.sc_cd = x.sc_cd)
AND x.fl_mvh IN ('1', '4')
AND x.deli_dt BETWEEN '01-JUL-15' AND '31-DEC-15'
AND a.br_cd = d.br_cd AND d.div_cd = '1'
AND a.typ_cd || a.cat_cd || a.prd_cd = p.typ_cd || p.cat_cd || p.prd_cd
AND p.typ_cd = '09'
AND x.sc_cd = c.sc_cd (+)
AND x.dist_cd = c.dist_cd (+)
AND x.tha_cd = c.tha_cd (+)
AND x.un_cd = c.un_cd (+)
AND x.cl_id = c.cl_id (+)
AND c.div_cd IN ('1', '4')
AND sale_cd IN ('IM', 'IC', 'IN')
AND cancl IS NULL
GROUP BY
LAST_DAY(x.deli_dt),
x.rmbs_cd,
x.sc_cd || x.dist_cd || x.tha_cd || x.un_cd || x.cl_id,
cl_nm,
a.br_cd,
brd_nm,
a.cat_cd || a.prd_cd,
prod_nm,
sale_cd
)
GROUP BY SUBSTR(mon, 4, 6), rmbs_cd, scdta, cl_nm, br_cd, brd_nm, prod, prod_nm
ORDER BY 1, 2, 3
Result :
Expected Output :
I would like to display each individual month in different vertical columns sequentially in Oracle.
Hisomething like this (tested on Oracle 11gr2)? Using Pivot function:
WITH T AS(SELECT 'A' AS PROD, 1 AS VAL, 1 AS myMONTH FROM DUAL
UNION ALL
SELECT 'A' AS PROD, 20 AS VAL, 2 AS myMONTH FROM DUAL
UNION ALL
SELECT 'A' AS PROD, 33 AS VAL, 3 AS myMONTH FROM DUAL
UNION ALL
SELECT 'A' AS PROD, 13 AS VAL, 4 AS myMONTH FROM DUAL
UNION ALL
SELECT 'B' AS PROD, 3211 AS VAL, 5 AS myMONTH FROM DUAL
UNION ALL
SELECT 'C' AS PROD, 1 AS VAL, 6 AS myMONTH FROM DUAL
UNION ALL
SELECT 'D' AS PROD, 1 AS VAL, 7 AS myMONTH FROM DUAL
UNION ALL
SELECT 'D' AS PROD, 32 AS VAL, 7 AS myMONTH FROM DUAL
UNION ALL
SELECT 'E' AS PROD, 1 AS VAL, 8 AS myMONTH FROM DUAL
UNION ALL
SELECT 'B' AS PROD, 1 AS VAL, 9 AS myMONTH FROM DUAL
UNION ALL
SELECT 'G' AS PROD, 2131 AS VAL, 9 AS myMONTH FROM DUAL
UNION ALL
SELECT 'A' AS PROD, 1 AS VAL, 10 AS myMONTH FROM DUAL
UNION ALL
SELECT 'H' AS PROD, 1 AS VAL, 11 AS myMONTH FROM DUAL
UNION ALL
SELECT 'J' AS PROD, 234 AS VAL, 1 AS myMONTH FROM DUAL
UNION ALL
SELECT 'J' AS PROD, 432 AS VAL, 3 AS myMONTH FROM DUAL
UNION ALL
SELECT 'J' AS PROD, 22 AS VAL, 5 AS myMONTH FROM DUAL
UNION ALL
SELECT 'J' AS PROD, 25546 AS VAL, 5 AS myMONTH FROM DUAL)
SELECT *
FROM (
SELECT PROD, VAL, MYMONTH
FROM T)
PIVOT(SUM(VAL) FOR MYMONTH IN (1 AS JAN, 2 AS FEB, 3 AS MAR, 4 AS APR, 5 AS MAY, 6 AS JUN,
7 AS JUL, 8 AS AGO, 9 AS SEPT, 10 AS OCT, 11 AS NOV, 12 AS DIC)
)
ORDER BY
PROD;
Igor