Calculate percentage in SQL Oracle - sql

I have the following data and I am trying to find the percentage of purchases on weekdays vs weekends in ORACLE SQL per ID
ID
DAY
total per day
1
weekday
78
1
weekend
20
2
weekday
13
2
weekend
37
The output i am expecting is:
ID
DAY
percentage per day
1
weekday
79
1
weekend
20
2
weekday
26
2
weekend
74
the percentage is calculated by the ( total per day / sum (total per day) ) for each ID. what is the best way to do it.
Appreciate your help!

Use SUM as an analytic function:
SELECT id,
day,
100 * total_per_day / SUM(total_per_day) OVER (PARTITION BY id)
AS percentage
FROM table_name
Which, for the sample data:
CREATE TABLE table_name (ID, DAY, total_per_day) AS
SELECT 1, 'weekday', 78 FROM DUAL UNION ALL
SELECT 1, 'weekend', 20 FROM DUAL UNION ALL
SELECT 2, 'weekday', 13 FROM DUAL UNION ALL
SELECT 2, 'weekend', 37 FROM DUAL;
Outputs:
ID
DAY
PERCENTAGE
1
weekday
79.59183673469387755102040816326530612245
1
weekend
20.40816326530612244897959183673469387755
2
weekday
26
2
weekend
74
db<>fiddle here

Related

BIGQUERY SQL How to count date range per hour total

I want to count total number of placed orders between date range 01 -31 days per hour in a day
Customer_placed_order_datetime
01 01:10:38
01 01:12:38
02 01:14:30
31 23:42:22
Example outcome would be like
Date 01-31
Date 01-31 total orders
1 hour 500
2 hour 300
and so forth.. Thank you
Consider below approach
select
format_datetime('%Y-%m', Customer_placed_order_datetime) year_month,
extract(hour from Customer_placed_order_datetime) hour,
count(*) total_orders
from your_table
group by year_month, hour
if applied to sample data as in your question
with your_table as (
select datetime '2021-12-01 01:10:38' Customer_placed_order_datetime union all
select '2021-12-01 01:12:38' union all
select '2021-12-02 01:14:30' union all
select '2021-12-31 23:42:22'
)
output is

SQL Group By weeks and months in the same time (Redshift)

In the code below I am selecting 42 days period and grouping it by SNAPSHOT_WEEK (where SNAPSHOT_WEEK has a number from 1 to 52(53) during the year).
SELECT
CASE
WHEN video_code = 'A' THEN 'Seller'
WHEN video_code = 'B' THEN 'Vendor'
WHEN video_code = 'C' THEN 'Others'
END AS CATEGORY
TO_CHAR(snapshot_time - DATE_PART('dow', snapshot_time)::int + 4, 'IW') AS SNAPSHOT_WEEK,
SUM(VIOLATION_COUNT)
FROM my_table
WHERE 1=1
AND snapshot_time BETWEEN '20180505'::date - '41 days'::interval AND '20180505'::date -- to calculate WoW
GROUP BY
CATEGORY, SNAPSHOT_WEEK;
Output for this query looks like this:
CATEGORY WEEK OR MONTH SUM_VIOLATION_COUNT
A 14 954
B 14 454
C 14 299
A 15 954
B 16 454
Is it possible, in the same query, beside grouping by week, group this data by month where month should start from 28th of one month to 28th of second month?
For example, in my output I need column that will show following values:
CATEGORY WEEK OR MONTH SUM_VIOLATION_COUNT
A 14 954
B 14 454
C 14 299
A 15 954
B 16 454
C 17 299
A 28 March 9354
B 28 March 2454
C 28 March 5354
A 28 April 1354
...... ..... .....
Where "28 March" - means number of violation between 28-Feb and 28 March; "28 April" - number of violation between 28 Feb and 28 April etc.
Is that possible to do using the same query?
You can do that with WITH Subquery, this will allow you do to run the query once on the database and group by twice based on your logic.
Your query has some disconnects between your column names but again it will look like something like this
P.S. Union requires number of columns should be same in both selects
WITH ALLDATA AS (
SELECT
CASE
WHEN video_code = 'A' THEN 'Seller'
WHEN video_code = 'B' THEN 'Vendor'
WHEN video_code = 'C' THEN 'Others'
END AS CATEGORY
TO_CHAR(snapshot_time - DATE_PART('dow', snapshot_time)::int + 4, 'IW') AS SNAPSHOT_WEEK,
SUM(VIOLATION_COUNT) SUM_VIOLATION_COUNT
FROM my_table
WHERE 1=1
AND snapshot_time BETWEEN '20180505'::date - '41 days'::interval AND '20180505'::date -- to calculate WoW
GROUP BY
CATEGORY, SNAPSHOT_WEEK)
SELECT CATEGORY, SNAPSHOT_WEEK, SUM_VIOLATION_COUNT FROM ALLDATA
UNION
SELECT CATEGORY, SNAPSHOT_WEEK, SUM_VIOLATION_COUNT FROM ALLDATA
GROUP BY <your month grouping logic>
To reiterate the logic in pseudo code
WITH ALLDATA AS (
SELECT <your base data without group by> )
SELECT columns FROM ALLDATA
GROUP BY <weekly group by logic>
UNION
SELECT columns FROM ALLDATA
GROUP BY <monthly group by logic>
You would need to UNION the output of two separate queries to generate those results.
The basic rule is that one input row will map to (at most) one output row.

How to use lead lag function in oracle

I have written some query to get my resultant result as below :
Note: I have months starting from jan-2016 to jan-2018.
There are two types, either 'hist' or 'future'
Resultant dataset :
In this example : let consider combination of id1+id2+id3 as 1,2,3
type month id1 id2 id3 value
hist jan-17 1 2 3 10
hist feb-17 1 2 3 20
future jan-17 1 2 3 15
future feb-17 1 2 3 1
hist mar-17 1 2 3 2
future apr-17 1 2 3 5
My calculation logic depends on the quarter number of month .
For eg . for month of january(first month of quarter) i want the value to be : future of jan + future value of feb + future value of march .
so for jan-17 , output should be : 15+1 + 0(for march there is no corresponding future value)
for the month of feb (2nd month of quarter), value should be : hist of jan + future of feb + future of march i.e 10+1+0(future of march is not available)
Similarly for the month of march , value should be : history of jan + history of feb + future of march i.e 10+20+0(frecast of march no present) .
similarly for april,may.june(depending on quarter number of month)
I am aware of the lead lag function , but I am not able to apply it here
Can someone please help
I would not mess with lag, this can all be done with a group by if you convert your dates to quarters:
WITH
dset
AS
(SELECT DATE '2017-01-17' month, 5 VALUE
FROM DUAL
UNION ALL
SELECT DATE '2017-02-17' month, 6 VALUE
FROM DUAL
UNION ALL
SELECT DATE '2017-03-25' month, 7 VALUE
FROM DUAL
UNION ALL
SELECT DATE '2017-05-25' month, 4 VALUE
FROM DUAL)
SELECT SUM (VALUE) value_sum, TO_CHAR (month, 'q') quarter, TO_CHAR (month, 'YYYY') year
FROM dset
GROUP BY TO_CHAR (month, 'q'), TO_CHAR (month, 'YYYY');
This results in:
VALUE_SUM QUARTER YEAR
18 1 2017
4 2 2017
We can use an analytic function if you need the result on each record:
SELECT SUM (VALUE) OVER (PARTITION BY TO_CHAR (month, 'q'), TO_CHAR (month, 'YYYY')) quarter_sum, month, VALUE
FROM dset
This results in:
QUARTER_SUM MONTH VALUE
18 1/17/2017 5
18 2/17/2017 6
18 3/25/2017 7
4 5/25/2017 4
Make certain you include year, you don't want to combine quarters from different years.
Well, as said in one of the comments.. the trick lies in another question of yours & the corresponding answer. Well... it goes somewhat like this..
with
x as
(select 'hist' type, To_Date('JAN-2017','MON-YYYY') ym , 10 value from dual union all
select 'future' type, To_Date('JAN-2017','MON-YYYY'), 15 value from dual union all
select 'future' type, To_Date('FEB-2017','MON-YYYY'), 1 value from dual),
y as
(select * from x Pivot(Sum(Value) For Type in ('hist' as h,'future' as f))),
/* Pivot for easy lag,lead query instead of working with rows..*/
z as
(
select ym,sum(h) H,sum(f) F from (
Select y.ym,y.H,y.F from y
union all
select add_months(to_Date('01-JAN-2017','DD-MON-YYYY'),rownum-1) ym, 0 H, 0 F
from dual connect by rownum <=3 /* depends on how many months you are querying...
so this dual adds the corresponding missing 0 records...*/
) group by ym
)
select
ym,
Case
When MOD(Extract(Month from YM),3) = 1
Then F + Lead(F,1) Over(Order by ym) + Lead(F,2) Over(Order by ym)
When MOD(Extract(Month from YM),3) = 2
Then Lag(H,1) Over(Order by ym) + F + Lead(F,1) Over(Order by ym)
When MOD(Extract(Month from YM),3) = 3
Then Lag(H,2) Over(Order by ym) + Lag(H,1) Over(Order by ym) + F
End Required_Value
from z

extracting total days of a month and then use it to get average sales per day

Hi i've been working on this project and need to get this.
SELECT sf.ORDER_QNT, dd.ACTUAL_DATE, dd.MONTH_NUMBER
FROM sales_fact sf,
date_dim dd
WHERE dd.date_id = sf.date_id
AND dd.MONTH_NUMBER = 1;
the result is the following:
ORDER_QNT ACTUAL_DATE MONTH_NUMBER
---------- ----------- ------------
1100 05/01/13 1
100 05/01/13 1
140 06/01/13 1
110 07/01/13 1
200 08/01/13 1
500 08/01/13 1
230 08/01/13 1
500 08/01/13 1
200 08/01/13 1
53 15/01/13 1
53 22/01/13 1
Now, I want to get the average for that month (average per day).
SELECT sum(sf.ORDER_QNT)/31 as AVGPERDAY
FROM sales_fact sf,
date_dim dd
WHERE dd.date_id = sf.date_id
AND dd.MONTH_NUMBER = 1;
The question is, instead of putting 31, how can I get the total day of the month? and how can I apply that to the SELECT query. I'm pretty good with logic(c++), but this database is pretty new to me. I'm using Oracle 11g by the way. Thank you for any help.
The question is, instead of putting 31, how can I get the total day of the month?
Pick any one solution :
1.
You can add a month to a date and substract both the dates :
ADD_MONTHS(date_col, 1) - date_col
Example :
SQL> WITH dates AS(
2 SELECT to_date('05/01/13','mm/dd/rr') dt FROM dual UNION ALL
3 SELECT to_date('06/01/13','mm/dd/rr') dt FROM dual UNION ALL
4 SELECT to_date('02/01/13','mm/dd/rr') dt FROM dual)
5 SELECT ADD_MONTHS(dt, 1) - dt num_of_days_per_month
6 from dates
7 /
NUM_OF_DAYS_PER_MONTH
---------------------
31
30
28
Or,
You can extract the last day of the month :
EXTRACT(DAY FROM LAST_DAY (date_col))
Example :
SQL> WITH dates AS(
2 SELECT to_date('05/01/13','mm/dd/rr') dt FROM dual UNION ALL
3 SELECT to_date('06/01/13','mm/dd/rr') dt FROM dual UNION ALL
4 SELECT to_date('02/01/13','mm/dd/rr') dt FROM dual)
5 SELECT EXTRACT(DAY FROM LAST_DAY(dt)) num_of_days_per_month
6 from dates
7 /
NUM_OF_DAYS_PER_MONTH
---------------------
31
30
28

Oracle: Need to calculate rolling average for past 3 months where we have more than one submission per month

I've seen many examples of rolling averages in oracle but done do quite what I desire.
This is my raw data
DATE SCORE AREA
----------------------------
01-JUL-14 60 A
01-AUG-14 45 A
01-SEP-14 45 A
02-SEP-14 50 A
01-OCT-14 30 A
02-OCT-14 45 A
03-OCT-14 50 A
01-JUL-14 60 B
01-AUG-14 45 B
01-SEP-14 45 B
02-SEP-14 50 B
01-OCT-14 30 B
02-OCT-14 45 B
03-OCT-14 50 B
This is the desired result for my rolling average
MMYY AVG AREA
-------------------------
JUL-14 60 A
AUG-14 52.5 A
SEP-14 50 A
OCT-14 44 A
JUL-14 60 B
AUG-14 52.5 B
SEP-14 50 B
OCT-14 44 B
The way I need it to work is that for each MMYY, I need to look back 3 months, and AVG the scores per dept. So for example,
For Area A in OCT, in the last 3 months from oct, there were 6 studies, (45+45+50+30+45+50)/6 = 44.1
Normally I would write the query like so
SELECT
AREA,
TO_CHAR(T.DT,'MMYY') MMYY,
ROUND(AVG(SCORE)
OVER (PARTITION BY AREA ORDER BY TO_CHAR(T.DT,'MMYY') ROWS BETWEEN 2 PRECEDING AND CURRENT ROW),1)
AS AVG
FROM T
This will look over the last 3 enteries not the last 3 months
One way to do this is to mix aggregation functions with analytic functions. The key idea for average is to avoid using avg() and instead do a sum() divided by a count(*).
SELECT AREA, TO_CHAR(T.DT, 'MMYY') AS MMYY,
SUM(SCORE) / COUNT(*) as AvgScore,
SUM(SUM(SCORE)) OVER (PARTITION BY AREA ORDER BY MAX(T.DT) ROWS BETWEEN 2 PRECEDING AND CURRENT ROW) / SUM(COUNT(*)) OVER (PARTITION BY AREA ORDER BY MAX(T.DT) ROWS BETWEEN 2 PRECEDING AND CURRENT ROW)
FROM t
GROUP BY AREA, TO_CHAR(T.DT, 'MMYY') ;
Note the order by clause. If your data spans years, then using the MMYY format poses problems. It is better to use a format such as YYYY-MM for months, because the alphabetical ordering is the same as the natural ordering.
You can specify also ranges, not only rows.
SELECT
AREA,
TO_CHAR(T.DT,'MMYY') MMYY,
ROUND(AVG(SCORE)
OVER (PARTITION BY AREA
ORDER BY DT RANGE BETWEEN INTERVAL '3' MONTH PRECEDING AND CURRENT ROW))
AS AVG
FROM T
Since CURRENT ROW is the default, just ORDER BY DT RANGE INTERVAL '3' MONTH PRECEDING should work as well. Perhaps you have to do some fine-tuning, I did not test the behaviour regarding the 28/29/30/31 days per month issue.
Check the Oracle Windowing Clause for further details.
SQL> WITH DATA AS(
2 SELECT to_date('01-JUL-14','DD-MON-RR') dt, 60 score, 'A' area FROM dual UNION ALL
3 SELECT to_date('01-AUG-14','DD-MON-RR') dt, 45 score, 'A' area FROM dual UNION ALL
4 SELECT to_date('01-SEP-14','DD-MON-RR') dt, 45 score, 'A' area FROM dual UNION ALL
5 SELECT to_date('02-SEP-14','DD-MON-RR') dt, 50 score, 'A' area FROM dual UNION ALL
6 SELECT to_date('01-OCT-14','DD-MON-RR') dt, 30 score, 'A' area FROM dual UNION ALL
7 SELECT to_date('02-OCT-14','DD-MON-RR') dt, 45 score, 'A' area FROM dual UNION ALL
8 SELECT to_date('03-OCT-14','DD-MON-RR') dt, 50 score, 'A' area FROM dual UNION ALL
9 SELECT to_date('01-JUL-14','DD-MON-RR') dt, 60 score, 'B' area FROM dual UNION ALL
10 SELECT to_date('01-AUG-14','DD-MON-RR') dt, 45 score, 'B' area FROM dual UNION ALL
11 SELECT to_date('01-SEP-14','DD-MON-RR') dt, 45 score, 'B' area FROM dual UNION ALL
12 SELECT to_date('02-SEP-14','DD-MON-RR') dt, 50 score, 'B' area FROM dual UNION ALL
13 SELECT to_date('01-OCT-14','DD-MON-RR') dt, 30 score, 'B' area FROM dual UNION ALL
14 SELECT to_date('02-OCT-14','DD-MON-RR') dt, 45 score, 'B' area FROM dual UNION ALL
15 SELECT to_date('03-OCT-14','DD-MON-RR') dt, 50 score, 'B' area FROM dual)
16 SELECT TO_CHAR(T.DT, 'MON-RR') AS MMYY,
17 round(
18 SUM(SUM(SCORE)) OVER (PARTITION BY AREA ORDER BY MAX(T.DT) ROWS BETWEEN 2 PRECEDING AND CURRENT ROW)/
19 SUM(COUNT(*)) OVER (PARTITION BY AREA ORDER BY MAX(T.DT) ROWS BETWEEN 2 PRECEDING AND CURRENT ROW),1)
20 AS avg_score,
21 AREA
22 FROM data t
23 GROUP BY AREA, TO_CHAR(T.DT, 'MON-RR')
24 /
MMYY AVG_SCORE A
------ ---------- -
JUL-14 60 A
AUG-14 52.5 A
SEP-14 50 A
OCT-14 44.2 A
JUL-14 60 B
AUG-14 52.5 B
SEP-14 50 B
OCT-14 44.2 B
8 rows selected.
SQL>
From next time, I would expect you to provide the create and insert statements so that we don't have to spend time on preparing a test case.
And, why YY format? Haven't you seen the Y2K bug? Please use YYYY format.