SQL select count distinct - sql

I want to know how many items of distinct name I have in my database.
When I use:
select count(distinct name) from products
I obviously gain only number of different, distinct names I have in my
database. I was experimenting with group by, but as a total beginner I
failed. I'll appreciate any help.

Group by name and use count() to get the counts for each group
select name, count(*)
from products
group by name

select count(name) as ct, name from products group by name

SELECT NAME, COUNT(*) FROM products GROUP BY name

Related

Filter by number of occurrences in a SQL Table

Given the following table where the Name value might be repeated in multiple rows:
How can we determine how many times a Name value exists in the table and can we filter on names that have a specific number of occurrances.
For instance, how can I filter this table to show only names that appear twice?
You can use group by and having to exhibit names that appear twice in the table:
select name, count(*) cnt
from mytable
group by name
having count(*) = 2
Then if you want the overall count of names that appear twice, you can add another level of aggregation:
select count(*) cnt
from (
select name
from mytable
group by name
having count(*) = 2
) t
It sounds like you're looking for a histogram of the frequency of name counts. Something like this
with counts_cte(name, cnt) as (
select name, count(*)
from mytable
group by name)
select cnt, count(*) num_names
from counts_cte
group by cnt
order by 2 desc;
You need to use a GROUP BY clause to find counts of name repeated as
select name, count(*) AS Repeated
from Your_Table_Name
group by name;
If You want to show only those Which are repeated more than one times. Then use the below query which will show those occurrences which are there more than one times.
select name, count(*) AS Repeated
from Your_Table_Name
group by name having count(*) > 1;

SQL count distinct

How can I do a count distinct selection? Basically I have all these charges and I want to count only the number of distinct employees involved with that charge. (I already have other fields for the group by)
SELECT COUNT(DISTINCT employeeid) FROM ....
Simple as: select count(distinct(field)) from table
Live sample: http://sqlfiddle.com/#!3/81379/2/3
You can put a DISTINCT in a COUNT (in TSQL):
SELECT COUNT(DISTINCT(employee)) ...

SQL To get Distinct Name and Number from table

Looking for sql to get distinct names and count of those names from a sql table:
Structure:
id
name
other details
Do I use distinct to get each group and then count through those to get:
name1 count(name1)
name2 count(name2)
etc
Thanks
Rob.
When you want a COUNT() or a SUM(), you're using an AGGREGATE FUNCTION based on a GROUP BY clause.
As GROUP BY brings together all records with the same values specified in the GROUP BY columns, you're already getting the same effect as DISTINCT.
Except that DISTINCT doesn't allow aggregates, and GROUP BY does.
SELECT
name,
COUNT(*) AS count_of_name
FROM
yourTable
GROUP BY
name
Try :
SELECT *, COUNT(*) FROM my_table GROUP BY name
Something like this?
select name,COUNT(name) FROM Persons GROUP BY name
In the end I used:
SELECT DISTINCT `school`,COUNT(`school`) AS cat_num FROM table GROUP BY school order by cat_num DESC

Count number of users from a certain country

I have a table of users, and in this table I have a country field telling where these people are from (i.e. "Sweden", "Italy", ...). How can I do a SQL query to get something like:
Country Number
Sweden 10
Italy 50
... ...
Users select their countries from a list I give to them, but the list is really huge so it would be great to have a SQL query that can avoid using that list, that is look in the DB and give back only those countries which are in the database, because for example I have nobody from Barbados, even if I have that option in the country select field of the signup form :)
Thanks in advance!
If the name of the country is in the Users table, try something like this:
SELECT Country, COUNT (*) AS Number
FROM Users
GROUP BY Country
ORDER BY Country
If the name of the country is in the country table, then you will have to join
SELECT Contries.CountryName, Count (*) AS Number
FROM Users
INNER JOIN Countries
ON Users.CountryId = Countries.CountryId
GROUP BY Countries.CountryName
ORDER BY Countries.CountryName
This will give what you want. But you might want to cache the result of the query. With a lot of users it's quite a heavy query.
SELECT
country,
COUNT(*)
FROM
users
GROUP BY
country
Perhaps a better idea is (assuming you don't need the counts) to do it like this:
SELECT
DISTINCT country
FROM
users
Sounds like you want something like this...?
SELECT Country, COUNT(*) AS Number
FROM Users
GROUP BY Country
This is pretty straightforward:
SELECT
Country, COUNT(*) AS 'Number'
FROM
YourTable
GROUP BY
Country
ORDER BY
Country
You just group your data by country and count the entries for each country.
Or if you want them sorted by the number of visitors, use a different ORDER BY clause:
SELECT
Country, COUNT(*) AS 'Number'
FROM
YourTable
GROUP BY
Country
ORDER BY
COUNT(*) DESC
If you want the count per country:
select country, count(*) from users group by country;
If you just want the possible values:
select distinct country from users;
SELECT BillingCountry, COUNT(*)as Invoices
FROM Invoice
GROUP BY BillingCountry
ORDER BY Invoices DESC

Count total number of callers?

I'm currently doing this query to find the guy who makes the most calls:
SELECT
`commenter_name`,
COUNT(*) AS `calls`
FROM `comments`
GROUP BY `commenter_name`
ORDER BY `calls` LIMIT 1
What I want now is to be able to find out how many total unique callers. I tried using DISTINCT but I didn't get anywhere.
SELECT COUNT(DISTINCT 'commenter_name') FROM 'comment';
SELECT DISTINCT `commenter_name` FROM `comments`
Select Count(*) From (Select DISTINCT commenter_name from comment) as CmtCnt;