getting Sum Floor and Group by to work - mysql

Ok all i have a question about sum and group by what i have currently is a error and i don't know how to fix basically the error is in regards to this line of code
SELECT PlayerName
,floor(SUM(PlayerScore / 500)) + floor(SUM(PPlayerScore / 1000)) + floor(SUM(S4 / 5)) * 2 + floor(Sum(P7 / 3)) * 2 + SUM(prereg) + SUM(st) + SUM(hb) AS BB
,floor(SUM(PlayerScore / 500)) + floor(SUM(PPlayerScore / 1000)) + floor(SUM(S4 / 5)) * 2 + floor(Sum(P7 / 3)) * 2 + SUM(PlayerBallots) + SUM(prereg) + SUM(st) + SUM(hb) AS TB
,SUM(PlayerBallots) AS PB
FROM player
the same issue is throughout the line and it is this
from this floor sum command
if 4 players have scores of 1100 1300 1800 and 1000 the sum of that will show 5 when the reality is it should only show 4 i tried adding in a group by operator it will only show the total for 1 player
is it possible to do what I'm trying or not
thank you in advance
To help a little i will shorten down the code and explain what i need it to do with an example
floor(SUM(PPlayerScore /1000))
so for each 1000 points a player scores he gets 1 ballot
so if
jane has 1100 gets 1 ballot floor 1.1
joe gets 1300 gets 1 ballot floor of 1.3
jerry gets 1800 1 ballot floor of 1.8
jane gets 1000 gets 1 ballot floor of 1
the issue is with current code i get 5 when in reality there is only 4 ballots

Related

Any number multiplies by zero equals to zero but why the factorial number of zero equals 1?

Mathematically speaking, zero is the absobing element of the multiplication. I would like to know why the factorial number of zero equals 1? Is this general accepted rule?
Can someone reason about this factorial concept based on this factorial formula?
n!=n×(n−1)×(n−2)×………×1
0! = 0 * 0 = 1
1! = 1 * 1 = 1
2! = 2 * 1 = 2
3! = 3 * 2 * 1 = 6
4! = 4 * 3 * 2 * 1 = 24
5! = 5 * 4 * 3 * 2 * 1 = 120
6! = 6 * 5 * 4 * 3 * 2 * 1 = 720
Here is a description with multiple definitions. 0! is always 1, in mathematics as well as programming. It has to do with data sets.

Compare the digits of two integers in each decimal position

I am not sure I am describing the problem using the correct terms, my math English is not that good.
What I need to do is check if they match for each digit of two integers based on the position of the digit: ones, tens, .. etc
For example check the following table of different numbers and the wanted comparison result:
number1 | number2 | desired result
-----------------------------------
100 | 101 | 001
443 | 143 | 300
7001 | 8000 | 1001
6001 | 8000 | 2001
19 | 09 | 10
Basically I need the absolute value of subtraction for each digit alone. So for the first example:
1 0 0
1 0 1 -
--------
0 0 1
And second:
4 4 3
1 4 3 -
-------
3 0 0
And third:
7 0 0 1
8 0 0 0 -
---------
1 0 0 1
This needs to be done in mysql. Any ideas please?
This should do the job if your numbers are below 10000.
If they exceed, simply modify the query ;)
SELECT number1,
number2,
REVERSE(CONCAT(ABS(SUBSTRING(REVERSE(number1), 1, 1) - SUBSTRING(REVERSE(number2), 1, 1)),
IF(CHAR_LENGTH(number1) > 1, ABS(SUBSTRING(REVERSE(number1), 2, 1) - SUBSTRING(REVERSE(number2), 2, 1)), ''),
IF(CHAR_LENGTH(number1) > 2, ABS(SUBSTRING(REVERSE(number1), 3, 1) - SUBSTRING(REVERSE(number2), 3, 1)), ''),
IF(CHAR_LENGTH(number1) > 3, ABS(SUBSTRING(REVERSE(number1), 4, 1) - SUBSTRING(REVERSE(number2), 4, 1)), ''))) as `desired result`
FROM numbers
for 3 digit numbers:
SELECT number1,
number2,
CONCAT(
ABS(SUBSTRING(number1, 1, 1) - SUBSTRING(number2, 1,1)),
ABS(SUBSTRING(number1, 2, 1) - SUBSTRING(number2, 2,1)),
ABS(SUBSTRING(number1, 3, 1) - SUBSTRING(number2, 3,1))
)
FROM numbers
actually you don't have reverse the string at all. this comes from a more mathematical approach I tried before ;)
if you want to do it with integers only, it can be done this way (for 5 digits as an example):
select abs(number1/10000 - number2/10000) * 10000 +
abs(number1/1000 % 10 - number2/100 % 10) * 1000 +
abs(number1/100 % 10 - number2/100 % 10) * 100 +
abs(number1/10 % 10 - number2/10 % 10) * 10 +
abs(number1 % 10 - number2 % 10)

Converting decimal floating number to binary

Can anyone please help me convert number (for example) 143,625 to binary? I've been searching through net for quite a long time but didn't find anything with good explanation.
Thanks in advance!
The integer part can be done by dividing by 2 repeatedly and keeping track of the remainder:
143 / 2 = 71 remainder 1
71 / 2 = 35 remainder 1
35 / 2 = 17 remainder 1
17 / 2 = 8 remainder 1
8 / 2 = 4 remainder 0
4 / 2 = 2 remainder 0
2 / 2 = 1 remainder 0
1 / 2 = 0 remainder 1
So the integer part is 10001111
For the fractional part, multiply it by 2 repeatedly and look at the integer part of the result:
.625 x 2 = 1.25 - we need the integer part, which is 1 - so far we have 0.1, we then disregard the 1 and look at 0.25
0.25 x 2 = 0.5 - so far we have 0.10, we look at 0.5
0.5 x 2 = 1.0 - we have 0.101, no decimal part so we're good.
The whole number is the integer part + the decimal part, so 10001111.101

MySQL weighted average in a single query

I have a MySQL table which looks like this:
id load_transit load_standby hours_transit hours_standby
1 40 20 8 4
2 30 15 10 10
3 50 10 3 9
I need to do the following calculations:
(intermediate calculations)
hours_transit_total = 8+10+3 = 21
hours_standby_total = 4+10+9 = 23
(desired result)
load_transit_weighted_mean = 40*(8/21) + 30*(10/21) + 50*(3/21) = 36.667
load_standby_weighted_mean = 20*(4/23) + 15*(10/23) + 10*(9/23) = 13.913
Is it possible to do this in a single query? What would the best design be?
Note that
40*(8/21) + 30*(10/21) + 50*(3/21) =
(40*8)/21 + (30*10)/21 + (50*3)/21 =
(40*8 + 30*10 + 50*3)/21
and
20*(4/23) + 15*(10/23) + 10*(9/23) =
(20*4)/23 + (15*10)/23 + (10*9)/23 =
(20*4 + 15*10 + 10*9)/23
Which allows you to get the results you want using
SELECT sum(hours_transit * load_transit) / sum(hours_transit),
sum(hours_standby * load_standby) / sum(hours_standby)
FROM your_table
I just had this same question and built this little query I think makes it clear how to find the weighted average in a single query:
select sum(balance), sum(rate * balance / 5200) as weighted_rate, -- what I want
-- what you cannot do: sum(rate * balance / sum(balance))
sum(balance * rate) / sum(balance) as weighted_rate_legit -- ah thank you transitive math properties
from (
select '4600' as balance, '2.05' as rate from dual
union all
select '600' as balance, '2.30' as rate from dual
) an_alias;

Binary calculation

I need help to calculate Processor Affinity value
0 (0000) Not allowed (that would mean use no processors)
1 (0001) Use processor 1
2 (0010) Use processor 2
3 (0011) Use both processors 1 and 2
4 (0100) Use processor 3
5 (0101) Use both processors 1 and 3
6 (0110) Use both processors 2 and 3
7 (0111) Use processors 1,2 and 3
8 (1000) Use processor 4
With 1, 2, 3 and result is 7. I wonder what formula is?
It seems to be a simple 4-digit binary number.
A 1 at the right-most position means 1, a 1 at the second position from the right means 2, at the 3rd it means 4 and at the 4th position from right (i.e. the first digit from left) it means 8. The total value is simply the sum of all those positions.
The basic idea (in pseudo-code, because we can't format formulas correctly here is):
totalValue
for every digit at position i (counted from the right, starting with 0)
totalValue = totalValue + 2^i*(digit at position i)
For example 3 (0011) the value is 0x2^3 + 0x2^2 + 1*2^1 + 1*2^0 = 0 + 0 + 2 + 1 = 3
For example 4 (0100) the value is 0x2^3 + 1x2^2 + 0*2^1 + 0*2^0 = 0 + 4 + 0 + 0 = 4
Processor_Affinity := Use_processor_1 + Use_processor_2 + Use_processor_3
So 1010; interpret it as:
0 at 1st position as OFF
1 at 2nd position as ON
0 at 3rd position as OFF
1 at 4th position as ON

Categories