Oracle GRoup by 7 Days - oracle

I'm trying to group seven days period for each date, but this seems not to work.
select
START_DATE-7 FROM_DATES,
SUM(EVENT_B),
SUM(EVENT_B)
FROM (
select
nvl(ENT1.DATE_FIELD,ENT2.DATE_FIELD)as START_DATE,
nvl(ENT1.EVENT_A,0) as EVENT_A,
nvl(ENT2.EVENT_B,0) as EVENT_B
from
(select
DATE_FIELD, SUM(NR_EVENTS) as EVENT_A
from TABLE_A
where EVENT_NAME in ('CALL', 'EDIT','INSERT')
group by START_DATE,START_HOUR,SUBSCRIBER_TYPE,CO_ID
) ENT1
FULL OUTER JOIN
(select
DATE_FIELD,
SUM(NR_EVENTS) as EVENT_B
from TABLE_B
where EVENT_NAME in ('CALL', 'EDIT','INSERT')
group by DATE_FIELD
) ENT2
on (
ENT1.DATE_FIELD=ENT2.DATE_FIELD)
)
WHERE
START_DATE >= START_DATE-7
AND START_DATE <= START_DATE-1
gROUP BY
START_DATE-7
The output im getting:
9/8/2014 262187 280365
9/7/2014 509405 478245
9/6/2014 564663 537996
9/5/2014 661871 632689
9/4/2014 669788 638839
Its only sum for one day not 7
The output should be
2014-09-15, (sum of event from A side in a 7 days period - 2014-09-15-7 to 2014-09-15) , (sum of event from B side in a 7 days period - 2014-09-15-7 to 2014-09-15)
2014-09-14, (sum of event from A side in a 7 days period - 2014-09-14-7 to 2014-09-14) , (sum of event from B side in a 7 days period - 2014-09-14-7 to 2014-09-14)
2014-09-13, (sum of event from A side in a 7 days period - 2014-09-13-7 to 2014-09-13) , (sum of event from B side in a 7 days period - 2014-09-13-7 to 2014-09-13)
etc...
Can anyone help me out?

with event_dates as (
select d_field from table_a
union
select d_field from table_b
)
select
d_field from_date,
d_field to_date,
(
select nvl(sum(nr_events),0) from table_a
where event_name in ('CALL', 'EDIT', 'INSERT')
and d_field between e.d_field and e.d_field+6
) nr_events_a,
(
select nvl(sum(nr_events),0) from table_b
where event_name in ('CALL', 'EDIT', 'INSERT')
and d_field between e.d_field and e.d_field+6
) nr_events_b
from event_dates e;
I made SQL Fiddle based on your original post and notes from Lalit Kumar B. Answer above includes event_name field filter which was later added.

Your query is incorrect.
Firstly, your output doesn't match with the selected columns. If 1st column is D_FIELD1+7, then how is that in the output the first column values are D_FIELD1?
Secondly, the predicate AND D_FIELD1 BETWEEN D_FIELD1+7 AND D_FIELD1 is ambiguous.
Thirdly, BETWEEN will include the upper and lower bound, so you will get the window of 8 days and not 7 days.

You could use an Analytic function with an Interval e.g.
SELECT nvl(ent1.date_field, ent2.date_field) - 6 start_date
, nvl(ent1.date_field, ent2.date_field) end_date
, sum(event_a) OVER
(ORDER BY nvl(ent1.date_field, ent2.date_field)
RANGE numtodsinterval(6, 'day') PRECEDING
) sum_event_a
, sum(event_b) OVER
(ORDER BY nvl(ent1.date_field, ent2.date_field)
RANGE numtodsinterval(6, 'day') PRECEDING
) sum_event_b
FROM (SELECT date_field,
sum(nr_events) AS event_a
FROM table_a
WHERE event_name IN ('CALL', 'EDIT', 'INSERT')
GROUP BY date_field
) ent1
FULL OUTER JOIN
(SELECT date_field,
sum(nr_events) AS event_b
FROM table_b
WHERE event_name IN ('CALL', 'EDIT', 'INSERT')
GROUP BY date_field
) ent2
ON (ent1.date_field = ent2.date_field)

I suggest you change the way of get the sums, I prefer in this case analytic functions. Try something like this:
SELECT START_DATE - 7 FROM_DATES,
SUM (EVENT_A) OVER (ORDER BY START_DATE RANGE 7 PRECEDING),
SUM (EVENT_B) OVER (ORDER BY START_DATE RANGE 7 PRECEDING)
FROM (SELECT NVL (ENT1.DATE_FIELD, ENT2.DATE_FIELD) AS START_DATE,
NVL (ENT1.EVENT_A, 0) AS EVENT_A,
NVL (ENT2.EVENT_B, 0) AS EVENT_B
FROM ( SELECT DATE_FIELD, SUM (NR_EVENTS) AS EVENT_A
FROM TABLE_A
WHERE EVENT_NAME IN ('CALL', 'EDIT', 'INSERT')
GROUP BY START_DATE,
START_HOUR,
SUBSCRIBER_TYPE,
CO_ID) ENT1
FULL OUTER JOIN
( SELECT DATE_FIELD, SUM (NR_EVENTS) AS EVENT_B
FROM TABLE_B
WHERE EVENT_NAME IN ('CALL', 'EDIT', 'INSERT')
GROUP BY DATE_FIELD) ENT2
ON (ENT1.DATE_FIELD = ENT2.DATE_FIELD))

Related

group orders based on crossing date ranges

I need to group order together with crossing their date ranges only
scenario A.
order 1, 1.3.2020-30.6.2020
order 2, 1.5.2020-31.8.2020
order 3, 31.7.2020-31.10.2020
order 4, 31.7.2020-31.12.2020
so the output should be
order 1, order 2
order 2, order 3, order 4
order1,3,4 are not grouped because their ranges don't cross at all
scenario B.
same as above plus another order
order 5, 1.1.2020-31.12.2020
so output will be
order 1, order 2, order 5
order 2, order 3, order 4, order 5
I tried Self Join to check which start date falls in that range.
so in the range of order 1 falls only the start date of order 2 -> we have one group
then in the range of order 2 fall both start dates of order 3 and 4 -> we have second group
but then for order 3 falls start date of order 4 and opposite -> that will give another 2 groups but they are invalid because order 2 is crossing their date ranges as well and shoul be included as well and becuase there will be 3 douplicates we should display it just once as in the desired output but this approach will fail.
Thanks
the result of MATCH_RECOGNIZE solution is incorrent because order 5 should be in both groups
I use some analitycal functions to solve this:
-- create table
Create table cross_dates (order_id number, start_date date , end_date date);
-- insert dates
insert into cross_dates values( 1, to_date('01.03.2020', 'dd.mm.yyyy'), to_date('30.06.2020', 'dd.mm.yyyy'));
insert into cross_dates values( 2, to_date('01.05.2020', 'dd.mm.yyyy'), to_date( '31.08.2020', 'dd.mm.yyyy'));
insert into cross_dates values( 3, to_date('31.07.2020', 'dd.mm.yyyy'), to_date( '31.08.2020', 'dd.mm.yyyy'));
insert into cross_dates values( 4, to_date('31.07.2020', 'dd.mm.yyyy'), to_date( '31.10.2020', 'dd.mm.yyyy'));
insert into cross_dates values( 5, to_date('01.01.2020', 'dd.mm.yyyy'), to_date( '31.12.2020', 'dd.mm.yyyy'));
-- SQL
select 'Order '|| min_order_id ||': ', listagg( order_id, ',') within group (order by order_id) list
from (
select distinct min_order_id, order_id from (
with dates (cur_date, end_date, order_id, start_date) as (
select start_date, end_date, order_id, start_date
from cross_Dates
union all
select cur_date + 1, end_date, order_id,start_date
from dates
where cur_date < end_date )
select d.order_id,
min(d.order_id) over(partition by greatest(d.start_date, cd.start_date)) min_order_id
from dates d, cross_Dates cd
where d.cur_date between cd.start_date and cd.end_date ))
group by min_order_id
having count(*) > 1;
Result:
Order 1: 1,2,5
Order 2: 2,3,4,5
-- add new column and update old records
alter table cross_dates add (item varchar2(1));
update cross_dates set item = 'A';
-- insert new records B
insert into cross_dates values( 1, to_date('01.01.2020', 'dd.mm.yyyy'), to_date( '30.06.2020', 'dd.mm.yyyy'), 'B');
insert into cross_dates values( 1, to_date('01.07.2020', 'dd.mm.yyyy'), to_date( '31.12.2020', 'dd.mm.yyyy'), 'B');
My assumption:
A and B are separate orders, not going in same groups even when crossing
order 1 B - has two records as a continuations - in my understanding counts like one order : order 1 B 01.01.2020 - 21.12.2020
If my assumption are correct the SQL could look like this:
select distinct min_order_id, order_id, item from (
with dates (cur_date, end_date, order_id, start_date, item) as (
select start_date, end_date, order_id, start_date, item
from cross_Dates
union all
select cur_date + 1, end_date, order_id,start_date, item
from dates
where cur_date < end_date )
select d.order_id, d.item,
min(d.order_id) over(partition by greatest(d.start_date, cd.start_date),d.item) min_order_id
from dates d, cross_Dates cd
where d.cur_date between cd.start_date and cd.end_date and d.item = cd.item )
order by item, min_order_id;
Result:
MIN_ORDER_ID ORDER_ID I
1 1 A
1 2 A
1 5 A
2 2 A
2 3 A
2 4 A
2 5 A
5 5 A
1 1 B
If my assumption are not ok please provide me what result should look like i this case.
:)
You can use MATCH_RECOGNIZE to find groups where the next value's start date is before, or equal to, the end date of all the previous values in the group. Then you can aggregate and exclude groups that would be entirely contained in another group:
WITH groups ( id, ids, start_date, end_date ) AS (
SELECT id,
LISTAGG( grp_id, ',' ) WITHIN GROUP ( ORDER BY start_date ),
MIN( start_date ),
MIN( end_date )
FROM (
SELECT t.id,
x.id AS grp_id,
x.start_date,
x.end_date
FROM table_name t
INNER JOIN table_name x
ON (
x.start_date >= t.start_date
AND x.start_date <= t.end_date
)
)
MATCH_RECOGNIZE (
PARTITION BY id
ORDER BY start_date
MEASURES
MATCH_NUMBER() AS mno
ALL ROWS PER MATCH
PATTERN ( FIRST_ROW GROUPED_ROWS* )
DEFINE GROUPED_ROWS AS (
GROUPED_ROWS.start_date <= MIN( end_date )
)
)
WHERE mno = 1
GROUP BY id
)
SELECT id,
ids
FROM groups g
WHERE NOT EXISTS (
SELECT 1
FROM groups x
WHERE g.ID <> x.ID
AND x.start_date <= g.start_date
AND g.end_date <= x.end_date
)
Which for the sample data:
CREATE TABLE table_name ( id, start_date, end_date ) AS
SELECT 'order 1', DATE '2020-03-01', DATE '2020-06-30' FROM DUAL UNION ALL
SELECT 'order 2', DATE '2020-05-01', DATE '2020-08-31' FROM DUAL UNION ALL
SELECT 'order 3', DATE '2020-07-31', DATE '2020-10-31' FROM DUAL UNION ALL
SELECT 'order 4', DATE '2020-07-31', DATE '2020-12-31' FROM DUAL;
Outputs:
ID | IDS
:------ | :----------------------
order 2 | order 2,order 3,order 4
order 1 | order 1,order 2
I you then:
INSERT INTO table_name ( id, start_date, end_date )
VALUES ( 'order 5', DATE '2020-01-01', DATE '2020-12-31' );
The output would be:
ID | IDS
:------ | :----------------------
order 2 | order 2,order 3,order 4
order 5 | order 5,order 1,order 2
db<>fiddle here

Oracle adding a subquery in a CTE

I have the following setup, which works fine and generates output as expected.
I'm trying to add the locations subquery into the CTE so my output will have a random location_id for each row.
The subquery is straight forward and should work but I am getting syntax errors when I try to place it into the 'data's CTE. I was hoping someone could help me out.
CREATE TABLE employees(
employee_id NUMBER(6),
emp_name VARCHAR2(30)
);
INSERT INTO employees(
employee_id,
emp_name
) VALUES
(1, 'John Doe');
INSERT INTO employees(
employee_id,
emp_name
) VALUES
(2, 'Jane Smith');
INSERT INTO employees(
employee_id,
emp_name
) VALUES
(3, 'Mike Jones');
CREATE TABLE locations AS
SELECT level AS location_id,
'Door ' || level AS location_name
FROM dual
CONNECT BY level <=
with rws as (
select level rn from dual connect by level <= 5 ),
data as ( select e.*,round (dbms_random.value(1,5)
) n from employees e)
select employee_id,
emp_name,
trunc (sysdate) + dbms_random.value (0, 5) AS random_date
from rws
join data d on rn <= n
order by employee_id;
-- trying to make this work
with rws as ( select level rn from dual connect by level <= 5 ),
data as ( select e.*, loc.location_id = (
select location_id
from locations order by dbms_random.value()
fetch first 1 row only
),
round (dbms_random.value(1,5)
) n from employees e )
select employee_id,
emp_name,
trunc (sysdate) + dbms_random.value (0, 5) AS random_date
from rws
join data d on rn <= n
order by employee_id;
You need to alias the subquery column expression, rather than trying to assign it to a [variable] name. So instead of this:
with rws as ( select level rn from dual connect by level <= 5 ),
data as ( select e.*, loc.location_id = (
select location_id
from locations order by dbms_random.value()
fetch first 1 row only
),
round (dbms_random.value(1,5)
) n from employees e )
you would do this:
with rws as (
select level rn
from dual
connect by level <= 5
),
data as (
select e.*,
(
select location_id
from locations
order by dbms_random.value()
fetch first 1 row only
) as location_id,
round (dbms_random.value(1,5)) as n
from employees e
)
db<>fiddle
But yes, you'll get the same location_id for each row, which probably isn't what you want.
There are probably better ways to avoid it (or to approach whatever you're actually trying to achieve) but one option is to force the subquery to be correlated by adding something like:
where location_id != -1 * e.employee_id
db<>fiddle
although that might be expensive. It's probably worth asking a new question about that specific aspect.
I am getting the same location_id for every employee_id, which I don't want either.
The subquery is in the wrong place then; move it to the main query, and correlate against both ID and n:
with rws as (
select level rn
from dual
connect by level <= 5
),
data as (
select e.*,
round (dbms_random.value(1,5)) as n
from employees e
)
select d.employee_id,
d.emp_name,
(
select location_id
from locations
where location_id != -1 * d.employee_id * d.n
order by dbms_random.value()
fetch first 1 row only
) as location_id,
trunc (sysdate) + dbms_random.value (0, 5) AS random_date
from rws r
join data d on r.rn <= d.n
order by d.employee_id;
db<>fiddle
Or move the location part to a new CTE, I suppose, with its own row number; and join that on one of your other generated values.

How to get count by using UNION operator

i'm trying to get total count by using UNION operator but it gives wrong count.
select count(*) as companyRatings from (
select count(*) hrs from (
select distinct hrs from companyA
)
union
select count(*) financehrs from (
select distinct finance_hrs from companyB
)
union
select count(*) hrids from (
select regexp_substr(hr_id,'[^/]+',1,3) hrid from companyZ
)
union
select count(*) cities from (
select regexp_substr(city,'[^/]+',1,3) city from companyY
)
);
individual query's working fine but total count not matching.
individual results here: 12 19 3 6
present total count: 31
Actual total count:40.
so there is any alternate solution without UNION operator?
To add values you'd use +. UNION is to add data sets.
select
(select count(distinct hrs) from companyA)
+
(select count(distinct finance_hrs) from companyB)
+
(select count(regexp_substr(hr_id,'[^/]+',1,3)) from companyZ)
+
(select count(regexp_substr(city,'[^/]+',1,3)) from companyY)
as total
from dual;
But I agree with juergen d; you should not have separate tables per company in the first place.
Edit. Updated query using Sum
select sum(cnt) as companyRatings from
(
select count(*) as cnt from (select distinct hrs from companyA)
union all
select count(*) as cnt from (select distinct finance_hrs from companyB)
union all
select count(*) as cnt from (select regexp_substr(hr_id,'[^/]+',1,3) hrid from companyZ)
union all
select count(*) as cnt from (select regexp_substr(city,'[^/]+',1,3) city from companyY)
)
Previous answer:
Try this
SELECT (
SELECT count(*) hrs
FROM (
SELECT DISTINCT hrs
FROM companyA
)
)
+
(
SELECT count(*) financehrs
FROM (
SELECT DISTINCT finance_hrs
FROM companyB
)
)
+
(
SELECT count(*) hrids
FROM (
SELECT regexp_substr(hr_id, '[^/]+', 1, 3) hrid
FROM companyZ
)
)
+
(
SELECT count(*) cities
FROM (
SELECT regexp_substr(city, '[^/]+', 1, 3) city
FROM companyY
)
)
AS total_count
FROM dual

Oracle Quarter SQL Query with top three products

I am working on a small project in Oracle. I need to get the top three best-selling products and the total amounts taken on these for the year and for each of the four quarters April-June, July-September, October-December and January-March. I have already found the first part, I simply need help getting the 4 quarter totals for each product. Hope someone can help, thank you
This is the SQL commands used so far:
select * from (
select "FACTQUANTITY"."PRODUCTID" as "PRODUCTID",
"DIMPRODUCT"."PRODUCTNAME" as "PRODUCTNAME",
sum(FACTQUANTITY.QUANTITY) as "QUANTITY"
from "FACTQUANTITY" "FACTQUANTITY",
"DIMPRODUCT" "DIMPRODUCT"
where "DIMPRODUCT"."PRODUCTID"="FACTQUANTITY"."PRODUCTID"
group by FACTQUANTITY.PRODUCTID,
DIMPRODUCT.PRODUCTNAME
order by sum(FACTQUANTITY.QUANTITY) desc
)
WHERE ROWNUM <= 3;
You can start from here:
select
trunc(fact_table.date_column,'Q') as quarter,
"FACTQUANTITY"."PRODUCTID" ,
"DIMPRODUCT"."PRODUCTNAME",
sum(FACTQUANTITY.QUANTITY) as "QUANTITY"
from "FACTQUANTITY" JOIN "DIMPRODUCT"
ON "DIMPRODUCT"."PRODUCTID"="FACTQUANTITY"."PRODUCTID"
group by
FACTQUANTITY.PRODUCTID, DIMPRODUCT.PRODUCTNAME, trunc(fact_table.date_column,'Q')
;
Subsequently, you can:
with a as (<previous query>)
select *
from (
select
quarter,
productid,
productname,
quantity,
row_number() over (partition by quarter order by quantity desc) rnk
from a
)
where rnk <= 3;
SELECT *
FROM (
SELECT *,
ROW_NUMBER() OVER ( PARTITION BY quarter
ORDER BY quantity DESC, PRODUCTNAME ASC ) AS rn
FROM (
SELECT TRUNC( f.datetime, 'Q' ) AS quarter,
f.PRODUCTID,
d.PRODUCTNAME,
sum(f.QUANTITY) AS QUANTITY
FROM FACTQUANTITY f
INNER JOIN
DIMPRODUCT d
ON ( d.PRODUCTID = f.PRODUCTID )
GROUP BY TRUNC( f.datetime, 'Q' ),
FACTQUANTITY.PRODUCTID,
DIMPRODUCT.PRODUCTNAME
)
)
WHERE rn <= 3;

Calculate average values in Oracle

I want to calculate average values in Oracle tables
CREATE TABLE AGENT_HISTORY(
EVENT_ID INTEGER NOT NULL,
AGENTID INTEGER NOT NULL,
EVENT_DATE DATE NOT NULL
)
/
CREATE TABLE CPU_HISTORY(
CPU_HISTORY_ID INTEGER NOT NULL,
EVENT_ID INTEGER NOT NULL,
CPU_NAME VARCHAR2(50 ) NOT NULL,
CPU_VALUE NUMBER NOT NULL
)
/
I use this SQL query:
----- FOR 24 HOURS CPU
CURSOR LAST_24_CPU_CURSOR IS
--SELECT EVENT_DATE, CPU FROM AGENT_HISTORY WHERE NAME = NAMEIN AND EVENT_DATE >= SYSDATE-(60*24)/1440;
SELECT START_DATE, NVL(AVG(CH.CPU_VALUE),0)
FROM (SELECT START_DATE - (LVL+1)/24 START_DATE, START_DATE - LVL/24 END_DATE
FROM (SELECT SYSDATE START_DATE, LEVEL LVL FROM DUAL CONNECT BY LEVEL <= 24))
LEFT JOIN AGENT_HISTORY AH ON EVENT_DATE BETWEEN START_DATE AND END_DATE
LEFT JOIN CPU_HISTORY CH ON AH.EVENT_ID = CH.EVENT_ID
JOIN AGENT AG ON AH.AGENTID = AG.ID
WHERE AG.NAME = NAMEIN
GROUP BY START_DATE
ORDER BY 1;
This query prints only one average value. I would like to modify it to print 24 values for every hour average value. Can you help me to modify the query?
I guess your input contains data only for one of the given intervals; since you're using an INNER JOIN with AGENT which in turn is filtered by AGENT_HISTORY, you're effectively converting all your LEFT JOINs to inner ones.
I suggest you use a CROSS JOIN between AGENT and the timeslots instead:
with agent_history(event_date, agentid, event_id) as (
select timestamp '2015-11-18 09:00:07', 1, 1001 from dual
),
agent(id, name) as (
select 1, 'myAgent' from dual
),
cpu_history(event_id, cpu_value) as (
select 1001, 75.2 from dual
),
time_slots(start_date, end_date) as (
SELECT START_DATE - (LVL + 1) / 24 START_DATE,
START_DATE - LVL / 24 END_DATE
FROM (SELECT SYSDATE START_DATE,
LEVEL LVL
FROM DUAL
CONNECT BY LEVEL <= 24)
)
SELECT START_DATE,
NVL(AVG(CH.CPU_VALUE),
0)
FROM time_slots ts
CROSS JOIN AGENT AG
LEFT JOIN AGENT_HISTORY AH
ON AH.AGENTID = AG.ID
AND EVENT_DATE BETWEEN START_DATE AND END_DATE
LEFT JOIN CPU_HISTORY CH
ON AH.EVENT_ID = CH.EVENT_ID
WHERE AG.NAME = 'myAgent'
GROUP BY START_DATE
ORDER BY 1;
This ensures you get the full 24 rows (one for each timeslot).
Change start_date to to_char(start_date, 'hh24:mi') both in select and group by clauses.

Resources