SQLPLUS (Oracle) - Get MAX COUNT of GROUPBY - sql

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'));

Related

How to aggregate in SQL by having clause

The below is the query I'm using, I would like to add two more columns in the select statement which I have and noted them in the group by clause but the total results are different. The below script gives me the correct total count but I want to see the totals also for a column called transaction, and a column called employee. The total count should still be the same.
SELECT SUB.YEAR, SUM(SUB.TOTAL_COUNT), SUM(SUB.TOTAL_SPENT)
FROM (
SELECT YEAR, COUNT(*) AS TOTAL_COUNT, SUM($SPENTX) AS TOTAL_SPENT, CUSTOMERID
FROM TABLE A
WHERE YEAR = 2017
GROUP BY YEAR, CUSTOMERID
HAVING SUM($SPENTX)<=1000
) SUB
GROUP BY SUB.YEAR

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;

Find Min in rows in SQL table

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)

Creating variable in SQL and using in WHERE clause

I want to create a variable that counts the number of times each customer ID appears in the CSV, and then I want the output to be all customer IDs that appear 0,1,or 2 times. Here is my code so far:
SELECT Customers.customer_id , COUNT(*) AS counting
FROM Customers
LEFT JOIN Shopping_cart ON Customers.customer_id = Shopping_cart.customer_id
WHERE counting = '0'
OR counting = '1'
OR counting = '2'
GROUP BY Customers.customer_id;
SELECT Customers.customer_id , COUNT(*) AS counting
FROM Customers LEFT JOIN Shopping_cart on Customers.customer_id=Shopping_cart.customer_id
WHERE COUNT(*) < 3
GROUP BY Customers.customer_id;
The query groups all customer ids, and with count() we get the number of items in a group. So for your solution you call the group count() and say only the items where the group count is smaller then 3. Smaller then 3 includes (0,1,2). You can reuse the count() in the query.
You're probably thinking of HAVING, not WHERE.
For example:
select JOB, COUNT(JOB) from SCOTT.EMP
group by JOB
HAVING count(JOB) > 1 ;
While a tad odd, you may be specific about the HAVING condition(s):
HAVING count(JOB) = 2 or count(JOB) = 4
Note: the WHERE clause is used for filtering rows and it applies on each and every row, while the HAVING clause is used to filter groups.
You can apply a filter after the aggregation with the HAVING clause.
Please note that count(*) counts all rows, including empty ones, so you cannot use it to detect customers without any shopping cart; you have to count the non-NULL values in some column instead:
SELECT customer_id,
count(Shopping_cart.some_id) AS counting
FROM Customers
LEFT JOIN Shopping_cart USING (customer_id)
GROUP BY customer_id
HAVING count(Shopping_cart.some_id) BETWEEN 0 and 2;