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

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

Related

count different column values after grouping by

Consider this table:
id name department email
1 Alex IT blah#gmail.com
1 Alex IT blah#gmail.com
2 Jay HR jay#gmail.com
2 Jay Marketing zou#gmail.com
If I group byid,name and count I get:
id name count(*)
1 Alex 2
2 Jay 2
With this query:
select id,name,count(*) from tb group by id,name;
However I would like to count only records that diverge from department,email, so as to have:
id name count(*)
1 Alex 0
2 Jay 1
This time the count for the first group 1,Alex is 0 because department,email have the same values (duplicated) , on the other hand 2,Jay is one because department,email has one different value.
If you meant "two different values" for "Jay", you can use distinct:
select id,name,count(*) from (SELECT distinct * FROM tb) group by id,name;
You can use count(*) - 1 to get similar results in your question.

SQL query all register but select only one of one column [duplicate]

This question already has answers here:
Get top 1 row of each group
(19 answers)
Closed 1 year ago.
Assume that I have one table looking like this:
ID
ClientID
Name
Country
1
JX100
John
Canada
2
JX100
John
Japan
3
JX690
Rob
EUA
4
PX301
Alice
France
And My query:
SELECT DISTINCT ClientID,Name,Country FROM CLIENTS
OUTPUT:
ClientID
Name
Country
JX100
John
Canada
JX100
John
Japan
JX690
Rob
EUA
PX301
Alice
France
I want to take that:
DESIRED:
ClientID
Name
Country
JX100
John
Canada
JX690
Rob
EUA
PX301
Alice
France
TL;DR
I just want to select one country for all Clients, I don't want to get repeated rows.
You can use row_number()over() with common table expression like below:
with cte as (
SELECT DISTINCT ClientID,Name,Country,row_number()over(partition by ClientID ,Name order by Country) rownumber FROM CLIENTS)
select * from cte where rownumber=1
Row_number()over(partition by ClientID ,Name order by Country) will generate a sequence for contries starting from 1 against each clientid and name. So when you will select rownumber=1 this query will select one country for a single clientid,name combination.

Limit column value repeats to top 2

So I have this query:
SELECT
Search.USER_ID,
Search.SEARCH_TERM,
COUNT(*) AS Search.count
FROM Search
GROUP BY 1,2
ORDER BY 3 DESC
Which returns a response that looks like this:
USER_ID SEARCH_TERM count
bob dog 50
bob cat 45
sally cat 38
john mouse 30
sally turtle 10
sally lion 5
john zebra 3
john leopard 1
And my question is: How would I change the query, so that it only returns the top 2 most-searched-for-terms for any given user? So in the example above, the last row for Sally would be dropped, and the last row for John would also be dropped, leaving a total of 6 rows; 2 for each user, like so:
USER_ID SEARCH_TERM count
bob dog 50
bob cat 45
sally cat 38
john mouse 30
sally turtle 10
john zebra 3
In SQL Server, you can put the original query into a CTE, add the ROW_NUMBER() function. Then in the new main query, just add a WHERE clause to limit by the row number. Your query would look something like this:
;WITH OriginalQuery AS
(
SELECT
s.[User_id]
,s.Search_Term
,COUNT(*) AS 'count'
,ROW_NUMBER() OVER (PARTITION BY s.[USER_ID] ORDER BY COUNT(*) DESC) AS rn
FROM Search s
GROUP BY s.[User_id], s.Search_Term
)
SELECT oq.User_id
,oq.Search_Term
,oq.count
FROM OriginalQuery oq
WHERE rn <= 2
ORDER BY oq.count DESC
EDIT: I specified SQL Server as the dbms I used here, but the above should be ANSI-compliant and work in Snowflake.

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

Count distinct same names

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