mysql count group by having - mysql

I have this table:
Movies (ID, Genre)
A movie can have multiple genres, so an ID is not specific to a genre, it is a many to many relationship. I want a query to find the total number of movies which have at exactly 4 genres. The current query I have is
SELECT COUNT(*)
FROM Movies
GROUP BY ID
HAVING COUNT(Genre) = 4
However, this returns me a list of 4's instead of the total sum. How do I get the sum total sum instead of a list of count(*)?

One way would be to use a nested query:
SELECT count(*)
FROM (
SELECT COUNT(Genre) AS count
FROM movies
GROUP BY ID
HAVING (count = 4)
) AS x
The inner query gets all the movies that have exactly 4 genres, then outer query counts how many rows the inner query returned.

SELECT COUNT(*)
FROM (SELECT COUNT(*)
FROM movies
GROUP BY id
HAVING COUNT(genre) = 4) t

Maybe
SELECT count(*) FROM (
SELECT COUNT(*) FROM Movies GROUP BY ID HAVING count(Genre) = 4
) AS the_count_total
although that would not be the sum of all the movies, just how many have 4 genre's.
So maybe you want
SELECT sum(
SELECT COUNT(*) FROM Movies GROUP BY ID having Count(Genre) = 4
) as the_sum_total

What about:
SELECT COUNT(*) FROM (SELECT ID FROM Movies GROUP BY ID HAVING COUNT(Genre)=4) a

Related

how to use min as a condition in sql query

cat use min as condition
the where statement is where it breaks but i cant fix it
select category, count(*) as number_of_cats
from books
where number_of_cats > min(number_of_cats)
group by category
order by category;
Having + sub-query
select category, count(*) as number_of_books
from books
group by category
having count(*) > -- check the one whose count is STRICTLY greater then minimum
( select min (st.t) -- find the minimum of all categories
from
( select count(*) as t --find the count for all categories
from books
group by category
) st -- an alias to avoid parsing errors
)
Another option, but with this solution in case of ex-aequo only first category is removed:
select select category, count(*) as number_of_books
from books
where category not in (select bb.category
from books bb
group by bb.category
order by count(*) asc
limit 1)
group by category
You could use common table expressions here, e.g.:
WITH CategoryCount AS (
SELECT
category,
COUNT(*) AS number_of_books
FROM
books
GROUP BY
category),
MinBooks AS (
SELECT
MIN(number_of_books) AS min_number_of_books
FROM
CategoryCount)
SELECT
cc.*
FROM
CategoryCount cc
CROSS JOIN MinBooks m
WHERE
cc.number_of_books > m.min_number_of_books;

Is it possible in mysql to display the sum of two count() results obtained from two different "group by" criteria on the same table?

I have the data table as follows
GameTable
And this is the result i need
ResultTable
I tried this but iit wouldn't work
(
(select count() from game group by team_1)
+
(select count() from game group by team_2)
)
Use union all:
select Team_id, sum(Game_count) as 'Game_count'
from (
select Team_1 as 'Team_id', count(*) as 'Game_count'
from games
group by Team_1
union all
select Team_2, count(*)
from games
group by Team_2
) as q
group by Team_id

Select distincts of same column in mutiple tables sql

SQL novice here.
I have this schema:
What I need in plain English is:
"Out of the 2 columns, make one above the other in one column, and then count how many distincts values there is"
I've tried
SELECT COUNT (DISTINCT uid ) from nodes UNION SELECT COUNT (DISTINCT uid) from ways ;
SELECT distinct nodes.uid from nodes JOIN ways on nodes.uid = ways.uid ;
sqlite> SELECT COUNT (DISTINCT uid ) from nodes UNION SELECT COUNT (DISTINCT uid) from ways ;
1195
2182
sqlite> SELECT uid from nodes FULL OUTER JOIN ways on nodes.uid = ways.iud ;
Error: RIGHT and FULL OUTER JOINs are not currently supported
SELECT COUNT (DISTINCT iud) FROM (SELECT DISTINCT uid from nodes as uid UNION SELECT DISTINCT uid from ways as uid as subq);
SELECT count (distinct nodes.uid) from nodes JOIN ways on nodes.uid = ways.uid ;
takes ages and i'm not sure nodes.uid = ways.uid is the correct way to go
Any idea ?
I think i got it
SELECT COUNT (DISTINCT uid) from (SELECT DISTINCT uid from nodes UNION SELECT DISTINCT uid from ways) as subq ;
Since UNION returns a distinct set of the joining tables you dont need to use the DISTINCT keyword
SELECT COUNT(uid) Cnt
FROM (
SELECT uid
FROM nodes
UNION
SELECT uid
FROM ways
) t

show all data only group by specific rows : Select * from table group by column having column = 'value'

I use mysql. My table look like this:
Last I try to use this query
SELECT * FROM movie GROUP BY `group` HAVING cateogry = 'TV'
I want with this query result as: show all but only GROUP BY TV category, where category = 'TV'
I want this Result
But my query give me this result (HAVING in query work as WHERE clouse)
IF I use this QUERY
SELECT * FROM movie GROUP BY `group`
It give me this result
I want -> QUERY -> GROUP BY group (ID no 9 and ID 1,2,3 treat as different group name)
IF group has all same values BUT category='movie' (RETURN ALL ROWS
group by NOT APPLY)
IF group has all same values BUT category='TV' (RETURN 1 ROW group by APPLY)
You seem to want this query:
select m.*
from movie m join
(select `group`, min(id) as minid
from movie
group by `group`
) g
on m.id = g.minid;
SELECT min(ID) as ID, min(Name), `group`, Category
FROM movie
GROUP BY `group`, Category
ORDER BY ID
Have you tried the below? I think you are pretty close. As when you are grouping your 'group' t. You are also grouping the one whose category is movie as well. So you just need to create a separate group Category.
SELECT * FROM movie
WHERE group = 't'
GROUP BY group, Category
ORDER BY ID

Find the most popular in mysql

My table is
f1(drinker,shop)
The table has a list of drinkers and the shops which they visit.I need to find the most popular shop.I know I can do a simple group by of shops and order it in a descending way and limit the results to 1 but my doubt is what if two or more shop have the same number of drinkers in that case my query fails.I can't use limit 2 or 3 because I want a general working query and not a one specific to the data.I am running out of ideas.
Note:
Please don't show this way:
select shop from f1 group by shop order by count(*) desc limit 1
In that case, you have to compare the counts. Not so pretty with a group by query:
select shop
from f1
group by shop
having count(*) = (select max(cnt)
from (select count(*) as cnt
from f1
group by shop
) s
);
You could also do this as a subquery:
select shop
from f1 join
(select count(*) as cnt
from f1
group by shop
order by count(*) desc
limit 1
) f1max
group by shop
having count(*) = max(f1max.cnt);