Equals values with the last limited row - mysql

I use a limit query to get a list of the top 5 directors counting also the total of their works e.g.
a 137
b 120
c 90
d 76
e 52
It really works but what about if with the last row there are also some names with equal works. And it usally happens...
f 52
g 52
h 52...
the query is:
SELECT
T_people.person,
COUNT(ID_films) AS CountOfID_films
FROM
((MT_films
JOIN ST_peoplefilms ON ((MT_films.ID_films = ST_peoplefilms.ID_films)))
JOIN T_people ON ((T_people.ID_person = ST_peoplefilms.ID_person)))
GROUP BY T_people.person, T_people.man
HAVING (T_people.man = 1)
ORDER BY COUNT(ST_peoplefilms.ID_films) DESC
LIMIT 5
thank's a lot

Create a subquery that gives the top 5 totals. Then use what you have now (you can remove LIMIT 5 and the ordering) and apply INNER JOIN on the subquery with respect to the totals. That will filter away all non-relevant rows.

Related

How can I consolidate a SQL query as an integer?

The following SQL (PHP, MySQL) gives me rows with one id per row and the number of matches for each id, as the COUNT() for each.
SELECT ot.id, COUNT(ot.id)
FROM ot
JOIN tm on tm.mediaid = ot.id
WHERE tm.uploadedToS3 = 1
AND (
ot.something IN (1)
OR ot.somethingelse IN (1)
OR ot.anotherthing IN (1)
OR ot.morestuff IN (1)
OR ot.evenmorestuff IN (1)
)
GROUP BY ot.id
The result is something like...
ot.id COUNT(ot.id)
40153 4
40305 3
40309 35
40314 29
40315 12
40317 10
40318 16
40319 14
40324 154
40331 113
I would just like to know how many rows this query returns. I don't need any other information, just one integer. For the above, I am looking for the number of rows, i.e. I'd expect to just get the number 10.
How would I get this?
Thanks.
Use count(distinct) with no group by:
SELECT COUNT(DISTINCT ot.id)
FROM ot JOIN
tm
ON tm.mediaid = ot.id
WHERE tm.uploadedToS3 = 1 AND
(ot.something IN (1)
OR ot.somethingelseIN (1)
OR ot.anotherthing IN (1)
OR ot.morestuff IN (1)
OR ot.evenmorestuff IN (1)
)

sorting 2 Columns in mysql

i Have Following Mysql Query
SELECT DISTINCT
main_Table.id,
t1.percentage,
t2.percentage
FROM image_new main_Table
INNER JOIN (SELECT DISTINCT
image_colors_new.image_id,
image_colors_new.percentage
FROM image_colors_new
WHERE image_colors_new.s BETWEEN 80 AND 120 AND image_colors_new.h BETWEEN - 20 AND 20 AND image_colors_new.v BETWEEN 73 AND 103
GROUP BY image_colors_new.percentage
ORDER BY image_colors_new.percentage DESC) t1
ON main_Table.id = t1.image_id
INNER JOIN (SELECT DISTINCT
image_colors_new.image_id,
image_colors_new.percentage
FROM image_colors_new
WHERE image_colors_new.s BETWEEN - 20 AND 20 AND image_colors_new.h BETWEEN - 20 AND 20 AND image_colors_new.v BETWEEN 85 AND 115
GROUP BY image_colors_new.percentage
ORDER BY image_colors_new.percentage DESC) t2
ON main_Table.id = t2.image_id
This Gives me Following Out Put
id percentage percentage1
36888 32.975669099756 0.559610705596
54899 9.9722991689751 0.55401662049861
43584 9.9195710455764 9.3833780160858
61517 9.7938144329897 40.20618556701
78076 9.7267759562842 12.786885245902
52123 9.6916299559471 3.3039647577093
36378 9.5798319327731 18.655462184874
43820 9.5238095238095 10.31746031746
53579 9.4736842105263 5.6140350877193
but i want rows in following format i.e rows that have percentage with highest value must pop to top, from either of columns
id percentage percentage1
61517 9.7938144329897 40.20618556701
36888 32.975669099756 0.559610705596
36378 9.5798319327731 18.655462184874
78076 9.7267759562842 12.786885245902
43820 9.5238095238095 10.31746031746
43584 9.9195710455764 9.3833780160858
54899 9.9722991689751 0.55401662049861
52123 9.6916299559471 3.3039647577093
53579 9.4736842105263 5.6140350877193
It sounds like you want to order by the larger of the percentage values. Add:
order by greatest(t1.percentage, t2.percentage) desc;
This should actually go in the outer query. Don't get in the habit of doing order by in subqueries. It might usually work, but there are no guarantees (and this is particularly true in other databases in multi-threaded implementations).

Find contiguous numbers ranges with mysql (25.000+ table rows)

I need to find contiguous numbers ranges (min, max) from a set of "series" (only numbers).
I have written a SQL using #row_number but it works partially. If the row ID (int, primary, auto_increment) is not consecutive, it returns 2 result sets instead of just one.
Check this sqlfiddle to see how the query works.
So I need to find a way to do this not using #row_number or using it but with a condition to "bind" the 2 results into single one is the series are consecutive (even if the ID is not consecutive).
The correct result should be: (see this sqlfiddle)
MIN MAX PRODUCT_ID
220 230 20
1106 1108 18
1110 1110 18
1112 1120 18
Response time is also important.
Thanks
I assume that you're after something like this...
SELECT a.product_id
, a.series start
, MIN(c.series) end
FROM card a
LEFT
JOIN card b
ON b.product_id = a.product_id
AND b.series = a.series - 1
LEFT
JOIN card c
ON c.product_id = a.product_id
AND c.series >= a.series
LEFT
JOIN card d
ON d.product_id = a.product_id
AND d.series = c.series + 1
WHERE b.series IS NULL
AND c.series IS NOT NULL
AND d.series IS NULL
GROUP
BY a.product_id
, a.series;
http://sqlfiddle.com/#!2/32dc8/4
If performance remains an issue, then we can take another look at a solution with #variables

Join and Group by Clause gives wrong output

I have two table L with columns (Code, Qtr, Fy, Limit) and R with (Code, Qtr,Fy,Limit). I want to get sum of limit of left and right table group by code, Qtr an Fy
The following query runs with no error but gives wrong output, can anyone help me in getting right output. IF I use only one table it works fine. I guess problem is with join
select L.Code, L. Qtr, L.FY, sum(L.limit),sum(R.Limit)
from tbl L,tbl R Where
L.Code=R.Code AND
L.Qtr=R.Qtr AND
L.FY=R.FY
group by L.Code,L.Qtr,L.FY
Sample Data ( the table contains other column as well but here i m keeping only selected)
Tbl L
Code Qtr, Fy Limit
001 1 70 200
001 1 70 700
001 2 70 500
001 2 70 300
Table R
Code Qtr Fy Limit
001 1 70 1000
001 1 70 200
001 2 70 50
001 2 70 125
Result
Code Qtr Fy Sum(l.Limit) sum(R.Limit)
001 1 70 900 1200
001 2 70 800 175
I m Using Mysql
Try this query:
select code, qtr, fy, sum(lsum), sum(rsum)
from (
select L.Code, L.Qtr, L.FY, L.limit as lsum, 0 as rsum
from L
union all
select R.Code, R.Qtr, R.FY, 0 as lsum, R.limit as rsum
from R) as combined
group by code, qtr, fy
Using join in this case would be a wrong idea because it will create multiple records (one for each match between L and R) and then when you do a sum you get incorrect results.
The problem is indeed the join - specifically, you're running into problems because you are using a GROUP BY after the join, when the join criteria results in non-unique rows. Usually, the way to solve this is to group before the join:
SELECT L.code, L.qtr, L.fy, L.lim as L_lim, R.lim as R_lim
FROM (SELECT code, qtr, fy, SUM(lim) as lim
FROM L
GROUP BY code, qtr, fy) L
JOIN (SELECT code, qtr, fy, SUM(lim) as lim
FROM R
GROUP BY code, qtr, fy) R
ON R.code = L.code
AND R.qtr = L.qtr
AND R.fy = L.fy
(have a working SQL Fiddle example)
Note that this will only show results for rows that are in both tables. Also, LIMIT is a reserved word (in MySQL and some other RDBMSs), so you're better off avoiding that for a column name.

Mysql Count per column

I have the following query:
SELECT a.feeder_id, b.feeder_pr
FROM authors_article_feeders a
LEFT JOIN feeders b ON b.id = a.feeder_id
WHERE website_id =1
LIMIT 0 , 30
which results in:
feeder_id feeder_pr
18 2
18 2
18 2
18 2
32 6
What I need is to modify the above query so that it will manipulate this data so that the result would end up with a count of each feeder_pr, so in this case the result would be:
feeder_pr count
2 4
6 1
Any assistance is appreciated. If you have time please describe your solution so that I can learn from it while I'm at it.
Everything I've tried has ended in inaccurate results, usually with just one row instead of the expected 2.
You just need to add a GROUP BY And, you would not even need the joins
SELECT b.feeder_pr, COUNT(b.feeder_pr)
FROM feeders b
GROUP BY b.feeder_pr
SELECT b.feeder_pr, count(a.feeder_id) as count
FROM authors_article_feeders a
LEFT JOIN feeders b ON b.id = a.feeder_id
WHERE website_id =1
GROUP BY 1