How to get the maximum count, and ID from the table SQL - mysql

**castID**
nm0000116
nm0000116
nm0000116
nm0000116
nm0000116
nm0634240
nm0634240
nm0798899
This is my table (created as a view). Now I want to list the castID which has the most count (in this case which is nm0000116, and how many occurences/count it has in this table ( should be 5 times) and I'm not quite sure which query to use

try
Select CastId, count(*) countOfCastId
From table
Group By CastId
Having count(*)
= (Select Max(cnt)
From (Select count(*) cnt
From table
Group By CastId) z)

SELECT
MAX(E),
castId
FROM
(SELECT COUNT(castId) AS E,castId FROM [directors winning movies list] GROUP BY castId) AS t

You could just return the topmost count using LIMIT:
SELECT castID,
COUNT(*) AS Cnt
FROM atable
GROUP BY castID
ORDER BY Cnt DESC
LIMIT 1
;
However, if there can be ties, the above query would return only one row. If you want all the "winners", you could take the count from the above query as a scalar result and compare it against all the counts to return only those that match:
SELECT castID,
COUNT(*) AS Cnt
FROM atable
GROUP BY castID
HAVING COUNT(*) = (
SELECT COUNT(*)
FROM atable
GROUP BY castID
ORDER BY Cnt DESC
LIMIT 1
)
;
(Basically, same as Charles Bretana's approach, it just derives the top count differently.)
Alternatively, you could use a variable to rank all the counts and then return only those that have the ranking of 1:
SELECT castID,
Cnt
FROM (
SELECT castID,
COUNT(*) AS Cnt,
#r := IFNULL(#r, 0) + 1 AS r
FROM atable
GROUP BY castID
ORDER BY Cnt DESC
) AS s
WHERE r = 1
;
Please note that with the above method the variable must either not exist or be pre-initialised with a 0 or NULL prior to running the query. To be on the safe side, you could initialise the variable directly in your query:
SELECT s.castID,
s.Cnt
FROM (SELECT #r := 0) AS x
CROSS JOIN
(
SELECT castID,
COUNT(*) AS Cnt,
#r := #r + 1 AS r
FROM atable
GROUP BY castID
ORDER BY Cnt DESC
) AS s
WHERE s.r = 1
;

Related

get users before and after a specific user with its rank

I have a table like this:
id|name|points
1|Ralph|15
2|Dave|2
3|Mourphy|180
I need to get the user with id x and 5 users before and after him based on points rank:
I can retrive the user with
select *,rank() OVER (ORDER BY points DESC ) as rank from client where id = x;
How to retrive the others ?
Thank you
You already gave the answer inside your question.
It would be (Sql Server style)
DECLARE #myRank int
SELECT #myRank = rank() OVER (Order BY points DESC) FROM client WHERE id = x;
Select *, rank() OVER (Order BY points DESC) as rank
FROM client
HAVING rank between (#myRank - 5) and (#myRank +5);
If you want it in pure SQL, you'll have to work a little extra, but it's the same idea (just with sub-queries).
One method is to calculate the rank for "x" and to compare that to the rank for each row:
select c.*
from (select max(case when id = #x then rank end) over () as x_rank
from (select c.*, rank() OVER (ORDER BY score DESC ) as rank
from client c
) c
) c
where rank >= x_rank - 5 and rank <= x_rank + 5;
Note that this may not return exactly 11 rows if you have ties.
If you want exactly 5 before and after plus all rows with the same score:
with c as (
select max(case when id = #x then rank end) over () as x_rank
from (select c.*, rank() OVER (ORDER BY score DESC ) as rank
from client c
) c
)
(select c.*
from c
where rank < x_rank
order by rank desc
limit 5
) union all
(select c.*
from c
where rank = x_rank
) union all
(select c.*
from c
where rank > x_rank
order by rank asc
limit 5
) ;

Sql - count the number of occurence for each duplicate value

Edit: What I want is to rank the frequency of all duplicates. I tried to use the code suggested earlier:
SELECT
t.*,
#rn:=IF(#name = name,
#rn + 1,
IF(#name:=name, 1, 1)) freq
FROM
(SELECT
*
FROM
student
ORDER BY name) t
CROSS JOIN
(SELECT #name:=NULL, #rn:=0) t2;
This, however, were able to count the frequency but only if they are consecutively ordered. See image below:
Instead of being counted as 3, number 1 under id 6 was counted back to 1.
It seems like you need every occurrence of the name, so a simple group won't do. Try something like this:
SELECT s.name AS "Name of Student",
(SELECT COUNT(name) FROM student ss WHERE s.name = ss.name) AS "Frequency of Late"
FROM student s
SELECT
name, COUNT(*)
FROM
TableName
GROUP BY
name
This query should work for you.
Use user variables for this:
SELECT
t.*,
#rn:=IF(#name = name,
#rn + 1,
IF(#name:=name, 1, 1)) freq
FROM
(SELECT
*
FROM
student
ORDER BY name) t
CROSS JOIN
(SELECT #name:=NULL, #rn:=0) t2;
EDIT:
If you want to order by date and freq later, you can do:
select * from (
/* the above query */
) t order by the_date_column desc, freq;
EDIT 2:
For the updated question, see below:
SELECT
*
FROM
(SELECT
t.*,
#rn:=IF(#number = number, #rn + 1, IF(#number:=number, 1, 1)) freq
FROM
(SELECT
*
FROM
your_table
ORDER BY number) t
CROSS JOIN (SELECT #number:=NULL, #rn:=0) t2) t
ORDER BY id;
DEMO # SQLFiddle
This is your solution
SELECT
Student_Name AS StudentName,count(Student_Name) Total
FROM
Student_Table
GROUP BY
Student_Name`

How to write sql query to get items from range

I would like to get values without the smallest and the biggest ones, so without entry with 2 and 29 in column NumberOfRepeating.
My query is:
SELECT Note, COUNT(*) as 'NumberOfRepeating'
WHERE COUNT(*) <> MAX(COUNT(*))AND COUNT(*) <> MIN(COUNT(*))
FROM Note GROUP BY Note;
SELECT Note, COUNT(*) as 'NumberOfRepeating'
FROM Notes
GROUP BY Note
HAVING count(*) <
(
SELECT max(t.maxi)
FROM (select
Note, COUNT(Note) maxi FROM Notes
GROUP BY Note
) as t
)
AND
count(*) >
(
SELECT min(t.min)
FROM (select
Note, COUNT(Note) min FROM Notes
GROUP BY Note
) as t
)
try this code.
One method would use order by and limit, twice:
select t.*
from (select t.*
from t
order by NumberOfRepeating asc
limit 99999999 offset 1
) t
order by NumberOfRepeating desc
limit 99999999 offset 1;
Try this code,
Select * from Note where NumberOfRepeating < (select MAX(NumberOfRepeating) from Note ) AND NumberOfRepeating > (select MIN(NumberOfRepeating) from Note );
Here in the code, as in your table Note is the name of the table, and NumberOfRepeating is the column name, as in your table.
Try this. It should work
SELECT *
FROM ( SELECT Note, COUNT(*) as 'NumberOfRepeating'
FROM Notes
GROUP BY Note
ORDER BY NumberOfRepeating DESC
LIMIT 1, 2147483647
) T1
ORDER BY T1.NumberOfRepeating
LIMIT 1, 2147483647

MySQL query to exclude highest and lowest count

I have following table in MySql
Number
1
2
3
2
3
4
4
4
I would like to get numbers by count but with excluded highest and lowest count. This should be the result
Number Count
2 2
3 2
Please help me with this query.
Try this:
SELECT number, COUNT(*) cnt
FROM mytable
GROUP BY number
HAVING cnt <> (SELECT COUNT(*)
FROM mytable
GROUP BY number
ORDER BY COUNT(*) LIMIT 1)
AND
cnt <> (SELECT COUNT(*)
FROM mytable
GROUP BY number
ORDER BY COUNT(*) DESC LIMIT 1)
The two subqueries used in the HAVING clause return the minimum and maximum count. Hence the two predicates of the HAVING clause filter out the groups with the highest and lowest count.
A solution is using user variable and sub-query.
Query needed:
select Number, Count
from (
select Number, count(*) as Count, #rank := #rank+1 as rank
from t1 cross join (select #rank := 0) param
group by Number
order by Count ) t2
where rank not in (1, #rank);
Demo:
create table t1(Number int);
insert into t1 values(1),(2),(3),(2),(3),(4),(4),(4);
select Number, Count
from (
select Number, count(*) as Count, #rank := #rank+1 as rank
from t1 cross join (select #rank := 0) param
group by Number
order by Count ) t2
where rank not in (1, #rank);
Output:
mysql> select Number, Count
-> from (
-> select Number, count(*) as Count, #rank := #rank+1 as rank
-> from t1 cross join (select #rank := 0) param
-> group by Number
-> order by Count ) t2
-> where rank not in (1, #rank);
+--------+-------+
| Number | Count |
+--------+-------+
| 2 | 2 |
| 3 | 2 |
+--------+-------+
2 rows in set (0.00 sec)
select number,count(number)
from numbers
where number not in ((select max(number) from numbers),(select min(number) from numbers) )
group by number
sqlfiddle demo
First select all rows except the maximum and minimum numbers.
Then find the count(number) from the result set.
Query
select t.number, count(t.number) as `Count` from(
select * from your_table_name
where number not in (
select max(number) from your_table_name
)
and number not in (
select min(number) from your_table_name
)
)t
group by t.num;
SELECT column FROM tbl
LIMIT 10 OFFSET 20
SELECT MIN(taka) AS mintaka, MAX(taka) AS maxtaka
FROM (SELECT taka FROM tbl LIMIT 10 OFFSET 20);

How to select certain numbers of groups in MySQL?

I have the table with data:
And for this table I need to create pegination by productId column. I know about LIMIT N,M, but it works with rows and not with groups. For examle for my table with pegination = 2 I expect to retrieve all 9 records with productId = 1 and 2 (the number of groups is 2).
So how to create pegination by numbers of groups ?
I will be very thankfull for answers with example.
One way to do pagination by groups is to assign a product sequence to the query. Using variables, this requires a subquery:
select t.*
from (select t.*,
(#rn := if(#p = productid, #rn + 1,
if(#rn := productid, 1, 1)
)
) as rn
from table t cross join
(select #rn := 0, #p := -1) vars
order by t.productid
) t
where rn between X and Y;
With an index on t(productid), you can also do this with a subquery. The condition can then go in a having clause:
select t.*,
(select count(distinct productid)
from t t2
where t2.productid <= t.productid)
) as pno
from t
having pno between X and Y;
Try this:
select * from
(select * from <your table> where <your condition> group by <with your group>)
LIMIT number;