Assigning numbers to mysql rows depending on their other variables - javascript

So I have 2 tables in MYSQL which look like this:
person(ID, name, division, subdivision, number)
label(ID, divsion, subdivision, ordernum)
person table
Persons are inputed via form, where name, division and subdivision are submitted. The ID column is autoincremented, and I'll get to the number column shortly.
label table
The labels are already inputed and can't be modified. There are different types of groups, and inside those divisions, 5 subgdivisions for each (to make it short, lets say car 1/2/3/4/5, plane 1/2/3/4/5, and so on).
Each of these rows have an order number assigned, starting from one.
My objective is to, once all persons are input, order them by the ordernum comparing their group and subgroup data (they can of course be repeated, just one beneath the other) and after this, assign them a number (which is where the number column comes in).
Once the persons are in order by ordernum, I want the numbers to be assigned like this:
The number of persons have to be a multiple of 5. Lets say for the sake of this example, there are 20 persons. I would want the numbers to be assigned like this:
1 2 3 4 4 3 2 1 1 2 3 4 4 3 2 1 1 2 3 4
Before I was doing this all with javascript, but I encountered too many problems and the number wasn't recorded in the DB, so I wanted to ask if something like this was possible via PHP.
Thanks in advance!

Related

Generating uniformly distributed random numbers in JS

In my application I've got a list of words that are being shown to the user one by one. The exact number may differ, but it can get as low as about 10. It's ok if the words repeat. To select a word I currently use Math.random
const select = (arr) => arr[Math.floor(Math.random() * arr.length)]
The problem is that Math.random generates, well, random numbers, that may sometimes form clusters, so the user may see the same words several times in a row. I have a simple check that new word is different than the previous one, but I want to improve it. The question is, how can one make random numbers from that range (0 - arr.length-1) more spread, like if it was a human who would choose random words (i.e. they would repeat rarer). Or, for this particular case, how to make the user see as many different words in given time as possible?

How to make permutations satisfying conditions using google apps script?

I'm trying to get permutations of a column from one sheet automatically updated in another sheet (of the same spreadsheet) - while satisfying some conditions.
Eg. This is a dummy table with dummy values for example to illustrate my problem/goal.
Sheet 1
a 2 3
b 4 8
c 3 5
d 11 7
e 7 15
I want to have combinations of column1 such that the sum of values of their column2 is say 20 +/- 5 and column3 is 15 +/- 3. In order to achieve this, the combinations can include multiples of one element too - like 1/2 a or 3 b.
So, for example, one set of combinations in Sheet 2 would be
column1 = a + d + 1/2 e
column2 = 16.5
column3 = 17.5
I've only ever done basic coding so far and I really have no idea how to go about this, any help would be much appreciated :(

Auto-increment dates in two cells if date has passed

Overview:
I'm not a programmer but I managed to get some serious coding into a Gsheets to track my teams project, so we have multiple-variable dropdown menus and integration with google calendar to track projects development and all that.
Why I'm at stackoverflow:
I kind of lack the knowledge to start the code from the scratch, I usually find spare parts of code through forums on the internet and clue them together and it usually works surprisingly well, but this time I couldn't find much informtation.
What I need:
I have 5 cells, and we can put as below,
Date start - Date end - date code* - number** - Priority***
*script to add the date range to gcalendar
** & *** The number is an array that's based on the word written on the priority cell, for example: If priority is written Weekly them
the number colunm will show 7 on the cell to the left and them it
goes. (monthly = 30 and blablabla...)
So I'd like to know if someone could give a hand with a script that would work (at least in my head) as following:
If I set the priority to weekly, it will show 7 on the number colunm and them, every time the "Date end" has passed, it will automatically add 7 days to the "Date start" and "Date end" again.
That way I could keep the projects on a loop where I'll be able to track them constatly.
Thanks in advance for any insights provided,
ps: I've seen some posts about this on sql, but I have no idea also on how to take advantage of the proposals that were presented there.
Edit:
Spreadsheet picture
eDIT2:
Spreadsheet with a increment colunm
Pertinent to the data set and description, you probably do not need any VBA as the increment could be achieved by adding +1 to the reference pointing to previous cell. For example, assuming cell A1 is formatted as Date, enter in cell B1: =A1+1 , then in cell C1: =B1+1 and so on. The result should be as shown below
A B C
9/1/2017 9/2/2017 9/3/2017
It could be further extended with simple logic allowing do display incremented value only if the previous cell is not empty, like : =IF(A1,A1+1,"")
In your case, it could be cell F1 containing =IF(E1,E1+1,"").
FYI, the underlying value of Date is just an Integer value (Time is represented as decimal part), so the arithmetic operations could be applied.
More generic solution would be based on the Excel DATE() Worksheet formula as shown in sample shown below (adding 1 mo. to the date entered in cell A1):
=DATE(YEAR(A1), MONTH(A1)+1, DAY(A1))
In order to implement additional logic, you may consider using Excel Worksheet IF() statement like for example, cell B1 containing:
=A1+IF(C1="week",7,1)
A B C
9/1/2017 9/8/2017 week
so based on the IF() condition it will add either 7 days if C1 contains the word "week" or 1 day otherwise. It could be further extended with nested IF().
Hope this will help.

How to Highlight Specific Column Values based on a CSS Class and Binary Digit On/Off Positions

Using Oracle APEX v5.1.2.
Unsure how to tackle the following but I have a table called, flag_defs with the following example data:
ID NAME
------- ------
1 A
2 B
3 C
4 D
5 E
6 F
Based on the above table, I need to display all these names with a report region but not on separate lines but in the following fashion:
A B C D
E F
where I'm only showing 4 names across.
Now based on the following binary convention, say the following:
010010
where the first 0 lines up to ID = 1 and the last 0 in this sequence lines up to ID = 6
Based on this binary sequence, which will be stored within a database field in another table, I need to apply a class called "flag-red" that I will define as color:red;font-weight:bold; to the names that have the ID position set to "1".
So in the above example binary sequence, both "B" and "E" only would receive the class of "flag-red" and would be red/bold within the report region. The others would not.
The same goes with removing the class if the digit "1" is reset back to "0" for that ID.
I would need to iterate through each digit in this field to set the correct class in my report.
I'm assuming I would firstly create a report and assign a span class to each name but unsure if this is the correct approach.
Furthermore, would JavaScript be a better option or stick to SQL
Would appreciate some assistance on how to tackle the above.
I would suggest you to make a report in the following way:
Lets say you have a query which returns some rows with rownum column. Adding a pivot to this query we can turn it into the following:
select *
from (select r, mod(rownum, 6) group_no, floor((rownum - 1)/6) row_id
from (select rownum r
from dual connect by level <= 30) t)
pivot(max(r) for group_no in (1, 2, 3, 4, 5, 0))
order by 1
Now we can join this query with the table which stores binary mask to show. Lets say initial query has columns C1, C2, ... C6with data, and joined table with mask data has columns M1, M2, ... M6. (If it is one column with a mask, we can produce 6 columns using calculating expressions) You can calculate a name of a CSS class in these columns, something like
select ...
case when <expression1> then 'flag-red' else 'flad-green' end M1,
...
Next step is to go to report properties, choose column C1 in the column list, go to Column formatting section, put #M1# in the CSS Classes field:
Also, mark M1, M2, ... M6 columns as hidden.

Select random combinations of list elements such that no list element is present in more than one selection (SQL)

I'm running a bit out of ideas how to realize a small project.
What I have:
- a list of users including their ID and name
What I want to achieve:
- I want to combine each user on this list with another user such that no user is assigned to more than one user and no user is assigned to herself.
- The combination has to be random and has to take past combinations into account
My idea so far:
- I have this information:
User (A,B,C,D) (the actual number of users ranges between 50 and 400)
Possible combinations: (A-B,A-C,A-D,B-C,B-D,C-D)
Random draw(1): (A-B, C-D)
Random draw(2): (A-D, B-C)
Random draw(3): (A-C, B-D)
I was able to get all possible combinations using a join of the user table with itself.
I guess I can take previous draws into account by storing the draws in a separate table and limit the possible combinations to those that are not in this special table.
What I can't do:
- I don't know how to randomly draw from the list of possible combinations such that every user is part of only one combination per draw (e.g. A-B,A-D in the same draw is not allowed)
- I try to use sql or a bit php for this (maybe javascript)
Thanks for any help.
An easy solution:
Create a temporary table with a row for each pairing. Loop over the list of users skipping a random number of empty rows from 1 to the number of empty rows -- insert user.
Easy solution number 2:
Given N users. Assign each user a unique random number from 1 to N (remove randomly from the set of all numbers from 1 to N). Pair each user with from 1-N/2 with user from N/2+1 to N.
The solution is the problem "Bergr (s) table", see Wikipedia: http://en.wikipedia.org/wiki/Round-robin_tournament.
The latest (best) solution is from Professor FronĨeka (Dalibor Froncek, a professor at the University of Minnesota in the US).
For custom solutions, look in the table of solutions n ^ 2.

Categories

Resources