SQL Query - how to have half-split column within the same row - mysql

I want to ask if it is possible to do a query and have a result like this? The more difficult part is to have a half-split column within the same row.
M stands for Male and F stands for Female for those who are wondering.
Teacher Class Level Name 7 8 9 10 11
M F M F M F M F M F
Mr A Class 1 Ant 0 0 3 9 0 1 0 0 0 0
Ms B Class 1 Bumble Bee 0 0 11 5 1 0 0 0 0 0
Mrs C Class 2 Cat 0 0 0 1 7 1 0 0 0 0
Mdm D Class 3 Dog 0 0 0 0 0 0 7 0 1 0
I have attached the same table but in excel so it's easier to see. It will be good if this can be done in pure sql query.
Thank you in advance to those who help in any way.

The closest result you could get solely using SQL would be to have a table returned with the following column headers:
Teacher Name, Class Level, Name, 7_M, 7_F,8_M, 8_F,9_M, 9_F,10_M, 10_F,11_M, 11_F.
Now, to display those X_M and X_F column as "half-split", you would have to do it outside SQL (EDIT: For the reasons Radim mentioned in his comment).

Related

How to create a circuit that outputs only 1, regardless of inputs

So this is the truth table:
In_1 In_2 In_3 Out
0 0 0 1
0 0 1 1
0 1 0 1
0 1 1 1
1 0 0 1
1 0 1 1
1 1 0 1
1 1 1 1
How do I create a circuit that matches this truth table?
Here is what I have tried, but this did not work:
Please help would be appreciated.
You just need to connect the output to a constant logic 1, which is usually just the power supply voltage. This is a trivial logic function and requires no gates to implement.
The solution is to apply Kmap. We will be ending up with an equation that will be similar to
but this is not the reduced form. If we try to reduce this equation we will end up with 1.
Check this tool for better understanding.
https://www.boolean-algebra.com/kmap/

How do I create a circuit based on this truth table?

So this is the truth table
In_1 In_2 In_3 Out
0 0 0 0
0 0 1 1
0 1 0 1
0 1 1 1
1 0 0 1
1 0 1 1
1 1 0 1
1 1 1 0
I would like to create a circuit based on this truth table.
This is what I have tried, but failed
A Karnaugh map as suggested by paddy, will give you a set of minterms which fulfil the expression. That is the classical way to tackle such problems.
By inspection of the truth-table you can convince yourself, that the output is true whenever In_1 is unequal In_3 or In_1 is unequal In_2:
f = (In_1 xor In_2) or (In_1 xor In_3)

Querying SQL randomly given a specific condition

Lets say I have this news collection database, where cat1, cat2, etc are the category:
title cat1 cat2 cat3 cat4 catcount new1 new2 new3 new4
title1 1 2 0 0 2 0 0 0 0
title2 0 2 0 4 2 0 0 0 0
title3 1 2 3 0 3 0 0 0 0
Now I want to randomly select title with any of its category that is non zero, for example title='title3' cat2='2' to do some processing that determines if this category is correct.
Then return the value to the corresponding new2 (since we processed cat2), decrement the catcount, and set the cat2 to zero since it has been processed.
The goal is to process all combination of titles-categories and write the result to new, except I read it randomly.
How do I query this in MySQL?
EDIT:
The end result should be like this:
title cat1 cat2 cat3 cat4 catcount new1 new2 new3 new4
title1 0 0 0 0 0 1 0 0 0
title2 0 0 0 0 0 0 2 0 4
title3 0 0 0 0 0 0 2 0 0
You can generate a pseudo-random number with the RAND() function of MySQL.
However, note that the entropy of this function is very low, so you should consider getting all the entries of your table, then do the processing that you explained separately, in your code.
http://dev.mysql.com/doc/refman/5.7/en/mathematical-functions.html
RAND() is not meant to be a perfect random generator. It is a fast way
to generate random numbers on demand that is portable between
platforms for the same MySQL version.

Boolean Logic and Truth Tables Explanation

I'm needing to create a truth table, and I really need to find a resource to explain how it works. I'll give an example of a problem.
I have to create a truth table based on this: A*(B+AB)=AB
So the truth table looks something like:
0 0
0 1
1 0
1 1 for A*(B+AB)=AB
How do I even begin to solve this? Are there any good resources that give a good explanation on what to do?
Ok So I then did one more complicated that involves a NOT.
! indicates not
!(A*!B+!AB) = AB+!(A+B)
So I did C = A*!B D=!A*B then !(C+D) for the left side. My final answer for that side is
0 0 1
0 1 0
1 0 0
0 0 1
So the right side is this
C = A * B D = A + B then C + !D
so that looked like this
0 0 1
0 1 0
0 1 0
1 1 1
I think I'm getting it? :)
Edit: I put in some extra explanation given your comment (which is now deleted).
A and B are two boolean variables. For example, in a program, A might be firstTestOK and B might be secondTestOK. Each of A and B can be either true (1) or false (0).
A+B means A or B which is true if either A or B is true. A*B means A and B is is true only if both A and B are true.
All of the combinations for A, B are:
A is false and B is false
A is false and B is true
A is true and B is false
A is true and B is true
This can be written more compactly as a truth table as follows:
A B
0 0
0 1
1 0
1 1
What you've been asked to do is show A*(B+AB) is the same as AB. So, for each combination, we work out the left-hand-side, which is A*(B+AB) and the right-hand-side, which is AB:
A B C=A*B D=B+C A*D = A*B
0 0 0 0 0 0
0 1 0 1 0 0
1 0 0 0 0 0
1 1 1 1 1 1
so, looking at all of the combinations in the last two columns, we see that the results are the same, so AD=A(B+AB) is AB.
Since the left-hand-side is a little complicated, I did it in steps by breaking it up into pieces, by introducing C and D.

mySQL to retrieve records that repeat a value in order

I have a big table with 300,000 records. This table has a integer value called "velocity" and it`s value is from 0 to 100.
In the firsts records, the value is 0 and I want to remove. I want to remove from the query, the records where the velocity field repeats more than 10 times. For example:
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 2 3 5 10 12 13 15 20 30 20 15 10 8 5 2 1 0 0 0 0 4 5 10 20...
[-------remove this-----------].......................................................................[---------] <- do not remove this
Thanks
The easiest way to do this is with a loop.
You can write a stored procedure that iterates through the records, or you might do it outside of the database. I'd do it like that if this needs to be done once. If this is a continuous process, it's better to make sure that the extra data is just not inserted into the database in the first place.
Anyway, if you insist on doing this in pure SQL, without stored procedures with loops, you can use a query like this:
set #groupnum=0;
select
GroupNum,
count(*) as RecsInGroup
from
(
select
t1.id as Id,
t1.velocity as velocity1,
t2.velocity as velocity2,
if(t1.velocity<>t2.velocity,#groupnum:=#groupnum+1,#groupnum) as GroupNum
from
VelocityTable as t1
join
VelocityTable as t2
on
t1.id=t2.id-1
) as groups
group by
GroupNum
having RecsInGroup>10
What happens here?
Step 1
The inner query just selects all records in your table, but splits the data in sequential groups.
So, using your example, it does this:
velocity : 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 2 3 5 10 12 13 15 20 30 20 15 10 8 5 2 1 0 0 0 0 4 5 10 20
Groupnum : 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 18 18 18 19 20 21 22
It does that by joining the table to itself, by linking subsequent records in the table. Every time the left and right velocity are different, the GroupNum is increased. Otherwise it's left unchanged.
Step 2
The result if the query is wrapped in an outer query, and grouped by GroupNum. Again, using your example it would result in this:
GroupNum,RecsInGroup
0,15 // !!
1,1
2,1
3,1
4,1
5,1
6,1
7,1
8,1
9,1
10,1
11,1
12,1
13,1
14,1
15,1
16,1
17,1
18,4 // !!
19,1
20,1
21,1
By Adding the having RecsInGroup>10 clause, the result becomes this:
GroupNum,RecsInGroup
0,15
Now, with this list of GroupNum's you can delete records.
Step 3
With the query above you have:
A list of all your records, with an added GroupNum column.
The list of GroupNum's that need to be removed.
Deleting the records should be easy at this point.
I'd just rip through the records sequentially, with a variable sized window that expands and contracts to comprehend identical values. Whenever the size is >= 10 when the value changes, delete the rows using the primary keys.
You can put BEGIN TRAN and COMMIT TRAN at the beginning and end of the DELETE statements to make things reasonably efficient.
thank you very much. I'm allmost there, but i tried it with a mySQL View as table source and it's not working (unkown table xxx). I can't use the whole table because it's have more than 19 millions records, I just need the record from a specific day, vehicle plate and city.