Can i limit my result to a specific number in sql? - mysql

Select count(*) as c from casting where ord = 1 Group by actorid Order by count(*) DESC
The result is
15 15 14 13 12 10 7 7 7 7 5 5
Then i would like to get the result only greater than 10 ??how to do that thanks~~

SELECT count(*) AS c
FROM casting
WHERE ord = 1
GROUP BY actorid
HAVING c > 10
ORDER BY c DESC
You can consider HAVING the WHERE clause for GROUP BY aggregates.

SELECT COUNT(*) AS c FROM casting WHERE ord = 1 GROUP BY actorid HAVING COUNT(*) > 10 ORDER BY COUNT(*) DESC

HAVING c > 10
Place this between group by and order by

Related

Query to return to 4th to 8th rank in a leaderboard?

I have been given a leaderboard, I need to return the row correspond to 4th to 8th rank in a leaderboard?
id name score
1 gongy 3001
2 urandom 2401
3 eduardische 2477
4 Gassa 2999
5 bcc32 2658
6 Alex_2oo8 6000
7 mirosuaf 2479
8 Sparik 2399
9 thomas_holmes 2478
The query I have is not being accepted
select b.name from
(
select a.name as name ,a.score as score from
(
select name,score from leaderboard order by score desc limit 8
)a
order by a.score
limit 5
) b
order by b.score desc;
You can use LIMIT OFFSET
select id,
name,
score
from leaderboard
order by score desc
limit 4 offset 3 ; --- fetch 4 records, begin with record 4 (OFFSET 3)
https://dbfiddle.uk/Ys-3jC4L
Above query skips the first 4 rows and limit the result to 4 make it from 4th to 8th
select x.id,x.name,x.score
from
(
select id,name,score,
row_number()over(order by score desc)xcol
from your_table
)x where x.xcol >=3 and x.xcol<=8
May be something like this ?

Mysql Select X rows after specific match

I'm trying to write a select statement in MySQL to get 5 rows after I get my match then sum how many times those numbers were repeated.
Example:
Raw Table
id
number
1
1
2
0
3
9
4
14
5
11
6
0
7
3
8
4
9
10
10
9
11
0
12
5
13
3
14
11
15
0
I need to find every row with the number 0, then after that select and show 5 rows after and counting the appearance of the numbers.
How can I select the numbers and get the count as a result?
The result of the first select should be like this:
id
number
3
9
4
14
5
11
6
0
7
3
7
3
8
4
9
10
10
9
11
0
12
5
13
3
14
11
15
0
The result of the count for every number in the last query.
Number
Count
9
2
14
1
11
2
0
3
3
3
4
1
10
1
5
1
This is a demo to get expected results:
Select numbers:
SELECT id, number
FROM (
SELECT b.id, b.number, ROW_NUMBER() OVER(PARTITION BY a.id ORDER BY b.id ASC) r
FROM numbers a
JOIN numbers b ON a.id < b.id
WHERE a.number = 0
) t WHERE t.r <= 5
ORDER BY id
Count numbers:
WITH n AS (
SELECT id, number
FROM (
SELECT b.id, b.number, ROW_NUMBER() OVER(PARTITION BY a.id ORDER BY b.id ASC) r
FROM numbers a
JOIN numbers b ON a.id < b.id
WHERE a.number = 0
) t WHERE t.r <= 5
)
SELECT number, COUNT(*) counts
FROM n
GROUP BY number
Sample data:
CREATE TABLE numbers (
id INT PRIMARY KEY auto_increment,
number INT NOT NULL
);
INSERT INTO numbers ( number ) VALUES (1),(0),(9),(14),(11),(0),(3),(4),(10),(9),(0),(5),(3),(11),(0);
https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=1fe71080cfb27680eb2a37b721e5de2d
Update for MySQL v5.7
SELECT n.*
FROM numbers n
JOIN (
SELECT a.id, SUBSTRING_INDEX(GROUP_CONCAT(b.id ORDER BY b.id SEPARATOR ','), ',', 5) selections
FROM numbers a
JOIN numbers b ON a.id < b.id
WHERE a.number = 0
GROUP BY a.id
) t ON FIND_IN_SET(n.id, t.selections)
ORDER BY n.id
SELECT n.number, COUNT(*) counts
FROM numbers n
JOIN (
SELECT a.id, SUBSTRING_INDEX(GROUP_CONCAT(b.id ORDER BY b.id SEPARATOR ','), ',', 5) selections
FROM numbers a
JOIN numbers b ON a.id < b.id
WHERE a.number = 0
GROUP BY a.id
) t ON FIND_IN_SET(n.id, t.selections)
GROUP BY n.number
ORDER BY n.number
https://dbfiddle.uk/?rdbms=mysql_5.7&fiddle=3be09acab5cd696ec4b01585eb5c32ed

How to fetch specific number of row on basis of score

Have to show on specific count . Means i have to show max score value 4 times then less than that score to 3 times and so on
I have table like:-
ID Score
1 1
2 1
3 1
4 1
5 2
6 2
7 2
8 3
9 3
10 4
11 4
12 4
and I am expecting output like:-
Score
1
1
1
2
2
3
4
Try this:
SELECT Score
FROM
(SELECT a.Score, a.ID, count(*) as RN FROM TableName a
JOIN TableName b ON a.Score = b.Score AND a.ID >= b.ID
GROUP BY a.Score, a.ID) T
WHERE RN>1
Result:
SCORE
1
1
1
2
2
3
4
4
An example in SQL Fiddle.
Explanation:
In the inner query, we are selecting a row number partitioned by ID and order by score. And then we select Score from the inner query whose Row number is greater than 1 (inorder to omit 1 record).
SELECT x.*
FROM my_table x
JOIN my_table y
ON y.marks = x.marks
AND y.id <= x.id
GROUP
BY x.marks
, x.id
HAVING COUNT(0) <= MAX(4-x.marks)
ORDER
BY marks DESC,id;

MySQL ranking query

Query:
SELECT u.user_id,r.totalVotes votes,r.totalPoints rating,#row:=#row+1 rank
FROM mismatch_user u
LEFT JOIN ratingItems r ON u.user_id=r.uniqueName,
(SELECT #row:=0) pos
ORDER BY votes DESC,rating DESC
Output:
user_id votes rating rank
2 2 10 2
6 2 9 6
3 2 5 3
1 1 5 1
4 1 5 4
27 1 5 27
9 0 0 9
The ranking miserably not telling me the truth and it's basing on the user_id. Can anyone help me?
Does this help?
select r.*, #row:=#row+1 rank
from
(SELECT u.user_id,r.totalVotes votes,r.totalPoints rating
FROM mismatch_user u
LEFT JOIN ratingItems r ON u.user_id=r.uniqueName
) r
join (SELECT #row:=0) pos
ORDER BY r.votes DESC, r.rating DESC
You're generating a sequence and that can lead to tricky behavior.
If your query is correct, the safest way to sort by rank is now to embed it in another SELECT:
SELECT *
FROM (
SELECT u.user_id,r.totalVotes votes,r.totalPoints rating,#row:=#row+1 rank
FROM mismatch_user u
LEFT JOIN ratingItems r ON u.user_id=r.uniqueName,
(SELECT #row:=0) pos
ORDER BY votes DESC,rating DESC) T
ORDER BY rank;

MySQL Max Count without Order By

I have the following MySQL line:
SELECT age, count(*) AS total FROM pacient WHERE age BETWEEN 20 AND 40 GROUP BY age ORDER BY age and I need to add an additional column to it that shows ONLY the max value of the count(*) for every row. For example:
13 2 7
18 2 7
23 5 7
24 7 7
26 6 7
32 3 7
38 1 7
41 1 7
46 4 7
This would be 3 columns and the 3rd column shows 7 since 7 was the highest number in the second column where the count(*) is made.
Here the solution:
select age,
count(*),
(select max(c) from
(select count(*) as c from pacient where age between 20 and 40 group by age) as x) as t
from pacient
where age between 20 and 40
group by age
order by age;
Have you tried to wrap your query with another query? something like
SELECT A.age, A.total, MAX(A.total) as max_value FROM (
SELECT age, count(*) AS total
FROM pacient
WHERE age BETWEEN 20 AND 40
GROUP BY age ORDER BY age) as A
GROUP BY A.age, A.total
select
p.Age,
count(*) CountPerAge,
max(ar.AllRecs) AllRecs
from
pacient p,
( select count(*) AllRecs
from pacient p2
where p2.age between 20 and 40 ) ar
where
p.age between 20 and 40
group by
p.age
By doing a join to the second "subselect" with no join condition, it will give a Cartesian result... Since it is a count with no group by, it will always return a single record and thus be joined to all age rows otherwise. The MAX() of the value is no problem since it is the only record will just be returned as-is.
It is always good to use SQL VIEWS instead of using sub queries. Because VIEW will be having already compiled result.
CREATE VIEW subqueryView
SELECT age, count(*) AS total
FROM pacient
WHERE age BETWEEN 20 AND 40
GROUP BY age ORDER BY age
SELECT A.age, A.total, MAX(A.total) as max_value FROM (SELECT FROM subqueryView) as A
GROUP BY A.age, A.total