Find Min in rows in SQL table - sql

I am trying to find all the clubs in my database that appear the least number of times in the database and display that club name and how many times it appeared. Below is my solution for what I tried
select club, MIN(num)
from
(select count(club) as num
from member
order by club)
But this code throws an error:
SQL0119N An expression starting with "CLUB" specified in a SELECT clause, HAVING clause, or ORDER BY clause is not specified in the GROUP BY clause or it is in a SELECT clause, HAVING clause, or ORDER BY clause with a column function and no GROUP BY clause is specified.
I am new to SQL - can someone please tell me what is wrong and how I can fix this?

You could use the fetch first n rows only syntax:
SELECT club, COUNT(*)
FROM members
GROUP BY club
ORDER BY 2 ASC
FETCH FIRST 1 ROWS ONLY

You're heading in roughly the right direction. You need to do a double-aggregate.
WITH Member_Count AS (SELECT club, COUNT(*) AS counted
FROM Members
GROUP BY club)
SELECT club, counted
FROM Member_Count
WHERE counted = (SELECT MIN(counted)
FROM Member_Count)

Related

SQL: Finding the Minimum aggregate using only count

I'm using DB2 for a project and looking to find which Group has the fewest members without using the min feature. My idea is to find all the groups and then subtract out any group which has more members from some other group thus leaving me with the group with that has no more members than any other group, i.e. the min.
So far I have
SELECT DISTINCT P.group as Group, count(P.id) as Count
FROM People P
EXCEPT
SELECT P.group, count(P.id)
FROM People P, People O
WHERE count(P.cid) > count(O.cid);
With a schema for People like
create table People (
group varchar(25) not null,
id smallint not null,
);
I am getting the following error:
SQL0119N An expression starting with "CLUB" specified in a SELECT clause,
HAVING clause, or ORDER BY clause is not specified in the GROUP BY clause or
it is in a SELECT clause, HAVING clause, or ORDER BY clause with a column
function and no GROUP BY clause is specified. SQLSTATE=42803
If you could help point out what I am doing wrong or the correct format for such a query it would be greatly appreciated!
find which Group has the fewest members
You can aggregate by group, order by member count, and fetch the top row only:
select p.group as grp, count(*) as cnt
from people p
group by p.group
order by count(*)
fetch first 1 rows only
You should try to use min(). Very straight forward. From your Error message, it seems like your HAVING clause is wrong so it would look into that.

How to select all columns and count from a table?

I'm trying to select all columns in table top_teams_team as well as get a count of values for the hash_value column. The sql statement here is partially working in that it returns two columns, hash_value and total. I still want it to give me all the columns of the table as well.
select hash_value, count(hash_value) as total
from top_teams_team
group by hash_value
In the sql statement below, it gives me all the columns, but there are duplicates hash_value being displayed which isn't what I want. I tried putting distinct keyword in but it wasn't working correctly or maybe I'm not putting it in the right place.
select *
from top_teams_team
inner join (
select hash_value, count(hash_value) as total
from top_teams_team
group by hash_value
) q
on q.hash_value = top_teams_team.hash_value
A combination of a window function with DISTINCT ON might do what you are looking for:
SELECT DISTINCT ON (hash_value)
*, COUNT(*) OVER (PARTITION BY hash_value) AS total_rows
FROM top_teams_team
-- ORDER BY hash_value, ???
;
DISTINCT ON is applied after the window function, so Postgres first counts rows per distinct hash_value before picking the first row per group (incl. that count).
The query picks an arbitrary row from each group. If you want a specific one, add ORDER BY expressions accordingly.
This is not "a count of values for the hash_value column" but a count of rows per distinct hash_value. I guess that's what you meant.
Detailed explanation:
Best way to get result count before LIMIT was applied
Select first row in each GROUP BY group?
Depending on undisclosed information there may be (much) faster query styles ...
Optimize GROUP BY query to retrieve latest row per user
I am assuming that you are getting duplicate columns when you say: "but there are duplicates hash_value being displayed"
select q.hash_value, q.total, ttt.field1, ttt.field2, ttt.field3
from top_teams_team ttt
join (
select hash_value, count(hash_value) as total
from top_teams_team
group by hash_value
) q
on q.hash_value = top_teams_team.hash_value
Try using COUNT as an analytic function:
SELECT *, COUNT(*) OVER (PARTITION BY hash_value) total
FROM top_teams_team;

SQLPLUS (Oracle) - Get MAX COUNT of GROUPBY

I need to identify which Month has the most entries. Ive used the TO_DATE function to format the date column to just the MONTH. Also, SELECT COUNT(*) in combination with the GROUP BY Clause I am able to return all records month and count attributes.
However, I need to be able to only return one row that is the MAX of the COUNT. IVE atempted to do so by adding a HAVING clause but returns an error. I suspect I need a subquery in here somewhere but am unsure as to how to go about it.
SELECT TO_CHAR(P.DATEREGISTERED,'MONTH') MONTH, COUNT(*) COUNT
FROM PET P
GROUP BY TO_CHAR(P.DATEREGISTERED,'MONTH')
HAVING COUNT = MAX(COUNT);
Another Attempt:
SELECT TO_CHAR(P.DATEREGISTERED,'MONTH') MONTH, COUNT(*) COUNT
FROM PET P
GROUP BY TO_CHAR(P.DATEREGISTERED,'MONTH')
HAVING COUNT(*) = (SELECT MAX(TO_CHAR(P.DATEREGISTERED,'MONTH')) FROM PET P);
In the query with alias, you are grouping by Month and getting a count of the number of records and you are checking whether that count is same as the maximum of the "date value" converted to month string. They are not even comparisons of the same type.
The query that you have provided in the answer correctly compares the count on both sides.
Another way to rewrite the query would be
select * from
(SELECT TO_CHAR(P.DATEREGISTERED,'MONTH') MONTH, COUNT(*) COUNT
FROM PET P
GROUP BY TO_CHAR(P.DATEREGISTERED,'MONTH') order by count(*) desc )
where rownum=1
Here we order the records in the subquery by descending order of the count and then getting the first row from that.
The bellow code works and returns the correct response. It is unclear to me as to why it works but the above attempts (w/ aliases) do not.
SELECT TO_CHAR(P.DATEREGISTERED,'MONTH') MONTH, COUNT(*) COUNT
FROM PET P
GROUP BY TO_CHAR(P.DATEREGISTERED,'MONTH')
HAVING COUNT(*) = (SELECT MAX(COUNT(*)) FROM PET P GROUP BY TO_CHAR(P.DATEREGISTERED,'MONTH'));

sql select 5 higest values

I'm trying to select the 5 rows with the highest count value
This is my query:
string sql = "SELECT top 5 count FROM Likes ORDER BY COUNT(*) DESC";
It's just throwing an error code that
Column 'Likes.count' is invalid in the select list because it is not
contained in either an aggregate function or the GROUP BY clause.
It's for a project I've got to present tomorrow...
On SQL Server, simply do this:
SELECT TOP 5 * FROM Likes ORDER BY [Count] DESC
This assumes that your Likes-table already contains a column named [Count] meaning that you don't need to count the records yourself (which is what COUNT(*) does).
You should not use COUNT(*) here for order by.
SELECT top 5 [count] FROM Likes ORDER BY [Count] DESC
count is a reserved word which is why you should stay clear of using them for column names. If you don't want to rename the column you can escape it, different dbms may effect who you do this. In ssms you would use square brackets.
string sql = "SELECT top 5 [count] FROM Likes ORDER BY COUNT(*) DESC";

Sort by count SQL reporting services

I have a simple query in a tabloid control that gets all the leads in one month. I then use the tabloid control to group them into lead source. And then I have an associated count column. I want to sort my report on the count descending, without doing it in the query. I keep getting an error saying you cannot sort on an aggregate.
Thanks.
you can do one more thing..
just write your query in subquery part and write order by clause in outer query.
(suppose you have group by query as follow-
select lead_source, count(*) cnt
from your_table
group by lead_source
)
so you can do as follow -
select lead_source, cnt from (
select lead_source, count(*) cnt
from your_table
group by lead_source
)
order by cnt
this your_table and group by column list you have to edit accordingly your table structure ..