summarising a 3 months sales report across 2 branches into top 3 product for each month - sql

I have the following REPORT table
m = month,
pid = product_id,
bid = branch_id,
s = sales
m pid bid s
--------------------------
1 1 1 20
1 3 1 11
1 2 1 14
1 4 1 16
1 5 1 31
1 1 2 30
1 3 2 10
1 2 2 24
1 4 2 17
1 5 2 41
2 3 1 43
2 5 1 21
2 4 1 10
2 1 1 5
2 2 1 12
2 3 2 22
2 5 2 10
2 4 2 5
2 1 2 4
2 2 2 10
3 3 1 21
3 5 1 10
3 4 1 44
3 1 1 4
3 2 1 14
3 3 2 10
3 5 2 5
3 4 2 6
3 1 2 7
3 2 2 10
I'd like to have a summary of this sales table
by showing the top 3 sales among the products across all branches.
something like this:
m pid total
---------------------
1 5 72
1 1 50
1 4 33
2 3 65
2 5 31
2 2 22
3 4 50
3 3 31
3 2 24
so on month 1, product #5 has the highest total sales with 72, followed by product #1 is 50.. and so on. if i could separate them into different table for each month would be better
so far what i can do is make a summary for 1 month and shows the entire thing and not top 3.
select pid, sum(s)
from report
where m = 1
group by pid
order by sum(s);
thanks a lot!

Most databases support the ANSI standard window functions. You can do what you want with row_number():
select m, pid, s
from (select r.m, r.pid, sum(s) as s,
row_number() over (partition by m order by sum(s) desc) as seqnum
from report r
group by r.m, r.pid
) r
where seqnum <= 3
order by m, s desc;

Related

SQL append data based on multiple dates

I have two tables; one contains encounter dates and the other order dates. They look like this:
id enc_id enc_dt
1 5 06/11/20
1 6 07/21/21
1 7 09/15/21
2 2 04/21/20
2 5 05/05/20
id enc_id ord_dt
1 1 03/7/20
1 2 04/14/20
1 3 05/15/20
1 4 05/30/20
1 5 06/12/20
1 6 07/21/21
1 7 09/16/21
1 8 10/20/21
1 9 10/31/21
2 1 04/15/20
2 2 04/21/20
2 3 04/30/20
2 4 05/02/20
2 5 05/05/20
2 6 05/10/20
The order and encounter date can be the same, or differ slightly for the same encounter ID. I'm trying to get a table that contains all order dates before each encounter date. So the data would like this:
id enc_id enc_dt enc_key
1 1 03/7/20 5
1 2 04/14/20 5
1 3 05/15/20 5
1 4 05/30/20 5
1 5 06/11/20 5
1 1 03/7/20 6
1 2 04/14/20 6
1 3 05/15/20 6
1 4 05/30/20 6
1 5 06/12/20 6
1 6 07/21/21 6
1 1 03/7/20 7
1 2 04/14/20 7
1 3 05/15/20 7
1 4 05/30/20 7
1 5 06/12/20 7
1 6 07/21/21 7
1 7 09/15/21 7
2 1 04/15/20 2
2 2 04/21/20 2
2 1 04/15/20 5
2 2 04/21/20 5
2 3 04/30/20 5
2 4 05/02/20 5
2 5 05/05/20 5
Is there a way to do this? I am having trouble figuring out how to append the orders and encounter table for each encounter based on orders that occur before a certain date.
You may join the two tables as the following:
SELECT O.id, O.enc_id, O.ord_dt, E.enc_id
FROM
order_tbl O
JOIN encounter_tbl E
ON O.ord_dt <= E.enc_dt AND
O.id = E.id
See a demo from db<>fiddle.

I have 3 table missing data find in SQL

enter image description here
select * from TimeSheet
WeekID StudentID Hours
1 1 40
2 1 40
4 1 40
1 2 40
2 2 40
3 2 4
4 2 40
1 4 40
3 3 40
select * from Week
WeekID
1
2
3
4
select * from Student
StudentID StudentName
1 ABC
2 XYZ
3 ASD
4 QWE
After the join I have these results.
If have this data using three tables
select TS.WeekID
, TS.StudentID
, TS.[Hours]
, SS.StudentID
, WW.WeekID
FROM TimeSheet TS
left JOIN Student SS ON SS.StudentID = TS.StudentID
left JOIN [Week] WW ON WW.WeekID = TS.WeekID
WeekID StudentID Hours StudentID WeekID
1 1 40 1 1
2 1 40 1 2
4 1 40 1 4
1 2 40 2 1
2 2 40 2 2
3 2 4 2 3
4 2 40 2 4
3 3 40 3 3
1 4 40 4 1
Expectation Result is ...
WeekID StudentID Hours StudentID WeekID
3 1 40 1 2
1 3 40 3 3
2 3 40 3 3
4 3 40 3 3
2 4 40 4 1
3 4 40 4 1
4 4 40 4 1
I want to get for example:
student 1 and week 3 is missing
student 2 no week is missing
and student 3 1,2 and 4 week is missing
and student 4 2,3,4 week is missing.
Please help me out.
If you cross join your Week and Student tables to get all combinations, and then use not exists to determine where no TimeSheet record exists.
select W.WeekID, S.StudentID
from [Week] W
cross join Student S
where not exists (select 1 from TimeSheet T where T.StudentID = S.StudentID and T.WeekID = W.WeekID);

How to order "group" of row?

This is the query:
SELECT WorkTypeId, WorktypeWorkID, LevelID
FROM Worktypes as w
LEFT JOIN WorktypesWorks as ww on w.ID = ww.WorktypeID
LEFT JOIN WorktypesWorksLevels as wwl on ww.ID = wwl.WorktypeWorkID
This is the result:
WorkTypeId WorktypeWorkID LevelID
1 1 1
1 1 2
1 1 3
1 2 1
1 2 2
1 2 3
1 3 1
1 4 1
1 4 2
1 5 1
NULL NULL NULL
3 19 2
4 6 1
4 7 1
4 7 2
4 7 3
4 17 1
4 17 2
4 18 1
4 18 2
NULL NULL NULL
I'd like to order the block of rows of each WorktypeWorkID, placing at the top the blocks which have the lower LevelID within the block.
Here's the result that I'd like to get:
WorkTypeId WorktypeWorkID LevelID
NULL NULL NULL
NULL NULL NULL
1 3 1 // blocks which have MinLevel 1
1 5 1
4 6 1
1 4 1 // blocks which have MinLevel 2
1 4 2
3 19 2
4 17 1
4 17 2
4 18 1
4 18 2
1 1 1 // blocks which have MinLevel 3
1 1 2
1 1 3
1 2 1
1 2 2
1 2 3
4 7 1
4 7 2
4 7 3
I think this is what you are looking for:
SELECT WorkTypeId, WorktypeWorkID, LevelID, MAX(LevelID) OVER (PARTITION BY WorktypeWorkID) as maxLevelID
FROM Worktypes as w
LEFT JOIN WorktypesWorks as ww on w.ID = ww.WorktypeID
LEFT JOIN WorktypesWorksLevels as wwl on ww.ID = wwl.WorktypeWorkID
ORDER BY maxLevelID

Pandas sum with groupby on condition

I have this dataframe:
id priority quantity
0 A 1 2
1 A 2 4
2 A 3 4
3 A 4 2
4 B 1 5
5 B 2 7
6 B 3 2
7 B 4 3
that I want to turn into this one:
id priority quantity cumulativeQuantity
0 A 1 2 2
1 A 2 4 6
2 A 3 4 10
3 A 4 2 12
4 B 1 5 5
5 B 2 7 12
6 B 3 2 14
7 B 4 3 17
Columns id, priority and quantity haven't changed.
cumulativeQuantity is the sum, by id, of all quantity from 1 to n where n is the priority of the current row.
priority can take any value. Only orders matter. We sum if priority is lower than the priority of the current row.
ANSWER:
df.groupby(['id','priority']).sum().groupby(level=0).cumsum().reset_index()

How to create select statements that include averages in sqlplus

I am very new to answering queries and using sql language, so I am having a hard time answering this one query.
The exact wording of it is: "Print the player_id and average points scored of players who scored an average of points that is greater than the average of points scored in arena 1."
This query is very confusing and I don't quite understand what I need to enter into my server to pull up the information it wants.
Here is the table that I have to use and it is named team_player_arena:
TEAM_ID PLAYER_ID ARENA_ID POINTS
---------- ---------- ---------- ----------
1 1 1 20
1 1 4 17
2 3 1 32
2 3 2 22
2 3 3 13
2 3 4 25
2 3 5 6
2 3 6 14
2 3 7 18
2 5 2 11
3 3 1 20
3 4 2 5
4 6 3 23
4 6 7 18
5 2 2 24
5 2 4 10
5 5 5 25
5 5 7 16
5 6 2 24
5 1 4 33
5 3 4 31
5 4 4 26
5 5 4 14
5 6 4 5
I understood as you want the list of players and their average score, Who has their average score above average score of arena 1
(SELECT player_id, avg(points)
FROM player_table,
GROUP BY player_id
Having avg(points) > (SELECT avg(points)
from player_table
where arena_id =1 )