I have to get total number of specimens in the database.
Table1:
There are 5 specimens as shown in the figure: user 1 has 2, user 2 has 2 and user 3 has 1 number of specimens (users are asked to type a word a number of times and each word is called here a specimen,e.g., cat, bird etc).
I tried following query:
Select count(distinct userId) from Table1 group by userId
It shows user ID wise total specimens. But what I need is total number of specimens inclusive of all users,i.e, 5.
You can use select distinct with multiple columns:
How do I (or can I) SELECT DISTINCT on multiple columns?
Then you can apply count(*) to the result:
select count(*) from (select distinct userId, specimen from Table1);
Note that it works not with MySQL only but with other dialects as well (including SQLite).
Related
I want to return an associative array based on a mySQL query such that the query results in 3 columns which are a count of a single set of data in 3 different ways. My best solution is a bit unweildly and I'm hoping there's a better way as there are some irrelevant complexities not shown below. Specifically I have a user table with a user number, their gender, and a place - the place being the dynamic variable which needs to be bound into the query later (in this instance I'm looking for place = 1). The basic table looks like this:
user gender place
1 m 1
2 m 2
3 f 1
4 m 1
5 f 2
I'd like to return 3 columns which are total, total male, total female at place 1.
My first attempt returns 3 rows with the right values, but as they are rows I can't access them cleanly using an associative array:
SELECT COUNT(DISTINCT user) as total FROM users WHERE place=1
UNION ALL
SELECT COUNT(DISTINCT id_customer) as male FROM users
WHERE gender = 'm' AND place=1
UNION ALL
SELECT COUNT(DISTINCT id_customer) as female FROM users
WHERE gender = 'f' AND place=1
My second attempt gives me the correct result but seems a bit verbose as I'll have to bind the place variable 3 times - is there a better way to do this?
SELECT total, male, female FROM
(SELECT COUNT(DISTINCT user) as total FROM users
WHERE place=1
) as total
INNER JOIN
(SELECT COUNT(DISTINCT user) as male FROM users
WHERE place=1 AND gender='m') as male
INNER JOIN
(SELECT COUNT(DISTINCT user) as female FROM users
WHERE place=1 AND gender='f') as female
Do you need the DISTINCT part? or is "user" field unique (primary key or otherwise)?
I prepared both versions in http://sqlfiddle.com/#!9/928fa/7
If user is unique, then this should be enough:
SELECT count(1), sum(gender='m'), sum(gender='f')
FROM users
WHERE place=1;
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
I have a MySql table with a non-Unique Name Field, I'd like to query the following;
For each Name Value ("John") count how many records there are (32 records with name John), I'll end up writing this to each record.
Average the counts for each name group (e.g 32 records with John and 42 records with Sally, Average for table 37 records)
Count counts meaning e.g 6 10 Record groups, 10 20-record groups, etc
Can I do this with one or more queries?
Average for Table Groups http://sqlfiddle.com/#!2/f5af3/5:
SELECT
AVG(ct)
FROM (
SELECT
COUNT(name) as ct
FROM
names
GROUP BY
name) as temp1;
Count per count http://sqlfiddle.com/#!2/b0628/1:
SELECT
COUNT(ct) AS "Count_per_count",
ct as "Actual Count"
FROM (
SELECT
COUNT(name) as ct
FROM
names
GROUP BY
name) as temp1
GROUP BY ct;
select name, count(*) as num from table group by name;
select ((select count(*) from table) / (select count(distinct name) from table));
select count(*) as num, type from
(select count(*) as type from table group by name) t
group by type
I have a table with figures like this
Report used UserID
1 2
1 2
1 2
2 2
In this case I'm looking to count the 1's in the 'Report used' column, which would give me the value 3. I might find a few of these in this column for different users, so I'd want to count how many times I found 3 1's.
I've tried using SELECT COUNT to count specific numbers but I'm not sure how to count this count, if you follow me.
Try this:
SELECT userid, COUNT(reportused) onescount
FROM tablename
WHERE reportused = 1
GROUP BY userid
Also check this:
SELECT COUNT(userid)
FROM (SELECT userid, COUNT(reportused) onescount
FROM tablename
WHERE reportused = 1
GROUP BY userid) a
WHERE onescount = 3
If I've got it right:
select Report_used,RU_count,count(*)
from
(select Report_used, UserID, count(*) RU_Count
from t
group by Report_used, UserID) t1
group by Report_used,RU_count;
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;