Mysql find out a distribution - mysql

I'm trying to make a query to find out a distribution.
My table has two columns: id, zone_id
I want the result to show how many ids have been in one zone, two zones, three zones etc. (there are 13 zones in total).
An id can be repeated, being a row for example id = 1 and zone_id = 1 and id = 1 and zone_id = 2.
Thanks in advance.

You can do this with two aggregations, one to compute the number of zones for each id, and a second one to tally the zone counts themselves:
SELECT
zone_cnt, COUNT(*) AS frequency
FROM
(
SELECT id, COUNT(DISTINCT zone_id) AS zone_cnt
FROM yourTable
GROUP BY id
) t
GROUP BY zone_cnt;

Try by using GROUP BY, your select should look something like this:
SELECT COUNT(id) as [Distribution]
FROM your_table
GROUP BY zone_id

Related

count of diferent values with the same id

I have ID_employe in one column (the same ID can be on more than 2 rows) and in other column I have ID_job.
I need MYSQL to find same values in the first column and then check, if there are everytime the same values in the second column.
If there is any difference, I need to give me number of ID_employe what has it different.
So example:
from this example I need SQL to give me result: 2
(because ID_employe 1 and 3 has different ID_job)
Thank you very much!
With EXISTS:
select count(distinct t.ID_employe) counter
from tablename t
where exists (select 1 from tablename where ID_employe = t.ID_employe and ID_job <> t.ID_job)
You can use a having clause and compare the minimum and maximum id_job per id_employee to exhibit those that have at least two jobs. Then you can count in another level of aggregation:
select count(*) cnt
from (
select id_employee
from mytable
group by id_employee
having min(id_job) <> max(id_job)
) t

Sql - Query to get total number of records in table

I have table like this
enter image description here
I need to get the data only whose age > 10, along with that i need to get the total number of records present in the table. ie. in this example it is 4 records. what i need is in single query i need to get the total number of records present in table and columns which i query.
Query will be somewhat like
SELECT ID, NAME, count(TOTAL NUMBER OF RECORDS IN TABLE) as Count from MYTABLE WHERE AGE > 10
Any idea about this ?
You can use a subquery in the FROM clause:
SELECT ID, NAME, c.cnt as Count
FROM MYTABLE CROSS JOIN
(SELECT COUNT(*) as cnt FROM MYTABLE) c
WHERE AGE > 10 ;
Both databases support window functions, but they are not really helpful here, because the count is not filtered in the same way as the outer query. If you do want the filter for both, then in the most recent versions you can do:
SELECT ID, NAME, COUNT(*) OVER () as cnt
FROM MYTABLE
WHERE AGE > 10 ;
You can try below - using scalar subquery
SELECT ID, NAME, age,(select count(*) from mytable WHERE AGE > 10) as Count
from MYTABLE
WHERE AGE > 10

MySQL count all items, aggregate less than as others

I need to fetch data from many mysql 5.6 tables to create a pie chart. As you know, the pie chart is useful if it represents meaningful data. However when you have many non meaningful data points, say less than .. or non important values, the pie chart becomes unclear. I need to count the occurrence of each category and aggregate the not significant counts, less than X, as OTHERS.
At the moment I make a
Select category, count(*) as total from table_name group by category.
It gives me each category and it's counts. How can I get the categories whose totals are over 50 and the ones that are below, get summarized under "Others". Thanks, Jorge.
SELECT IF(total > 50, category, 'Others') AS category, SUM(total) AS total
FROM (SELECT category, COUNT(*) AS total
FROM table_name
GROUP BY category) AS subquery
GROUP BY category
Say you want to summarize all categories with less than 4 entries:
select category, count(*) as total from table_name group by category having count(*) >= 4
union
select 'others', sum(c) as total from (
select category, count(*) c from table_name group by category having count(*) < 4
) tmp

MySQL query for weighted voting - how to calculate with values assigned to different columns

I have a voting application that writes values to a mysql db table. It is a preference/weighted voting system so people choose a first option, second option, and third option. These all go into separate fields in the table. I'm looking for a way to write a query that will assign numerical values to the responses (3 for a first response, 2 for a second, 1 for a first) and then display the value with the summed score. I've been able to do this for total number of votes
select count(name) as votes,name
from (select 1st_option as name from votes
union all
select 2nd_option from votes
union all
select 3rd_option from votes) as tbl
group by name
having count(name) > 0
order by 1 desc;
but haven't quite figured out how to assign values to response in each column and then pull them together. Any help is much appreciated. Thanks!
You could do something like this:
select sum(score) as votes,name
from (select 1st_option as name, 3 as score from votes
union all
select 2nd_option as name, 2 as score from votes
union all
select 3rd_option as name, 1 as score from votes) as tbl
group by name;

Determine total amount of top result returned

I would like to determine two things from a single query:
Most prevalent column in a table
The amount of times such column was located upon querying the table
Example Table:
user_id some_field
1 data
2 data
1 data
The above would return user_id # 1 as being the most prevalent in the table, and it would return (2) for the total amount of times that it was located in the table.
I have done my research and I came across two types of queries.
GROUP BY user_id ORDER BY COUNT(*) DESC
SUM
The problem is that I can't figure out how to use these two queries in conjunction with one another. For example, consider the following query which successfully returns the most prevalent column.
$top_user = "SELECT user_id FROM table_name GROUP BY user_id ORDER BY COUNT(*) DESC";
The above query returns "1" based on the example table shown above. Now, I would like to be able to return "2" for the total amount of times the user_id (1) was found in the table.
Is this by any chance possible?
Thanks,
Evan
You can include count(*) in the SELECT list:
SELECT user_id, count(*) as totaltimes from table_name
GROUP BY user_id ORDER BY count(*) DESC;
If you want only the first one:
SELECT user_id, count(*) as totaltimes from table_name
GROUP BY user_id ORDER BY count(*) DESC LIMIT 1;