Mysql find the range with most elements [closed] - mysql

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I have a simple mysql table with name and age columns. I need to find the age range (say with length 5) which contains the most number of records. Please note that the range can be from anything to anything (like 1 to 5 years or 2 to 6 years). I have created a sqlfiddle for the same at http://sqlfiddle.com/#!2/a65265/1
I have tried using DIV and searched through the forums, but the closest i can get is predefined ranges like age 5-10, 10-15 etc. I need a more generic solution for all possible age ranges.

select 5 * floor((t.age-o.offset)/5) + o.offset as from_age
,5 * (floor((t.age-o.offset)/5) + 1) + o.offset - 1 as to_age
,count(*) as cnt
from test as t
cross join ( select 0 as offset
union all select 1
union all select 2
union all select 3
union all select 4
) as o
group by o.offset
,floor((t.age-o.offset)/5)
order by cnt desc
limit 1
The basic idea -
Each row is being duplicated 5 times, with offset in the range of 0 to 4.
Each offset is causing a different distribution of the elements as described in the following diagrams:
x: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| | | | | | | | | | | | | | | | | | | |
------------- ------------- ------------- -------------
floor((x-0)/5): 0 1 2 3
x: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| | | | | | | | | | | | | | | | | | | |
- ------------- ------------- ------------- -------------
floor((x-1)/5): 0 1 2 3
x: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| | | | | | | | | | | | | | | | | | | |
---- ------------- ------------- ------------- -------------
floor((x-2)/5): 0 1 2 3
x: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| | | | | | | | | | | | | | | | | | | |
------- ------------- ------------- ------------- -------------
floor((x-3)/5): 0 1 2 3
x: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| | | | | | | | | | | | | | | | | | | |
---------- ------------- ------------- ------------- -------------
floor((x-4)/5): 0 1 2 3

Related

Get Percentages of Tables with Group by

I am working on a prediction game for a local league.
A Match is defined by its SPIELTAG (playday) and its MATCH_NR.
A prediction has a Match and a result (ERGA, ERGB)
I have a Table like in this fiddle https://www.db-fiddle.com/f/o4NXPFfzod39LpMaTzqz3r/0
I got the output to count each result per gameday and per match.
| SPIELTAG | MATCH_NR | ERGA | ERGB | AMOUNT |
| -------- | -------- | ---- | ---- | ------ |
| 4 | 1 | 7 | 1 | 1 |
| 4 | 1 | 7 | 2 | 2 |
| 4 | 1 | 7 | 3 | 5 |
| 4 | 1 | 7 | 4 | 1 |
| 4 | 2 | 1 | 7 | 1 |
| 4 | 2 | 2 | 7 | 6 |
| 4 | 2 | 3 | 7 | 3 |
What i am trying to achieve is, next to the amount column something like
| SPIELTAG | MATCH_NR | ERGA | ERGB | AMOUNT | PERC |
| -------- | -------- | ---- | ---- | ------ | ---- |
| 4 | 1 | 7 | 1 | 1 | 11.1%| => 1 / 9
| 4 | 1 | 7 | 2 | 2 | 22.2%| => 2 / 9
| 4 | 1 | 7 | 3 | 5 | 55.5%| => 5 / 9
| 4 | 1 | 7 | 4 | 1 | 11.1%| => 1 / 9
| 4 | 2 | 1 | 7 | 1 | 11.1%| => 1 / 9
| 4 | 2 | 2 | 7 | 5 | 55.5%| => 5 / 9
| 4 | 2 | 3 | 7 | 3 | 33.3%| => 3 / 9
Where PERC is the percentage of picks per playday SPIELTAG. Basically how often is the Result, the tuple of (ERGA, ERGB) predicted for each match SPIELTAG, MATCH_NR.
I found a post where i can get the percentage over all picks but not restricted on the gameday,match tuple.
An Example:
Match 1 (Spieltag 4, Match 1) has 9 Predictions.
1x: 7-1
2x: 7-2
5x: 7-3
1x: 7-4
__
9x -> ALL_COUNTS_PER_MATCH
So PERC should be something like 'AMOUNT' / ALL_COUNTS_PER_MATCH.
I think you need another group by:
SELECT `t`.`SPIELTAG` AS `SPIELTAG`,
`t`.`MATCH_NR` AS `MATCH_NR`,
`t`.`TEAM_A` AS `ERGA`,
`t`.`TEAM_B` AS `ERGB`,
count(0) AS `AMOUNT`,
COUNT(0) / MAX(A.TOTAL_AMOUNT) -- MIN would also work
FROM `TIPPSPIEL_TIPP` `t`
JOIN (
-- Calculate the row count by each different spieltag & match_nr combination
SELECT `t`.`SPIELTAG`,
`t`.`MATCH_NR`,
count(0) AS `TOTAL_AMOUNT`
FROM `TIPPSPIEL_TIPP` `t`
GROUP BY `t`.`SPIELTAG`, `t`.`MATCH_NR`
) A USING (SPIELTAG, MATCH_NR)
GROUP BY `t`.`SPIELTAG`, `t`.`MATCH_NR`, `t`.`TEAM_A`, `t`.`TEAM_B`
;
https://www.db-fiddle.com/f/o4NXPFfzod39LpMaTzqz3r/3
Check this, but will work in Mysql 8, not lower versions
https://www.db-fiddle.com/f/o4NXPFfzod39LpMaTzqz3r/4
SELECT DISTINCT `t`.`SPIELTAG` AS `SPIELTAG`,
`t`.`MATCH_NR` AS `MATCH_NR`,
`t`.`TEAM_A` AS `ERGA`,
`t`.`TEAM_B` AS `ERGB`,
COUNT(0) OVER (PARTITION BY `t`.`SPIELTAG`,
`t`.`MATCH_NR`,
`t`.`TEAM_A`,
`t`.`TEAM_B`) AS AMOUNT,
COUNT(0) OVER (PARTITION BY `t`.`SPIELTAG`,
`t`.`MATCH_NR`,
`t`.`TEAM_A`,
`t`.`TEAM_B`) / COUNT(CONCAT(SPIELTAG, MATCH_NR)) OVER (PARTITION BY `t`.`SPIELTAG`,
`t`.`MATCH_NR`) AS match_count
FROM `TIPPSPIEL_TIPP` `t`

MySQL CTE - is it possible to iterate over range of numbers?

I'm curious if is that possible to do similar query using CTE (the values of "id" may vary, not necessarily in succession)
SELECT
elt((#mxId := if(#mxId + 1 <= 3, #mxId + 1, 1)), 5, 10, 22, 33) val,
id
FROM my_table
INNER JOIN (SELECT #mxId := 0) tmp;
Expected output:
val | id
-----+----
5 | 1
10 | 2
22 | 3
5 | 4
10 | 5
22 | 6
5 | 7
10 | 8
22 | 9
5 | 10
10 | 11
22 | 12
5 | 13
10 | 14
22 | 15
5 | 16
10 | 17
22 | 18
5 | 19
10 | 20
with my_table_numbered as (
select ((row_number() over ())-1)%3 as rn_mod, id from my_table
)
select elt(rn_mod+1, 5, 10, 22, 33) as val, id
from my_table_numbered;
Output:
+------+----+
| val | id |
+------+----+
| 5 | 1 |
| 10 | 2 |
| 22 | 3 |
| 5 | 4 |
| 10 | 6 |
| 22 | 7 |
| 5 | 8 |
| 10 | 9 |
| 22 | 13 |
| 5 | 14 |
| 10 | 15 |
| 22 | 16 |
+------+----+
You should specify an ORDER BY in the CTE to get a meaningful and reliable row numbering, but you didn't have one in the query in your question.
row_number() OVER ()
that's do the magic - Thanks very much

Filtering Column based on values on Another table

I'am tying to get the specific columns whose name starts with some patterns.
table_1
abcA| abcB | abcC | xyD | mnE
1 | 2 | 3 | 4 | 5
6 | 7 | 8 | 9 | 10
11 | 12 | 13 | 14 | 15
From the above table i'am in need of the Output Like
abcA | abcB | abcC
1 | 2 | 3
6 | 7 | 8
11 | 12 | 13
The columns should be selected DYNAMICALLY by filtering like any column name starts with abc should me selected.
I Tried this Query
"select column_name from information_schema.columns
where table_name='table_1' and column_name like 'abc%';"
It gives a another table only with column names
column_name
abcA
abcB
abcC
But I want to get the values of that Column names.
Thanks
This is poor table design, and it is fairly difficult to write code which can select a dynamic column name. Here is the design I would suggest to you:
ID | name | pos
1 | abcA | 1
2 | abcB | 1
3 | abcC | 1
4 | xyD | 1
5 | mnE | 1
6 | abcA | 2
7 | abcB | 2
8 | abcC | 2
9 | xyD | 2
10 | mnE | 2
11 | abcA | 3
12 | abcB | 3
13 | abcC | 3
14 | xyD | 3
15 | mnE | 3
With this design in place, you only need a very simple query:
SELECT pos, GROUP_CONCAT(ID) AS ids
FROM yourTable
WHERE name LIKE 'abc%'
GROUP BY pos;

Numbering group results into unique identifiers

I am trying to create an query that results in unique identifiers (account number).
What I need to achieve is for each unique entry into Col1 to create a another row number / UI.
I have been attempting this with the below query
elect col1,col2 ,(DENSE_RANK() OVER( ORDER BY col1,col2)) as UI from [TABLE]
and this is what i have been getting:
col1 col2 UI
34 1 1
1973 448 2
355 3924 3
18709 8168 4
5201 9211 5
5762 9294 6
3864 10669 7
4914 12568 8
4914 12569 9
42465 921 10
but i need it to look like this:
col1 col2 UI
34 1 1
1973 448 2
355 3924 3
18709 8168 4
5201 9211 5
5762 9294 6
3864 10669 7
4914 12568 8
4914 12569 8
42465 921 9
You need to define a way to - more loosely - rank col2 e.g. using division
select
col1,col2
,DENSE_RANK() OVER( ORDER BY col1,col2/10) as newUI
from mytable
+----+-------+-------+-------+
| | col1 | col2 | newUI |
+----+-------+-------+-------+
| 1 | 34 | 1 | 1 |
| 2 | 355 | 3924 | 2 |
| 3 | 1973 | 448 | 3 |
| 4 | 3864 | 10669 | 4 |
| 5 | 4914 | 12568 | 5 |
| 6 | 4914 | 12569 | 5 |
| 7 | 5201 | 9211 | 6 |
| 8 | 5762 | 9294 | 7 |
| 9 | 18709 | 8168 | 8 |
| 10 | 42465 | 921 | 9 |
+----+-------+-------+-------+

How to compare two column and how to get how get all rows?

I have one table. Here my question is how to find how many rows with same value compare two column.
id true_ans your_ans que_id
------+---------+--------+---------
1 | 2 | 2 | 1
2 | 5 | 4 | 3
3 | 8 | 9 | 4
4 | 14 | 14 | 6
5 | 19 | 19 | 7
6 | 21 | 22 | 9
Here i want find how many rows correct answer. Please reply me.
Do this :
SELECT COUNT(*) CorrectAnswers
FROM YourTable
WHERE
true_ans = your_ans