How to calculate count of records by 2 columns - mysql

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;

Related

SQL get count of one column values and group by another

I have a table where I have 3 columns like so:
NAME ZONE_ID GROUP_ID
Mark 11 1
Mary 11 1
Mart 12 1
Mike 11 2
Kent 13 2
Now I want to count all the zone_id-s for a specific group. So the output for group 1 would be that there are 2 entries in zone 11 and 1 entry in zone 12 and for group 2 that there is 1 entry for zone 11 and one entry in zone 13.
It doesn't seem like it would be a very difficult query but I have hard time searching for it the right way and haven't found anything useful.
You need to group by group_id, zone_id:
select group_id, zone_id, count(*) counter
from tablename
group by group_id, zone_id

Mysql group by some field non distinct

I have some task.
I need to get this table. It consist of two tables. where table_2.name not distinct.
Please help me to make this query. Thanks!
id name1 id name2
1 Alex 2 Alexander
2 Alex 3 Alexan
4 Vlad 5 Vladimir
5 Vlad 6 Vladik
From two tables.
Table_1
id name
1 Alex
2 Pit
3 Vlad
And
Table_2
id id_table_1 real_name
1 1 Alexander
2 1 Alexan
3 2 Piter
4 3 Vladimir
5 3 Vladik
my query
select table_1.name,table_2.id,table_2.real_name
from table_1 join table_2
where table_1.id = table_2.id_table_1
if all you want is to combine duplicated rows, use SELECT DISTINCT.
If you need to combine rows that are duplicate in some columns, use GROUP BY but you need to to specify what to do with the other columns. You can either omit them (by not listing them in the SELECT clause) or aggregate them (using functions like SUM, MIN, and AVG)

How to filter rows from an ordered table?

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

Using sql, how do get rows with count for duplicates?

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

How to group by column, order by a different column, and limit each group to one row?

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);