I have a table where I am having duplicates value also.
From that table, I want to count duplicate value as 1.
I am using below query to find count
SELECT id, team, count(*) as votes FROM vot GROUP BY team ORDER BY votes DESC;
From this query, I get the duplicates count also.
I hope I made my query clear.
I am very new to MySQL.
What I got from your question is:
Instead of --
id team votes
1 A 2
3 C 2
2 B 1
you want --
id team votes
1 A 1
2 B 1
3 C 1
For this result use the following query:
SELECT id, team, count(distinct team) as votes FROM vot GROUP BY team,id ORDER BY votes DESC;
Related
I have a database with one table as shown below. Here I'm trying to write a query to display the names of medication manufactured by the company that manufactures the most number of medications.
By looking at the table we could say the medication names which belongs to the company id 1 and 2 - because those company manufactures the most medication according to this table, but I'm not sure how to write a query for selecting the same i said before.
ID | COMPANY_ID | MEDICATION_NAME
1 1 ASPIRIN
2 1 GLUCERNA
3 2 SIBUTRAMINE
4 1 IBUPROFEN
5 2 VENOFER
6 2 AVONEN
7 4 ACETAMINOPHEN
8 3 ACETAMINO
9 3 GLIPIZIDE
Please share your suggestions. Thanks!
Several ways to do this. Here's one which first uses a subquery to get the maximum count, then another subquery to get the companies with that count, and finally the outer query to return the results:
select *
from yourtable
where companyid in (
select companyid
from yourtable
group by companyid
having count(1) = (
select count(1) cnt
from yourtable
group by companyid
order by 1 desc
limit 1
)
)
SQL Fiddle Demo
This Query might work. I have not tested but the logic is correct
SELECT MEDICATION_NAME
FROM TABLE where
COMPANY_ID=(SELECT
MAX(counted)
FROM ( SELECT COUNT(*) AS counted FROM TABLE ) AS counts);
I have table that looks like this:
id rank
a 2
a 1
b 4
b 3
c 7
d 1
d 1
e 9
I need to get all the distinct rank values on one column and count of all the unique id's that have reached equal or higher rank than in the first column.
So the result I need would be something like this:
rank count
1 5
2 4
3 3
4 3
7 2
9 1
I've been able to make a table with all the unique id's with their max rank:
SELECT
MAX(rank) AS 'TopRank',
id
FROM myTable
GROUP BY id
I'm also able to get all the distinct rank values and count how many id's have reached exactly that rank:
SELECT
DISTINCT TopRank AS 'rank',
COUNT(id) AS 'count of id'
FROM
(SELECT
MAX(rank) AS 'TopRank',
id
FROM myTable
GROUP BY id) tableDerp
GROUP BY TopRank
ORDER BY TopRank ASC
But I don't know how to get count of id's where the rank is equal OR HIGHER than the rank in column 1. Trying SUM(CASE WHEN TopRank > TopRank THEN 1 END) naturally gives me nothing. So how can I get the count of id's where the TopRank is higher or equal to each distinct rank value? Or am I looking in the wrong way and should try something like running totals instead? I tried to look for similar questions but I think I'm completely on a wrong trail here since I couldn't find any and this seems a pretty simple problem that I'm just overthinking somehow. Any help much appreciated.
One approach is to use a correlated subquery. Just get the list of ranks and then use a correlated subquery to get the count you are looking for:
SELECT r.rank,
(SELECT COUNT(DISTINCT t2.id)
FROM myTable t2
WHERE t2.rank >= r.rank
) as cnt
FROM (SELECT DISTINCT rank FROM myTable) r;
I am trying to do a simple query that will count the number of reviews for each company in a database table as follows grouped by name
e.g reviews table
id company_id review
1 1 Great
2 1 Ok
3 1 Bad
4 2 Nice
So this would return company id 1 with 3, and company id 2 with 1. Any ideas on the easiest solution
select company_id, count(company_id) from tablename group by company_id
try
SELECT company_id,Count(1) FROM reviews GROUP BY company_Id;
I'm running contests on my website. Every contest could have multiple entries. I want to retrieve if only the MAX value of votes has a duplicate.
The table is as follows:
contest_id entry_id votes
1 1 50
1 2 34
1 3 50
2 4 20
2 5 55
3 6 53
I just need the query to show me that contest 1 has a duplicate MAX value without additional information.
I tried this but didn't work:
SELECT MAX(votes) from contest group by contest_id having count(votes) > 1
SELECT a.contest_ID
FROM contest a
INNER JOIN
(
SELECT contest_id, MAX(votes) totalVotes
FROM contest
GROUP BY contest_id
) b ON a.contest_ID = b.contest_ID AND
a.votes = b.totalvotes
GROUP BY a.contest_ID
HAVING COUNT(*) >= 2
SQLFiddle Demo
This finds the max votes value per contest and counts the entries with that number of votes.
It then displays contest with more than one hit.
SELECT contest_id
FROM contests
WHERE votes=(
SELECT MAX(votes) FROM contests c WHERE c.contest_id=contests.contest_id
)
GROUP BY contest_id
HAVING COUNT(*) > 1;
SQLfiddle for testing.
You could do it by first selecting the maximum number of votes for each contest ID in a subquery, and then joining against the results (demo on SQLFiddle):
SELECT contest_id, votes
FROM contest
JOIN (
SELECT contest_id, MAX(votes) AS votes
FROM contest GROUP BY contest_id
) AS foo USING (contest_id, votes)
GROUP BY contest_id
HAVING COUNT(*) > 1
The nice thing about doing it like this is that it's an independent subquery, so MySQL only needs to rub it once.
Ps. Yes, this is basically identical to JW's answer, but I figured I'd leave it up anyway to show the slightly different syntax I used for the join.
I have table with statistical data.
I'm trying to count the records grouped by a version, the problem is that the records have another criteria (ref) and should counted only once (per ref).
stats sample data to illustrate the issue:
id stat_date ref version
-------------------------
1 2012-01-25 1 A
2 2012-01-25 2 B
3 2012-01-25 3 A
4 2012-01-26 8 B
5 2012-01-26 2 B
6 2012-01-26 3 B <-- version has been updated for ref=3
Simple counting would return
SELECT COUNT(*),version FROM stat GROUP BY version
1,A
5,B
The problem here is that only the last record with ref=3 (id=6) should be counted and (id=3) has to be ignored.
So the question is, how can I filter row (id=3) from the query?
I can't figure out what I should insert as condition in the subquery
SELECT COUNT(*),version FROM stats
WHERE stat_date BETWEEN "2012-01-25" AND "2012-01-26"
AND id = (SELECT MAX(id) FROM stats WHERE <condition>)
GROUP BY 2
The expected result would be:
1,A (since id=3 is ignored)
3,B (since the first id=2 is ignored and only id=5 is taken into account)
It doesn't matter which row you ignore. All you want is to count ref once.
SELECT COUNT(DISTINCT ref),version FROM stat GROUP BY version
Edit:
Your example should also count the 2 rows where ref = 2 once as well according to your logic.
This answers satisfies the questions as posed even if it doesn't solve your unknown real problem
Try:
SELECT COUNT(*),version
FROM stats s1
WHERE stat_date BETWEEN "2012-01-25" AND "2012-01-26" AND
not exists (SELECT null
FROM stats s2
WHERE s1.ref = s2.ref and s2.id>s1.id)
GROUP BY version
EDIT: Not guaranteed to always work:
SELECT COUNT(*),version from
(select * from
(select * from stats
where stat_date BETWEEN "2012-01-25" AND "2012-01-26"
order by ref, id desc) s0
group by ref) s1
group by version
SELECT COUNT(*),version FROM stats
WHERE stat_date BETWEEN "2012-01-25" AND "2012-01-26"
AND id = (SELECT MAX(s.id) FROM stats s WHERE s.id = id)
GROUP BY version
ORDER BY stat_date