I have three tables having following structure
Table Name : users
id name age
1 Alok 26
2 Ashok 28
3 Amit 25
Table Name : Departments
id name d_name
1 Alok Ops
2 Amit IT
3 Shekahr CS
I want duplicate name with total count as following using mysql query
total name
2 Alok
2 Amit
1 Ashok
1 Shekhar
Please help
Thanks in Advance.
Try this:
select count(*) as total,name from (
select name from users
union
all select * from deepartment ) as temp
group by name
Union all will merge your tables and with group by and count you should get the expected result.
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
I want to show only the biggest value from my table GROUPED BY NAME
I have the table:
ID name money
1 jim 100
2 aura 150
3 mike 200
4 jim 300
5 aura 450
6 mike 1000
mysql query:
SELECT *
FROM table
GROUP BY name
and result are only first 3 id and i want to show only the biggest value in money: id 4, 5 and 6
You have to use aggregate function MAX:
SELECT name, MAX(money) FROM table GROUP BY name
Use belowquery,might be you get your solution.
SELECT name,max([money]) as MaxSalary
FROM table
GROUP BY name
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
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