By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,283 Members | 1,591 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,283 IT Pros & Developers. It's quick & easy.

Selecting Last 2

P: n/a
aut_id txt_head mem_body yon_approved
1 y
2 n
3 y
4 n
5 y
6 n
Trying to select last two records added that are approved and the last
two that are not approved... what i want to return is
5
3
6
4

SELECT Top 2 * FROM tbl_page WHERE Yon_approved = True ORDER BY aut_id
DESC UNION ALL ELECT Top 2 * FROM tbl_page WHERE Yon_approved = False
ORDER BY aut_id DESC
Returns
5
4
3
2

Feb 25 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
<mg*******@gmail.com> wrote:

I think you can do this with a _very_ ugly set of nested queries.

Try this:

SELECT [tbl_page].[aut_id]
FROM tbl_page
WHERE tbl_page.aut_id=(select max(aut_id) FROM tbl_page WHERE yon_approved =
"y") OR tbl_page.aut_id = (select max(aut_id) FROM tbl_page WHERE
yon_approved = "y" AND aut_id < (select max(aut_id) FROM tbl_page WHERE
tbl_page.yon_approved = "y"));

That should give you the "top two" approved entries.
If you get it working, try cloning it for the unapproved entries.
Your next challenge would be to splice both of them into a single query.

Good luck!
-Mark
Feb 26 '06 #2

P: n/a
(SELECT Top 2 * FROM tbl_page WHERE Yon_approved = True ORDER BY aut_id
DESC) UNION ALL (SELECT Top 2 * FROM tbl_page WHERE Yon_approved =
False
ORDER BY aut_id DESC)

Feb 26 '06 #3

P: n/a
<mg*******@gmail.com> wrote:
(SELECT Top 2 * FROM tbl_page WHERE Yon_approved = True ORDER BY aut_id
DESC) UNION ALL (SELECT Top 2 * FROM tbl_page WHERE Yon_approved =
False
ORDER BY aut_id DESC)

Ok, the "top" keyword is a new one to me. Thanks.
Feb 26 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.