Count distinct same names - sql

I have table where have over 100k information.
ID FirstName
1 Bob
2 Bob
3 Tom
4 John
5 John
6 John
.. ....
Want procedure which will be count how much names are same, For example it must be like :
FirstName Count
Bob 2
Tom 1
John 3
Please help me to write it

It's very basic SQL example, group by column + aggregating results
select
FirstName, count(*)
from Table1
group by FirstName

Try this
select FirstName,Count(FirstName) From TableA group by FirstName

Try this
SELECT FirstName, COUNT(*) As Count
FROM YourTable
GROUP BY FirstName
HAVING COUNT(*) > 1
ORDER BY COUNT(*) DESC

Create Procedure GetCount
as
BEGIN
Select FirstName,Count(*) from tablename group by FirstName
END

Related

I need to get name and count value in table

I got a table like this
table userReport
id
name
surname
userId
resulId
1
test
test
123
1
2
test
test
123
1
3
test
test
123
2
4
test
test
123
3
5
john
test
124
3
6
john
test
124
2
7
john
test
124
1
8
james
test
125
3
9
james
test
125
2
My sql is trying to get name sunrame and count how many and also needed with resultId=1
SELECT
name+' '+ surname
,count(userId) AS userCount
FROM [userReport]
GROUP BY userId
it gives error
Column 'userReport.name' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
Column 'userReport.surname' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
also I want to add a where for resultId = 1 in the same select
So its be like joining the theese three selections in one
select name+' '+ surname from [userReport]
select count(userId) as userCount from [userReport] group by userId
select count(userId) as resultCount from [userReport] where resulId='1' group by userId
The quickest fix might be to just add the name and surname to the GROUP BY clause:
SELECT userId, name + ' ' + surname AS full_name, COUNT(*) AS userCount
FROM userReport
GROUP BY userId, name + ' ' + surname;
Note that I have also added userId to the SELECT clause. You might want to do this in the event that two users happen to have the same first and last name.

Finding distinct count of combination of columns values in sql

Currently I have a table this :
Roll no. Names
------------------
1 Sam
1 Sam
2 Sasha
2 Sasha
3 Joe
4 Jack
5 Jack
5 Julie
I want to write a query in which I get count of the combination in another column
Required output
Combination distinct count
-----------------------------
2-Sasha 1
5-Jack 1
5-Julie 1
Basically, you could group by these columns and use a count function:
SELECT rollno, name, COUNT(*)
FROM mytable
GROUP BY rollno, name
You could also concat the two columns:
SELECT CONCAT(rollno, '-', name), COUNT(*)
FROM mytable
GROUP BY CONCAT(rollno, '-', name)

SQL: How do you count the number of unique records?

I'm new to SQL and sorry if this q has been asked before - I couldn't phrase it properly.
Say I have a table that looks like this:
Name Call ID
Sally 1
Sally 2
Sally 3
Mike 4
Mike 5
Bob 6
Bob 7
I want to create a new table that looks like this:
Name No. of calls
Sally 3
Mike 2
Bob 2
Attempt
I assume I would do something like:
SELECT
Name,
COUNT(distinct Name) AS No. of Calls
FROM Table
Thanks.
You just need to group them and that's all.
SELECT
Name
COUNT(*) AS [No. of Calls]
FROM
Table
GROUP BY
Name
You're looking for GROUP BY:
SELECT Name, COUNT(*) AS 'No. of Calls'
FROM Table
GROUP BY NAME
ORDER BY COUNT(*) DESC

Best way to get distinct rows from a database

I have two tables tbPerson and tbComment with data such as below:
tbPerson
PersonID FirstName LastName
1 William Tell
2 Joe Smith
3 Sam Hampton
tbComment
CommentID PersonID CommentValue CommentPosition
45 2 This is my comment 100
46 2 This is my second comment 100
47 2 This is my third comment 100
48 1 A comment 101
49 3 This comment rules 102
50 3 A comment here 102
I need a query that only returns:
William Tell 101
Joe Smith 100
Sam Hampton 102
I figured it was something like below but this returns multiple rows. I only want the three rows.
SELECT FirstName, LastName, CommentPosition
FROM tbPerson
JOIN tbComment ON tbPerson.PersonID = tbComment.PersonID
GROUP BY FirstName, LastName, CommentPosition
For your data, your query should return only three rows. If you have multiple comments for a person, You need to choose which comment position you want.
The following would return the minimal value:
SELECT FirstName, LastName, MIN(CommentPosition) as CommentPosition
FROM tbPerson JOIN
tbComment
ON tbPerson.PersonID = tbComment.PersonID
GROUP BY FirstName, LastName;
Notice that CommentPosition was removed from the GROUP BY clause.
If comment position varies, try this:
SELECT FirstName, LastName, CommentPosition
FROM tbPerson
INNER JOIN (
SELECT PersonID, MIN(CommentPosition) AS CommentPosition
FROM tbComment
GROUP BY PersonID
) T1 ON tbPerson.PersonID = T1.PersonID
This will also handle the case of multiple people with the same name.
You can use SELECT DISTINCT FirstName, LastName, ...to get exactly what you want. Here's a tutorial on it.
http://www.mysqltutorial.org/mysql-distinct.aspx

Count entries that have different values in other column

Here is an (simplified) example of DB I have (sorry for the ulgy format, I don't know how to write tables):
Name | Num
John | 1
John | 3
John | 4
Dany | 2
Andy | 5
Andy | 5
I want to count how many people have more at least two different Numbers.
For instance, here, only john, because he has 1, 3 and 4.
Not Andy because he has twice 2 and no other one.
And obviously not Dany because he has only one entry.
Thank you very much.
Try this.
select count(name) from table group by name having count(distinct num)>1
Try this:
SELECT A.Name, COUNT(DISTINCT A.Num) cnt
FROM tableA
GROUP BY A.Name
HAVING cnt >= 2;
select count(*)
from (
select Name from Temp group by Name having count(distinct num) > 1
) as a
Try this:
select name from `table` group by name,num having count(num)>1