SQL help - Summary with data from values listed - sql

Trying to figure this out, but am stuck... I am looking to calculate distinct emails from a system that holds email address from multiple companies, and want to summarize by the companies that are values within a column..
Current query:
select count(*), count(EMAIL), count(distinct EMAIL), count(company) from "email_db"
GROUP BY(company);
I can not get the company values to show up, just the counts, so ideally the results would be:
Company XYZ 2
Company ABC 1
What statement should replace count(company) to show the actual values within the field to summarize it by?
select count(*), count(EMAIL), count(distinct EMAIL), count(company) from "email_db"
GROUP BY(company);
Any help would be greatly appreciated to get me to the correct results... This is simple enough to do in Excel.

You seem to want to add company to the select:
select company, count(*), count(EMAIL), count(distinct EMAIL)
from "email_db"
group by company;
The three columns are:
count(*) -- number of matching rows for the company, even if email is null.
count(email) -- number of matching rows where email is not null.
count(distinct email) -- number of different emails.

Related

Group By Clause, Do i have to call all rows what i using in Select?

Do I need to put all the column names in group by which I have select put in select?
for example in this simple query :
Select
CustomerID,
CompanyName,
ContactName,
ContactTitle,
City,
Country
From
Customers
Group By
Country,
CompanyName,
ContactName,
ContactTitle,
City,
Country,
CustomerID
I have to allways call same amount Group By what i used in Select?
If you're just selecting columns and you want the returned records to discard the exact duplicate rows? Then there are 2 methods.
1) group by
2) distinct
Your query doesn't use any of the aggregate functions like f.e. COUNT, MIN, MAX, SUM, ...
So your query could use DISTINCT instead of a GROUP BY.
select DISTINCT
CustomerID, CompanyName, ContactName, ContactTitle, City, Country
from Customers
But if CustomerID is a primary key, then CustomerID would already make the result unique.
So then this query doesn't need a GROUP BY or a DISTINCT to only get unique records.
select CustomerID, CompanyName, ContactName, ContactTitle, City, Country
from Customers
Note that one could have both DISTINCT and GROUP BY in the same query. But that's just pointless. A GROUP BY already enforces the uniqueness, so adding a DISTINCT to make them unique would just make the query slower for no reason.
As for the why all the columns in that select also have to be listed in the GROUP BY? Some databases, f.e. MySql can be more tolerant about not having to group on all columns. But it's a rule from one of the SQL Standards. So most databases enforce that. It's to avoid potential misleading results.
GROUP BY x, y means you want one result row per x and y. So if you have a table with bills, you could group by year and month for instance and thus get the number of bills (count(*)) and the total (sum(amount)) per month.
So the question is what rows do you want to see. A row per company (with the number of their customers) maybe? A row per city? The GROUP BY clause contains exactly those columns mentioned.
Your GROUP BY clause does exactly nothing, as select customers and you group by customer ID (which should be the customer table's primary key).

SQL query to count number of rows with same value

I have this table data:
I want to perform an sql query which will give me the total number of distinct loan applications per city.
So for example, I would expect this output
City Wexford
Loans 1
City Waterford 1
Loans 1
City Galway
Loans 3
Any idea what kind of query I need to perform to get the count of distinct loans for each city?
I would guess, probably a COUNT (Distinct ID) with GROUP BY. Something like this:
SELECT city, COUNT(DISTINCT LoanApplicationID) as Loans
FROM tableName
GROUP BY city
Here is another approach for this question. I am adding this because using DISTINCT may cause performance issues for another example, especially for large databases. Good to keep in mind.
select city,count(LoanApplicationID) as Loans
from (
select LoanApplicationID, city
from tablename
group by LoanApplicationID, city
) t
group by city

Identifying a Distinct Count for a Column Without Using Group By

I'm trying to figure out how to get the distinct count of something that's conditional and doesn't use group by. I've got a table that has columns as seen here:
Employeeid, Training_Course_name, CompletedDate
Some of the courses have the word Rope in them.
I want to take the number of completed courses per person with the word "Rope" in the title and divide it by the number of unique courses there are that have the word rope in the title. If there are 15 unique course names that have the word rope in the title, regardless of who they're assigned to, I want to come up with that number and have it divided into the number of completed rope courses per person.
You can use conditional aggregation:
select count(distinct case when Training_Course_name like '%rope%'
then Training_Course_name
end) as courses_with_rope
This will help you to solve your problem
Declare #UniqueCourses As TABLE(Course As VarChar(32))
Select #UniqueCourses = Training_Course_Name
From
(SELECT DISTINCT Training_Course_Name
FROM Employess
WHERE Training_Course_Name LIKE '%Rope%') A
SELECT
EmpId,
(SELECT COUNT(1) FROM Employees innerEmployees
WHERE innerEmployees.EmpId = outerEmployees.EmpId AND
innerEmployees.CompletedDate is not null
) AS Completed Courses
From Employees outerEmployees
You can get the course with the word rope in them with this query
SELECT Employeeid, Training_Course_name, CompletedDate
FROM Table_Name_You_Did_Not_Say
WHERE Training_Course_name LIKE '%rope%'
And a distinct count like this
SELECT Employeeid, Training_Course_name, CompletedDate,
count(distinct Training_Course_name) as distinct_names
FROM Table_Name_You_Did_Not_Say
WHERE Training_Course_name LIKE '%rope%'
Anything "by employee id" would require a group by -- so what exactly is your requirement?

How to display all columns associated with duplicate emails in SQL server 2008

I've done some research on looking for a way to filter duplicate emails so all columns display the data associated with these duplicate emails, but can't find an answer to help me with this.
I currently have data pulled using the following code:
SELECT
Email, COUNT(Email) AS dup_count
FROM
[cem_farmers_masterinvitelist].[dbo].InviteList
GROUP BY
Email
HAVING
(COUNT(Email) > 1)
Order by Email
It then gives me xxxxxx amount of rows. I then want to be able to pull any data (columns) that are associated with these duplicate emails -and just the duplicates.
SELECT * FROM [marks_party_MasterInvite] .[dbo].[InviteList]
WHERE
Email in(Select Email FROM [marks_party_MasterInvite].[dbo].[InviteList] GROUP BY Email HAVING COUNT(Email)>1)
I know I am doing something wrong, because the row count doesn't match.
So any help would be greatly appreciated!
Thanks guys,
You want to use window functions. The following adds the count to each row. Then you can use a where filter to get all the columns:
SELECT il.*
FROM (select il.*, count(*) over (partition by email) as cnt
from [cem_farmers_masterinvitelist].[dbo].InviteList
) il
where cnt > 1
Order by Email
The counts don't match because when you fetch every row, you are going to get duplicates. In the first query, you are getting distinct emails.
Join the source table, to the knowledge you just generated about your source:
SELECT *
FROM [cem_farmers_masterinvitelist].[dbo].InviteList src
INNER JOIN
(
SELECT
Email, COUNT(Email) AS dup_count
FROM
[cem_farmers_masterinvitelist].[dbo].InviteList
GROUP BY
Email
) qDupes
ON qDupes.email = src.email AND qDupes.dup_count > 1
Row count will be different, if you want to get count of all dupe records involved, you can use *WITH rollup *, witch will total the number for you... This number should match the row number of second Query...
SELECT
Email, COUNT(Email) AS dup_count
FROM
[cem_farmers_masterinvitelist].[dbo].InviteList
GROUP BY
Email WITH ROLEUP
HAVING
(COUNT(Email) > 1)
Order by Email

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