How can I count duplicates rows (where the date and names are the same) using a select statement?
Here is my table
id date name
1 01/02/12 sam
2 01/02/12 john
3 02/04/12 eddie
4 01/06/12 joe
5 01/02/12 john
6 01/02/12 john
7 02/04/12 eddie
8 01/05/12 eddie
9 01/07/12 joe
Result should be like this:
id date name count
1 01/02/12 sam 1
01/02/12 john 3
02/04/12 eddie 2
4 01/06/12 joe 1
8 01/05/12 eddie 1
9 01/07/12 joe 1
I need a third coloumn in result set which value will be count column. also i dont need the id if the count is more than 1 (i think that would be impossible anyways). I am using mysql.
Thanks for advice.
You can write:
SELECT MIN(id) AS id, date, name, COUNT(1) AS `count`
FROM table_name
WHERE ...
GROUP BY date, name
;
That will always give the least id of the group. If you specifically want the first field to be NULL when there are duplicates, then you can change MIN(id) to CASE WHEN COUNT(1) > 1 THEN NULL ELSE MIN(id) END, but it sounds like you don't care about that?
something like this:
select id, date, name, count(*)
from mytable
group by id, date, name
having count(*) > 1
select date, name, count(id) as counter
from mytable
group by date, name
having count(id) > 1
Related
I need to find the list of all people who have had a child with more than one person. I am using one table that has the person name, person ID. The person's ID also serves as their mother_ID and father_ID.
ID NAME Father ID Mother ID
1 Paul
2 Debbie
3 Jessie
4 Pam 1 3
5 Sue 1 3
6 Trish 1 3
7 Sarah 1 2
9 John
10 johnny 9 4
11 Ben 9 4
In the example above, I want to find Paul who has four children with two different people, Debbie and Jessie.
Try this:
select * from
your_table a where
(select count(distinct father_id, mother_id)
from your_table b where b.father_id=a.id or b.mother_id=a.id)>1;
See it run on SQL Fiddle.
You could use COUNT(DISTINCT col) > 1:
SELECT *
FROM table
WHERE Id IN (
SELECT Father_Id
FROM table
GROUP BY Father_Id
HAVING COUNT(DISTINCT Mother_id) > 1
UNION ALL
SELECT Mother_Id
FROM table
GROUP BY Mother_Id
HAVING COUNT(DISTINCT Father_Id) > 1
);
DBFiddle Demo
In MySQL I have a table.
Example:
id name type
1 Thomas 2
2 Thomas 2
3 Thomas 1
4 Paul 3
5 Paul 4
6 Paul 4
I need calculate same records by 2 columns.
Result for this example should be:
name type countOfRecords
Thomas 2 2
Thomas 1 1
Paul 3 1
Paul 4 2
Could you help me with this request?
Since you want records in your result set for each name and type <name,type> pair
you need to group by name and type.
SELECT
name,
type,
COUNT(*) countOfRecords
FROM your_table
GROUP BY name,type;
Note:
Group BY <some column> would generate a result set where number of rows = number of distinct / different / unique <some column>.
Same holds for multiple columns in GROUP BY clause.
This may be right solution:
SELECT name, type, COUNT(*) as countOfRecords
FROM your_table
GROUP BY name,type;
Here's the table. It's ordered by points (desc) and id
id name points
1 ed 10
1 ed 9
2 jim 14
2 jim 8
2 jim 4
3 mike 11
Here's the results i'm looking for:
id name points
1 ed 10
2 jim 14
3 mike 11
How can this be done? basically, i want to list only the highest point row for each name and filter other rows away.
You can try something like this: use the MAX() function
SELECT id, name, MAX(points)
FROM your_table
GROUP BY id, name
ORDER BY points desc
Try this:
select id,name,max(points) from table1 group by id
I have one voter table which contain large amount of data. Like
Voter_id name age
1 san 24
2 dnyani 20
3 pavan 23
4 ddanial 19
5 sam 20
6 pickso 38
I need to show all voter_name by Alphabetically and count them.Like
name
san
sam
s...
s...
dnyani
ddanial
d...
pavan
pickso
p..
p..
I try using count(voter_name) or GROUP BY.
But both not working for me..... Suppose table contain 50 voters details.
number of person name start with A=15,b=2, c=10,y=3 and so on.
Then how to count and show first 15 record of 'A' person, next 2 record of 'B' person and so on..
Give me any reference or hint..
Thanks in Advance.
It is as simple as this,
SELECT SUBSTRING(name,1,1) as ALPHABET, COUNT(name) as COUNT
FROM voter GROUP BY SUBSTRING(name,1,1);
This order names only:
SELECT `name` FROM `voter` ORDER BY `name` ASC
This counts each occurrence of the first letter and group them group them together
ex.:
Letter COUNT
------ -------
A 15
B 2
C 10
y 3
SELECT SUBSTR(`name`,1,1) GRP, COUNT(`name`) FROM `voter` WHERE
SUBSTR(`name`,1,1)=SUBSTR(`name`,1,1) GROUP BY GRP ORDER BY GRP ASC
Here you go!
If you need names and their counts in ascending order, then you can use:
SELECT
name, COUNT(*) AS name_count
FROM
voter
GROUP BY
name
ORDER BY
name ASC
Which will give the output like
name name_count
------------------
albert 15
baby 6
...
If you need to display all records along with their counts, then you may use this:
SELECT
voter_id, name, age, name_count
FROM
(
SELECT
name, COUNT(name) AS name_count
FROM
voter
GROUP BY
name
) counts
JOIN actor
USING (name)
ORDER BY
name
and you get the output as:
voter_id name age name_count
------------------------------------
6 abraham 26 2
24 abraham 36 2
2 albert 19 1
4 babu 24 4
15 babu 53 4
99 babu 28 4
76 babu 43 4
...
Check the SUBSTRING function of MySQL here
http://dev.mysql.com/doc/refman/5.5/en/string-functions.html#function_substring
And we can use a sub-query to achieve our result.
So using that, how about this
SELECT voter_id, name, age, COUNT(*) AS alphabet
FROM
(SELECT voter_id, name, age, SUBSTRING(name, 1, 1) AS first_letter FROM voter)
AS voter
GROUP BY first_letter
ORDER BY first_letter ASC
Name Score
Jim 1
Jim 2
Jim 4
Lisa 2
Lisa 5
Ted 1
Ted 2
Ted 3
How can i group by name, order by highest score, and only pick that one row? So The query would return 3 rows Jim 4, Lisa 5, and Ted 3.
To find the max score, you can GROUP BY name, and use the MAX function:
SELECT ns.Name, MAX(ns.Score) AS Score
FROM NameScore AS ns
GROUP BY ns.Name
ORDER BY ns.Name ASC
I made up the table name, since you did not provide one, switch that for your real table.
I think the following will work, but I haven't tested it:
SELECT Name, MAX(Score) FROM Table
GROUP BY Name
i think this is trust:
select from Table group by Name having MAX(Score);