Jump to content

[SOLVED] Select 2 most recent for each distinct value


Recommended Posts

I am trying to select the two most recent (highest) meta_id's for each distinct catcode.  I can only seem to extract one.  I am using MySQL 4.1.20.

 

My current query:

SELECT meta_id, catcode 
FROM pda_dbs 
GROUP BY catcode 
ORDER BY meta_id DESC;

 

Table structure:

mysql> describe pda_dbs;
+---------+---------+------+-----+---------+----------------+
| Field   | Type    | Null | Key | Default | Extra          |
+---------+---------+------+-----+---------+----------------+
| meta_id | int(11) |      | PRI | NULL    | auto_increment |
| catcode | int(10) |      | MUL | 0       |                |
| version | int(10) |      |     | 0       |                |
| oncheck | int(11) |      |     | 0       |                |
+---------+---------+------+-----+---------+----------------+

 

TIA.

 

Hi

 

Can't think of an elegant way to do it. This should do it but is pretty horrible:-

 

SELECT a.catcode, a.max_meta, b.almost_max_meta

FROM (SELECT catcode, MAX(meta_id) as max_meta FROM pda_dbs GROUP BY catcode) a

(SELECT catcode, MAX(meta_id) AS almost_max_meta FROM pda_dbs

WHERE meta_id NOT IN

LEFT OUTER JOIN (SELECT max_meta_id

FROM (SELECT catcode, MAX(meta_id) AS max_meta_id FROM pda_dbs GROUP BY catcode))

GROUP BY catcode) b

ON a.catcode = b.catcode

 

You could probably clean it up and remove the NOT IN.

 

All the best

 

Keith

I tried your query and received the following error.  It may be a version issue related to the sub-queries.  I can't really spot the correction for the error, any suggestions?

 

mysql> SELECT a.catcode, a.max_meta, b.almost_max_meta
    -> FROM (SELECT catcode, MAX(meta_id) as max_meta FROM pda_dbs GROUP BY catcode) a
    -> (SELECT catcode, MAX(meta_id) AS almost_max_meta FROM pda_dbs
    -> WHERE meta_id NOT IN
    -> LEFT OUTER JOIN (SELECT max_meta_id
    -> FROM (SELECT catcode, MAX(meta_id) AS max_meta_id FROM pda_dbs GROUP BY catcode))
    -> GROUP BY catcode) b
    -> ON a.catcode = b.catcode;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '(SELECT catcode, MAX(meta_id) AS almost_max_meta FROM pda_dbs
WHERE meta_id NOT ' at line 3

 

Thanks for the reply roopurt, but I have actually already tried that query (sorry I forgot to mention it) which throws the following version error:

 

ERROR 1235 (42000): This version of MySQL doesn't yet support 'LIMIT & IN/ALL/ANY/SOME subquery'

Eh.  This might help then:

http://searchoracle.techtarget.com/expert/KnowledgebaseAnswer/0,289625,sid41_cid580284,00.html

(scroll to bottom)

 

Looks like you may need multiple queries and / or temp tables.

Hi

 

Made a right mess of pasting that in a hurry didn't I.

 

Not tried it but I think this should work

 

SELECT a.catcode, a.max_meta, b.almost_max_meta
FROM (SELECT catcode, MAX(meta_id) as max_meta FROM pda_dbs GROUP BY catcode) a
LEFT OUTER JOIN (SELECT catcode, MAX(meta_id) AS almost_max_meta FROM pda_dbs 
WHERE meta_id NOT IN 
(SELECT max_meta_id 
FROM (SELECT catcode, MAX(meta_id) AS max_meta_id FROM pda_dbs GROUP BY catcode) Deriv1)) b
ON a.catcode = b.catcode

 

Basically doing 2 selects.

 

One subselect to get the max meta_id for each catcode.

 

The 2nd one is getting the max meta_id for each catcode that is IN a select of all the max meta_ids.

 

All the best

 

Keith

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.