SQL IFELSE Statement To create Sum Variable - sql

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

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.

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]}

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 )

calculate the total value for each group using Calculated Column in Spotfire

I have a problem about the sum calculation for the rows using calculated column in Spotfire.
For example, the raw data is as below, the raw table is order by id, for each type, the sequence is 2,3,0.
id type value state
1 1 12 2
2 1 7 3
3 1 10 0
4 2 11 2
5 2 6 3
6 3 9 0
7 3 7 2
8 3 5 3
9 2 9 0
10 1 7 2
11 1 3 3
12 1 2 0
for type of each cycle of (2,3,0), I want to sum the value, then the result could be:
id type value state cycle time
1 1 12 2
2 1 7 3
3 1 10 0 29
4 2 11 2
5 2 6 3
6 3 7 2
7 3 5 3
8 3 9 0 21
9 2 9 0 26
10 2 7 2
11 2 3 3
12 2 2 0 12
note: only the row which its state is 0 will have the sum value , i think it will be easier to see the rules, when we order the type :
id type value state cycle time
1 1 12 2
2 1 7 3
3 1 10 0 29
4 2 11 2
5 2 6 3
9 2 9 0 26
10 2 7 2
11 2 3 3
12 2 2 0 12
6 3 7 2
7 3 5 3
8 3 9 0 21
thanks for your time and help!
Here is a solution for you.
Insert a Calculated Column RowId() and name it RowId
Insert a Calculated Column If(Mod([RowId],3)=0,[RowId] / 3,Ceiling([RowId] / 3)) and name it Groups
Insert a Calculated Column Sum([value]) OVER ([Groups]) and name it Running Sum
Insert a Calculated Column If([state] = 0,[RunningSum]) and name it OnlyState=0
The only thing to really explain here is #2. With the data sorted as you listed in your example, the last row for each group, based on the RowId, should be divisible by 3. We have to do it this way since your type field can have multiple groups for any given type. RowId 3, 6, 9, 12 etc will all have a Modulus of 0 since they are divisible by 3. This marks the last row in each set. If it is the last row, we just set it to RowId / 3. This gives us groups 1,2,3,4 etc... For the rows which aren't divisible by 3, we round them up to the nearest whole number of the divisor... which will be the last row in the set.
The last calculated column is the only way I know how to get ONLY the values you care about. If you use the If [state] = 0 logic anywhere else, you negate all other rows.

CPlex coding logic

The professor in charge of an industrial engineering design course is faced with the problem of assigning 28 students to 8 projects. Each student must be assigned to one project and each project group must have 3 or 4 students. The students have been asked to rank the projects, with 1 being the best ranking and higher numbers representing lower rankings.
a) Formulate an OPL model for this problem.
b) Solve the assignment problem for the following table of assignments:
A ED EZ G H1 H2 RB SC
Allen 1 3 4 7 7 5 2 6
Black 6 4 2 5 5 7 1 3
Chung 6 2 3 1 1 7 5 4
Clark 7 6 1 2 2 3 5 4
Conners 7 6 1 3 3 4 5 2
Cumming 6 7 4 2 2 3 5 1
Demming 2 5 4 6 6 1 3 7
Eng 4 7 2 1 1 6 3 5
Farmer 7 6 5 2 2 1 3 4
Forest 6 7 2 5 5 1 3 4
Goodman 7 6 2 4 4 5 1 3
Harris 4 7 5 3 3 1 2 6
Holmes 6 7 4 2 2 3 5 1
Johnson 2 2 4 6 6 5 3 1
Knorr 7 4 1 2 2 5 6 3
Manheim 4 7 2 1 1 3 6 5
Morris 7 5 4 6 6 3 1 2
Nathan 4 7 5 6 6 3 1 2
Neuman 7 5 4 6 6 3 1 2
Patrick 1 7 5 4 4 2 3 6
Rollins 6 2 3 1 1 7 5 4
Schuman 4 7 3 5 5 1 2 6
Silver 4 7 3 1 1 2 5 6
Stein 6 4 2 5 5 7 1 3
Stock 5 2 1 6 6 7 4 3
Truman 6 3 2 7 7 5 1 4
Wolman 6 7 4 2 2 3 5 1
Young 1 3 4 7 7 6 2 5
How many students are assigned their second or third choice?
c) Some of the projects are harder than others to reach without a car. Thus, it is desirable that at least a certain number of students assigned to each project must have a car; the numbers vary by project as follows:
A ED EZ G H1 H2 RB SC
1 0 0 2 2 2 1 1
The students who have cars are Chung, Demming, Eng, Holmes, Manheim, Morris, Nathan, Patrick, Rollins and Young.
Modify the model to add this car constraint and solve the problem again. How many more students than before must be assigned second or third choices?
I coded the file for a) & b) but i am getting stuck at c).
can anyone help pls with the logic? even ampl wil suffice
Let C_i be the indicator matrix (input): C_i = 1 if student i has a car and 0 otherwise. I'll assume you have the following decision variables:
x_ij = 1 if student i is assigned to project j; 0 otherwise
then c) constraint can me modeled as follows
sum_i C_i * x_ij >= b_j for all j
where b_j is
j A ED EZ G H1 H2 RB SC
b_j 1 0 0 2 2 2 1 1