How to create select statements that include averages in sqlplus - sql

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 )

Related

SQL IFELSE Statement To create Sum Variable

TABLE 1
STUDENT TIME SCORE
1 1 4
1 2 3
1 3 4
2 1 2
2 2 2
2 3 8
3 3 10
3 4 10
4 1 1
4 2 3
4 3 2
4 4 4
4 5 4
4 6 5
I have TABLE 1. I wish to group and SUM(SCORE) for each STUDENT and TIME 1-2, 3-4, 5-6 to create this TABLE 2
STUDENT TIME TOTALSCORE
1 1-2 7
1 3-4 4
1 5-6 NA
2 1-2 4
2 3-4 8
2 5-6 NA
3 1-2 NA
3 3-4 20
3 5-6 NA
4 1-2 3
4 3-4 6
4 5-6 4
However I have BIG DATA so Wish to start by doing this
select DISTINCT(TIME) from TABLE1
1
2
3
4
5
6
and then basically take all TIME values >= 1 & < 2 as T1; >=2 & < 3 as T2; it matters because we use #.# where the first # is the year and the second # is the trimester. So there are values such as 1.1, 1.2, 1.3 but I don't wish to list that out all the time
Using integer math we can use (time-1)/2 to give us groups of all times between 1-2, 3-4, 5-6, 7-8 etc.
select student
,sum(score) as total_score
,concat((time+1)/2*2-1, '-', (time+1)/2*2) as semester
from t
group by student, (time+1)/2
order by student
student
total_score
semester
1
7
1-2
1
4
3-4
2
4
1-2
2
8
3-4
3
20
3-4
4
4
1-2
4
6
3-4
4
9
5-6
Fiddle

Pandas function to group by cumulative sum and return another column when a certain amount is reached

Here it is my problem.
I got a dataframe like this:
ID item amount level
1 1 10 5
1 1 10 10
2 4 15 5
2 9 30 8
2 4 10 10
2 4 10 20
3 4 10 4
3 4 10 6
and I need to know, per each id, at what level the cumulative sum of each item reaches a fixed amount.
For example, If I need to know the first time when a given items reach an amount of 20 or more for a user.
I would like to have something like:
ID item amount level
1 1 10 5
1 1 20 10
2 4 15 5
2 9 30 8
2 4 25 10
2 4 40 20
3 4 10 4
3 4 20 6
and then something like a list or a dictionary in which I can store the results. for example:
d[item_number] = [list_of_levels_per_id_when_20_is_reached]
In this example:
{1: [10], 4: [10,6], 9: [8]}
cumsum
You can perform the cumsum post group with:
df['amount_cumsum'] = df.groupby(['ID', 'item'])['amount'].cumsum()
Output (as separate column for clarity):
ID item amount level amount_cumsum
0 1 1 10 5 10
1 1 1 10 10 20
2 2 4 15 5 15
3 2 9 30 8 30
4 2 4 10 10 25
5 3 4 10 4 10
6 3 4 10 6 20
dictionary
(df[df['amount_cumsum'].ge(20)]
.groupby(['item'])['level'].agg(list)
.to_dict()
)
Output:
{1: [10], 4: [10, 6], 9: [8]}

SQL Select Distinct Group By Having Two Items

I currently have a table with data like below.
How would I go about grouping by HeaderId and get the distinct HeaderId where it contains multiple specified items?
An example is to return which HeaderId contains NTNB and NMPTN locations. I use the SQL below and then use C# to manually check if they are the same HeaderId .
SELECT DISTINCT([HeaderId]) FROM [dbo].[timings] WHERE Location = 'NTNB'
SELECT DISTINCT([HeaderId]) FROM [dbo].[timings] WHERE Location = 'NMPTN'
An expected outcome looking for distinct HeaderId's containing NTNB and NMPTN for the data below would be 4.
HeaderId Ordinal Location
3 0 KRKYLEJ
3 1 IRNVLJN
3 2 LGML
3 3 TRWLJN
3 4 STAPLFD
3 5 TOTODSL
4 0 CREWBHM
4 1 CREWBHJ
4 2 MADELEY
4 3 NTNB
4 4 STAFFRD
4 5 STAFTVJ
4 6 WHHSJN
4 7 COLWICH
4 8 RUGLYNJ
4 9 RUGL
4 10 LCHTNJ
4 11 AMNGTNJ
4 12 NNTN
4 13 RUGBTVJ
4 14 RUGBY
4 15 HMTNJ
4 16 LNGBKBY
4 17 NMPTN
4 18 HANSLPJ
4 19 MKNSCEN
4 20 DNBGHSJ
4 21 BLTCHLY
4 22 LEDBRNJ
4 23 TRING
4 24 BONENDJ
4 25 WATFDJ
4 26 HROW
4 27 WMBY
4 28 WLSDNBJ
4 29 HARLSJN
4 30 WLSDWLJ
4 31 CMDNJN
4 32 CMDNSTH
4 33 EUSTON
4 34 CMDNSTH
4 35 CMDNJN
4 36 QPRKAC
Aggregate by the HeaderId and then assert that both locations are present:
SELECT HeaderId
FROM timings
WHERE Location IN ('NTNB', 'NMPTN')
GROUP BY HeaderId
HAVING MIN(Location) <> MAX(Location)

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;

Update Query in SQL with numeric pattern in MS Access

Good Day All,
I need assistance in an creating an update query that groups my data.
The data in my table is actually spatial in nature and can be thought of a matrix that is 10 columns by 5 rows. I have the ObjectID, Row and Column but I want the column DesiredResult which is a 2x2 grouping of the rows & columns.
So the R,Cs of 1,1 1,2, 2,1 and 2,2, will have a DesiredResult of 1 while the 1,3 1,4 2,3 2,4 will have a DesiredResult of 2 and so on (see below for an example) ....
I was able to create the R and C columns using a combination of Quotient & Mod so I assume I would do somethign similar but I am stuck. How would I go about this query in MS Access ?
ObjectID R C DesiredResult
1 1 1 1
2 1 2 1
3 1 3 2
4 1 4 2
5 1 5 3
6 1 6 3
7 1 7 4
8 1 8 4
9 1 9 5
10 1 10 5
11 2 1 1
12 2 2 1
13 2 3 2
14 2 4 2
15 2 5 3
16 2 6 3
17 2 7 4
18 2 8 4
19 2 9 5
20 2 10 5
21 3 1 6
22 3 2 6
23 3 3 7
24 3 4 7
25 3 5 8
26 3 6 8
27 3 7 9
28 3 8 9
29 3 9 10
30 3 10 10
31 4 1 6
32 4 2 6
33 4 3 7
34 4 4 7
35 4 5 8
36 4 6 8
37 4 7 9
38 4 8 9
39 4 9 10
40 4 10 10
41 5 1 11
42 5 2 11
43 5 3 12
44 5 4 12
45 5 5 13
46 5 6 13
47 5 7 14
48 5 8 14
49 5 9 15
50 5 10 15
Something like ... ?
SELECT a.Row, a.Col, Col\2 AS D1, Col Mod 2 AS D2, [D1]+[D2] AS Desired
FROM table AS a
ORDER BY a.Row, a.Col;
Remou had a close approximation but it turns out this gives me what I need. I needed both a row and a column index.
SELECT ObjectID, R, C,
Int(([C]-1)/2) AS ColIndex,
Int(([R]-1)/2) AS RowIndex,
[RowIndex]*5+[ColIndex]+1 AS DesiredResult
FROM Testing
ORDER BY ObjectID
The key in the query is that there is the number 2 in both the Column & Row Index (which is the grouping size) and the number 5 is used in Desired Result and represents the Number of Row cells.
Thanks !