Get first/last n records per group by - mysql

I have two tables : tableA (idA, titleA) and tableB (idB, idA, textB) with a one to many relationship between them. For each row in tableA, I want to retrieve the last 5 rows corresponding in tableB (ordered by idB).
I've tried
SELECT * FROM tableA INNER JOIN tableB ON tableA.idA = tableB.idA LIMIT 5
but it's just limiting the global result of INNER JOIN whereas I want to limit the result for each different tableA.id
How can I do that ?
Thanks

Much simplified and corrected Carlos solution (his solution would return first 5 rows, not last...):
SELECT tB1.idA, tB1.idB, tB1.textB
FROM tableB as tB1
JOIN tableB as tB2
ON tB1.idA = tB2.idA AND tB1.idB <= tB2.idB
GROUP BY tB1.idA, tB1.idB
HAVING COUNT(*) <= 5
In MySQL, you may use tB1.textB even if it is group by query, because you are grouping by the idB in the first table, so there is only single value of tB1.textB for each group...

I think this is what you need:
SELECT tableA.idA, tableA.titleA, temp.idB, temp.textB
FROM tableA
INNER JOIN
(
SELECT tB1.idB, tB2.idA,
(
SELECT textB
FROM tableB
WHERE tableB.idB = tB1.idB
) as textB
FROM tableB as tB1
JOIN tableB as tB2
ON tB1.idA = tB2.idA AND tB1.idB >= tB2.idB
GROUP BY tB1.idA, tB1.idB
HAVING COUNT(*) <= 5
ORDER BY idA, idB
) as temp
ON tableA.idA = temp.idA
More info about this method here:
http://www.sql-ex.ru/help/select16.php

Ensure your "B" table has an index on ( idA, idB ) for optimized order by purposes so for each "A" ID, it can quickly have the "B" order descending thus putting the newest to the top PER EACH "A" ID. Using the MySQL variables, every time the "A" ID changes, it resets the rank back to 1 for the next "A" id.
select
B.idA,
B.idB,
B.textB
#RankSeq := if( #LastAGroup = B.idA, #RankSeq +1, 1 ) ARankSeq,
#LastAGroup := B.idA as ignoreIt
from
tableB B
JOIN tableA A
on B.idA = A.idA,
(select #RankSeq := 0, #LastAGroup := 0 ) SQLVars
having
ARankSeq <= 5
order by
B.idA,
B.idB DESC

select * from tablea ta, tableb tb
where ta.ida=tb.idb and tb.idb in
(select top 5 idb from tableB order by idb asc/desc)
(asc if you want lower ids desc if you want higher ids)
less complicated and easy to include more conditions
if top clause is not present in mysql use limit clause (I don't have much knowledge abt mysql)

Related

How do I return 0 when my sql returns with no rows?

select a, count (b)
from table1 where b in ( select distict b from table2)
and table1.dated>=DATE('yy/mm/dd')
group by a;
In the above SQL, when I have count(b)>0 then it returns columns but when count=0 then no rows were returned
I did try UNION, NULLIF() and SELECT(SELECT()) as something but nothing worked.
I was expecting to get 0 returned if the count is equal to 0.
https://www.db-fiddle.com/#&togetherjs=2AkxeMUrPF
You could use:
select table1.a, count(DISTINCT table2.b)
from table1
LEFT JOIN table2
ON table1.b = table2.b
AND table1.dated>=DATE('yy/mm/dd') -- this comparision is simply incorrect
group by table1.a
We can ensure that a query returns a row by having the query guarantee it.
Here's an example that retrieves exactly one row from an inline view i.
Then an outer join to another inline view s that gets a distinct list of values.
And then and then another outer join to table1.
SELECT t.a
, COUNT(t.b) AS cnt
FROM ( SELECT 1 AS n ) i
LEFT
JOIN ( SELECT DISTINCT r.b
FROM table2 r
) s
LEFT
JOIN table1 t
ON t.b = s.b
AND t.dated >= ...
GROUP
BY i.n
, t.a
If inline view s returns no rows, the query should return
a cnt
---- ---
NULL 0

MySql query ordering

Here is my data. I want to take 6 rows, but I want all HeadlineCategoryId's to be unique in my result list. If I select the top 6 I would take 2 rows from HeadlineCategoryID 20 (6,2). Do you have any suggestions about it?
SELECT a.*
FROM tableName a
INNER JOIN
(
SELECT HeadlineCategoryID, MAX(Creation) max_date
FROM TableName
GROUP BY HeadlineCategoryID
) b ON a.HeadlineCategoryID = b.HeadlineCategoryID AND
a.Creation = b.max_date
ORDER BY a.Creation DESC -- << specify here how are you going to sort
LIMIT 6 -- the records you want to get
UPDATE 1
SELECT a.*
FROM tableName a
INNER JOIN
(
SELECT HeadlineCategoryID, MAX(NewsID) max_id
FROM TableName
GROUP BY HeadlineCategoryID
) b ON a.HeadlineCategoryID = b.HeadlineCategoryID AND
a.NewsID = b.max_id
ORDER BY a.Creation DESC -- << specify here how are you going to sort
LIMIT 6 -- the records you want to get
It looks like you want the six most recent records, but unique by HeadlineCategoryId. If so, this will work:
select top 6 NewsId, Creation, HeadlineCategoryId
from (select t.*,
row_number() over (partition by HeadlineCategoryId order by Creation desc) as seqnum
from t
) t
where seqnum = 1
As a note . . . This question originally indicated that it was using SQL Server, not MySQL. The solution in MySQL is not as simple. Here is one method with not exists:
select NewsId, Creation, HeadlineCategoryId
from t
where not exists (select 1
from t t2
where t2.HeadlineCategoryId = t.HeadlineCategoryId and
t2.id < t.id)
limit 6
The not exists portion is saying "where there is no other record with a larger id for a given headline category".

SQL - selecting userid which has max number of not null rows in a different table

I've 3 tables say A,B,C.
Table A has userid column.
Table B has caid column.
Table C has lisid and image columns.
one userid can have one or several caids.
one caid can have one or several lisids.
how do I select a userid which has maximum number of rows with image column as not null (in some lisids image column is blank and in some it has some value).
can someone please help.
Presumably, the ids are spread among the tables in a reasonable fashion. If so, the following should do this:
select b.userid, count(*)
from TableB b join
TableC c
on b.caid = c.caid
where c.image is not null
group by b.userid
order by count(*) desc
limit 1
The question in the comments is how you connect TableA to TableB and TableB to TableC. The reasonable approach is to have the userid in TableB and the caid in TableC.
Getting all the rows with the max requires a bit more work. Essentially, you have to join in the above query to get the list
select s.*
from (select b.userid, count(*) as cnt
from TableB b join
TableC c
on b.caid = c.caid
) s
(select count(*) as maxcnt
from TableB b join
TableC c
on b.caid = c.caid
group by b.userid
order by count(*) desc
limit 1
) smax
on s.cnt = smax.cnt
Other databses have a set of functions called window functions/ranking functions that make this sort of query much simpler. Alas, MySQL does not offer these.

Need a sequence number for rows in query [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Need a sequence number for every row in MySQL query
So I found this great use:
SELECT (#row:=#row+1) AS ROW, ID
FROM TableA ,(SELECT #row := 0) r
ORDER BY ID DESC
My tables look more like this:
SELECT (#row:=#row+1) AS ROW, ID , ColA, ColB, ColC
FROM TableA
JOIN TableB on TableB.ID = TableA.ID
JOIN TableC on TableC.ID = TableA.ID
,(SELECT #row := 0) r
ORDER BY ID DESC
The #row:=#row+1 works great, but I get the row ordered by the ID.
so I get something more like this:
ROW | ID
3 15
2 10
1 2
What I am after is:
ROW | ID
1 15
2 10
3 2
Note: I noticed that if I remove the JOINs I DO get the requested result (In Which ROW is the sequential number of each row, no matter the ORDER BY of ID)
It basically seems that the row number is evaluated before the ORDER BY takes place. I need the ORDER BY to take place after row was given.
Any idea regarding how I can achieve that, and what do the JOINS do that messes it up?
Thx!
I need the ORDER BY to take place after row was given.
Then try this:
SELECT *
FROM
(
SELECT (#row:=#row+1) AS ROW, ID , ColA, ColB, ColC
FROM TableA
JOIN TableB on TableB.ID = TableA.ID
JOIN TableC on TableC.ID = TableA.ID
,(SELECT #row := 0) r
) t
ORDER BY ROW ASC

Selecting two rows in a table which have the same data for a particular column

There is a column in a table(contracts) called service location. I have to show all the rows where the service locations matches any other row in the table.
Table Example
A B C
1 2 3
3 2 1
2 5 3
I require a query where the first and second rows will be returned based on a comparison on the second column. I am assuming I will need to use a HAVING COUNT(B) > 1
I came up with this
SELECT `contract_number`
FROM `contracts`
WHERE `import_id` = 'fe508764-54a9-41f7-b36e-50ebfd95971b'
GROUP BY `service_location_id`
HAVING COUNT(`service_location_id` ) >1
But it doesn't generate what I exactly need.
Having would do it, but you would need to use it like this
SELECT *
FROM Contracts
INNER JOIN
( SELECT B
FROM Contracts
GROUP BY B
HAVING COUNT(*) > 1 -- MORE THAN ONE ROW WITH THE SAME VALUE
) dupe
ON dupe.B = Contracts.B
Depending in your indexing you may find a self join performs better though:
SELECT DISTINCT t1.*
FROM contracts t1
INNER JOIN contract` t2
ON t1.B = t2.B
AND t1.A <> t2.A
SELECT *
FROM sheet1
WHERE C
IN (
SELECT C
FROM sheet1
GROUP BY C
HAVING COUNT( C ) >1
)
ORDER BY C
LIMIT 0 , 5000