Identifying a Distinct Count for a Column Without Using Group By - sql

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?

Related

SQL How many of a counted row is in another counted row?

I've been stuck on how to write a particular query for the following question:
How many employees are in how many businesses?
My end result should look like this:
EmployeeId Count
BusinessId Count
1
23473423
2
56245764
3
834456
So there are 23473423 businesses that have 1 employee, 23473423 businesses that have 2 employees, etc.
I have a table with a list of items including EmployeeId and BusinessId. A BusinessId can connect to many EmployeeIds.
So far I have the following code to get me employees per business
Select BusinessId,
Count(EmployeeId) as EIdCount
From Table
Group by BusinessId
Which gets me me a list of BusinessIds and how many EmployeeIds are attached to it.
BusinessId
EIdCount
23
2
24
5
25
1
26
3
But now I need to figure out how to further group it to where the BusinessId's can be grouped by the Grouped Counted Employee Ids. I've looked at subqueries, having by, and group but I am still at a loss how to progress this without running into an error.
Thank you for your help in advance!
Not sure if this is what you want:
Select EIdCount, Count(BusinessId)
From (
Select BusinessId,
Count(EmployeeId) As EIdCount
From Table
Group by BusinessId
) A
Group By EIdCount
Just use a subquery:
select
EIdCount,count(BusinessId) as [BusinessId Count]
from
(
--your original query/start
Select BusinessId,
Count(EmployeeId) as EIdCount
From Table
Group by BusinessId
--your original query/end
)t
group by EIdCount

SQL help - Summary with data from values listed

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.

SQL Query: Find the name of the company that has been assigned the highest number of patents

Using this query I can find the Company Assignee number for company with most patents but I can't seem to print the company name.
SELECT count(*), patent.assignee
FROM Patent
GROUP BY patent.assignee
HAVING count(*) =
(SELECT max(count(*))
FROM Patent
Group by patent.assignee);
COUNT(*) --- ASSIGNEE
9 19715
9 27895
Nesting above query into
SELECT company.compname
FROM company
WHERE ( company.assignee = ( *above query* ) );
would give an error "too many values" since there are two companies with most patents but above query takes only one assignee number in the WHERE clause. How do I solve this problem? I need to print name of BOTH companies with assignee number 19715 and 27895. Thank you.
You have started down the path of using nested queries. All you need to do is remove COUNT(*):
SELECT company.compname
FROM company
WHERE company.assignee IN
(SELECT patent.assignee
FROM Patent
GROUP BY patent.assignee
HAVING count(*) = (SELECT max(count(*))
FROM Patent
GROUP BY patent.assignee
)
);
I wouldn't write the query this way. The use of max(count(*)) is particularly jarring, but it is valid Oracle syntax.
Applying an aggregate function on another aggregate function (like max(count(*))) is illegal in many databases but I believe using the ALL operator instead and a join to get the company name would solve your problem.
Try this:
SELECT COUNT(*), p.assignee, c.compname
FROM Patent p
JOIN Company c ON c.assignee = p.assignee
GROUP BY p.assignee, c.compname
HAVING COUNT(*) >= ALL -- this predicate will return those rows
( -- for which the comparison holds true
SELECT COUNT(*) -- for all instances.
FROM Patent -- it can only be true for the highest count
GROUP BY assignee
);
Assuming you have Oracle, I thought about this a bit differently:
select
c.compname
from
company c
join
(
select
assignee,
dense_rank() over (order by count(1) desc) rnk
from
patent
group by
assignee
) p
on p.assignee = c.assignee
where
p.rnk = 1
;
I like this because is lets you find the any rank. For example, if you want the top 3 you would just change p.rnk = 1 to p.rnk <= 3. If you want 10th place, you just change it to p.rnk = 10. Adding the total count and rank into the results would be easy from here too. Overall I think it's more versatile.

help with query in DB2

i would like your help with my query.I have a table employee.details with the following columns:
branch_name, firstname,lastname, age_float.
I want this query to list all the distinct values of the age_float
attribute, one in each row of the result table, and beside each in the second field show the
number of people in the details table who had ages less than or equal to that value.
Any ideas? Thank you!
You can use OLAP functions:
SELECT DISTINCT age_float,
COUNT(lastname) OVER(ORDER BY age_float) AS number
FROM employee_details
COUNT(lastname) OVER(ORDER BY age_float) AS number orders rows by age, and returns employees count whose age <= current row age
or a simple join:
SELECT A.age_float, count(lastname)
FROM (SELECT DISTINCT age_float FROM employee_details) A
JOIN employee_details AS ED ON ED.age_float <= A.age_float
GROUP BY A.age_float

Selecting COUNT(*) with DISTINCT

In SQL Server 2005 I have a table cm_production that lists all the code that's been put into production. The table has a ticket_number, program_type, program_name and push_number along with some other columns.
GOAL: Count all the DISTINCT program names by program type and push number.
What I have so far is:
DECLARE #push_number INT;
SET #push_number = [HERE_ADD_NUMBER];
SELECT DISTINCT COUNT(*) AS Count, program_type AS [Type]
FROM cm_production
WHERE push_number=#push_number
GROUP BY program_type
This gets me partway there, but it's counting all the program names, not the distinct ones (which I don't expect it to do in that query). I guess I just can't wrap my head around how to tell it to count only the distinct program names without selecting them. Or something.
Count all the DISTINCT program names by program type and push number
SELECT COUNT(DISTINCT program_name) AS Count,
program_type AS [Type]
FROM cm_production
WHERE push_number=#push_number
GROUP BY program_type
DISTINCT COUNT(*) will return a row for each unique count. What you want is COUNT(DISTINCT <expression>): evaluates expression for each row in a group and returns the number of unique, non-null values.
I needed to get the number of occurrences of each distinct value. The column contained Region info.
The simple SQL query I ended up with was:
SELECT Region, count(*)
FROM item
WHERE Region is not null
GROUP BY Region
Which would give me a list like, say:
Region, count
Denmark, 4
Sweden, 1
USA, 10
You have to create a derived table for the distinct columns and then query the count from that table:
SELECT COUNT(*)
FROM (SELECT DISTINCT column1,column2
FROM tablename
WHERE condition ) as dt
Here dt is a derived table.
SELECT COUNT(DISTINCT program_name) AS Count, program_type AS [Type]
FROM cm_production
WHERE push_number=#push_number
GROUP BY program_type
try this:
SELECT
COUNT(program_name) AS [Count],program_type AS [Type]
FROM (SELECT DISTINCT program_name,program_type
FROM cm_production
WHERE push_number=#push_number
) dt
GROUP BY program_type
You can try the following query.
SELECT column1,COUNT(*) AS Count
FROM tablename where createddate >= '2022-07-01'::date group by column1
This is a good example where you want to get count of Pincode which stored in the last of address field
SELECT DISTINCT
RIGHT (address, 6),
count(*) AS count
FROM
datafile
WHERE
address IS NOT NULL
GROUP BY
RIGHT (address, 6)