SQL(Oracle) select the most common value (multiple tables) - sql

I want to know which seat was the most sold by individual halls?
TICKETS
IDTICKET MOVIE_IDMOVIE HALL_IDHALL PRICE SEAT ROW
1 10 2 4 10 6
2 5 2 4 10 5
3 5 2 4 10 4
4 8 5 4 3 1
5 7 5 4 4 15
6 10 7 4 7 9
7 6 2 4 14 3
HALLS
IDHALL PLACE_IDPLACE NAME NUMSEATS EQUIPMENT
1 5 A1 250 high
2 5 B1 200 medium
3 5 B2 200 medium
4 5 C2 180 medium
5 5 C2 180 medium
6 9 old hall 120 low
Display should look like
B1 10
C2 3
...

SELECT b.Name, a.Seat
FROM (SELECT Hall, Seat, COUNT(1) AS SeatCount, RANK() OVER (PARTITION BY Hall ORDER BY COUNT(1) DESC) AS SeatRank
FROM SEAT
GROUP BY Hall, Seat ) a
INNER JOIN
HALL b
ON a.HALL_IDHALL = b.IDHALL
WHERE a.SeatRank = 1

select h.name,t.hall_idhall,h.idhall, max(t.seat) from tickets t, hall h where t.hall_idhall=h.idhall
group by h.name,t.hall_idhall,h.idhall
Try the above query

Related

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);

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 )

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

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;

mysql count from same table and data from the other table

I want to display the name of the registered users with the count of regid by supplying replyid, I don't know what will be the correct query to get the results
Here are the tables.
details_table
id regid replyid
-------------------
1 1 2
2 1 3
6 2 4
5 3 4
8 2 5
9 3 5
10 4 5
11 5 5
12 2 6
13 6 6
14 4 6
15 7 7
16 8 7
17 9 7
18 10 8
19 2 9
20 2 10
21 11 10
22 12 10
reg_table
id regname
---------------
1 Sam
2 Ash
3 Tina
4 Rohny
5 Martin
6 Natasha
7 Natalia
8 Kim
9 Alex
10 John
11 Neil
12 Peter
So if replyid i.e. (10) is select from details_table by where clause, it's suppose to display the 2,11,12 i.e. (Ash,Neil,Peter) from reg_table with the count of Ash=5,Neil=1,Peter=1
SELECT a.id, a.regname, COUNT(1)
FROM reg_table a, details_table b,
details_table c
WHERE b.replyid=10
AND b.regid = a.id
AND c.regid = a.id
GROUP BY a.id, a.regname
SELECT r.regid, r.regname, count(*)
FROM (
SELECT DISTINCT regid
FROM details_table
WHERE replyid = 10
) rg
JOIN reg_table r ON rg.regid = r.regid
JOIN details_table d ON r.regid = d.regid
GROUP BY r.regid
try this
select count(dt.regid) as cnt, regname from details_table dt, reg_table rt where dt.regid = rt.Regid and dt.replyid = 10 group by rt.Regid
SELECT reg_table.regname, count(*) from reg_table, details_table where details_table.regid = reg_table.id GROUP BY reg_table.id