MySQL count all items, aggregate less than as others - mysql

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

Related

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 find out a distribution

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

mysql COUNT results with GROUP BY

I have a table with with 2 unique linked table ids.
I get the results I want with GROUP BY but when I count I only get the number of each group.
When I do:
SELECT COUNT(id) FROM my_table GROUP BY first_linked_table_id, second_linked_table_id
I get as results 1, 2, 3, 1 but I want 4 as a result.
I tried DISTINCT but I think that only works with one column
Your requirement is to get count of number of groups. So we need two operations-
Group(inner query)
Count(outer query)
Following query will do precisely that:
SELECT COUNT(*)
FROM
(
SELECT COUNT(id)
FROM my_table
GROUP BY first_linked_table_id,
second_linked_table_id
) t
If you want to count the rows, I think you're going to need a subquery. Something like this:
SELECT COUNT(*) FROM (
SELECT COUNT(id) FROM my_table GROUP BY first_linked_table_id, second_linked_table_id
);

SQL sum and count command. How do I group my table content to output two groups (rows) of results?

This is my SQL table:
So this is my sql query:
SELECT sponsor_name, sub_name, sum(amount), count(id)
FROM sponsor
and the output is like this:
I want the output to show both Adidas and Dell Computers with their own sum and count. So it will show two rows like:
sponsor_name
sub_name
sum(amount)
count(id)
Adidas
Food Bank Kelowna
9000
4
Dell Computer
Food Bank Kelowna
1500
3
You should group by the columns you don't want to aggregate. In this case, sponsor_name and sub_name:
SELECT sponsor_name, sub_name, SUM(amount), COUNT(id)
FROM sponsor
GROUP BY sponsor_name, sub_name
COUNT and SUM (among others) are Aggregated Functions, that means they return a single value grouped somehow (by a column or a set of columns). You should do this by specifying a GROUP BY clause in your query.
So, try this:
SELECT sponsor_name, sub_name, sum(amount), count(1)
FROM SPONSOR
GROUP BY sponsor_name, sub_name;

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;