0

I have a query like so

Select * from tableA A 
LEFT JOIN
mydb.tableB B
ON A.pk = B.tableA_pk 
LEFT JOIN 
mydb.tableC C    
ON B.tableC_pk = C.pk
WHERE C.PK  IN ('325','305', '322')
ORDER BY A.pk desc;

This will return a result like so with potentially multiple rows for an entry where applicable

4, xxx, yyy, 325, 325, zzz <<<< most recent for this entry ( 'unique' key is tableC.pk, 325)
3, aaa, bbb, 325, 325, eee <<<< next most recent 
3, ccc, ddd, 322, 322, fff
2, eee, fff, 305, 305, rrr 
2, ggg, hhhh,322, 322, ttt
1, iii, jjj, 325, 325, uuu <<< oldest

Ideally i want the result to look like below, that is for each matching entry in the LIST ..IN ('325', '322') it should only return the most recent entry like below

4, xxx, yyy, 325, 325, zzz <<<< most recent
3, ccc, ddd, 322, 322, fff
2, eee, fff, 305, 305, rrr 

To explain further..

Select * from tableA A 
LEFT JOIN
mydb.tableB B
ON A.pk = B.tableA_pk 
LEFT JOIN 
mydb.tableC C    
ON B.tableC_pk = C.pk
WHERE C.PK  IN ('325')
ORDER BY A.pk desc;

I would get this

4, xxx, yyy, 325, 325, zzz <<<< most recent for this entry 
3, aaa, bbb, 325, 325, eee <<<< next most recent 
1, iii, jjj, 325, 325, uuu <<< oldest

But I need this:

4, xxx, yyy, 325, 325, zzz <<<< most recent for this entry ( 'unique' key is tableC.pk)

I have tried LIMIT 1 (I'm using MySQL) like below but this limits to 1 for the total request not 1 per searched entry in my query list.

Select * from tableA A 
LEFT JOIN
mydb.tableB B
ON A.pk = B.tableA_pk 
LEFT JOIN 
mydb.tableC C    
ON B.tableC_pk = C.pk
WHERE C.pk  IN ('325','305', '322')
ORDER BY A.pk desc
LIMIT 1;
8
  • Well you can achieve it using Row_Number() function with Partition By Clause. Sep 5, 2014 at 22:15
  • 1
    If you provide adequate information on your table structure and some sample data I'll tell you how to do this.
    – jpw
    Sep 5, 2014 at 22:15
  • @KrishnrajRana I don't think MySQL has those.
    – Laurence
    Sep 5, 2014 at 22:16
  • possible duplicate of Retrieving the last record in each group
    – Bulat
    Sep 5, 2014 at 22:16
  • Can you create the schema in SQL fiddle ? So that we can help you betterly. Sep 5, 2014 at 22:16

3 Answers 3

0

You can use variable by iterating it with 1 like so.

Select A.*, @i:=@i+1 AS rownum
from tableA A 
LEFT JOIN
mydb.tableB B
ON A.pk = B.tableA_pk 
LEFT JOIN 
mydb.tableC C    
ON B.tableC_pk = C.pk
WHERE C.PK IN ('325') and (SELECT @i:= 0) r
ORDER BY A.pk desc;

Note: I have not tested above query.

0
0

After reading suggestions and other answers I achieved it like so..many thanks for all help....

SELECT
DISTINCT C.PK
,B.PK
,B.tableC_pk
,B.tableA_pk
,A.pk

FROM

tableC C
INNER JOIN
tableB B
ON C.PK = B.tableC_pk
INNER JOIN
tableA A
ON A.PK = B.tableA_pk

INNER JOIN ( SELECT tableC_pk,MAX(PK) AS PK FROM tableB
WHERE tableC_pk IN ('325','322','111')
GROUP BY 1) D
ON C.PK = B.tableC_pk
AND B.PK = D.PK
-1

This question is asked several times a day. Here you will find an answer:

SQL Select only rows with Max Value on a Column

3
  • 1
    I flagged it as duplicate.
    – Bulat
    Sep 5, 2014 at 22:25
  • 2
    Oh ok. Then why post an answer? My understanding is that questions that should be closed aren't supposed to be answered.
    – jpw
    Sep 5, 2014 at 22:28
  • 3
    I guess. I have flagged several questions like this and none of them was closed, so I just answer with the link, if it gets accepted it will only confirm that its a dupe. Or author can remove the question, that will be even better.
    – Bulat
    Sep 5, 2014 at 22:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.