473,398 Members | 2,812 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

Selecting Last 2

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
3 1388
<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
(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
<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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Joe Six-Pack | last post by:
Hi, Im having problems in randomly selecting an element in an array that has not been selected before.. in other words, I have an array of answers to questions, then I want to select 5, with one...
2
by: thomas | last post by:
Hello all Im having problems with selecting subelements from a dynamic created RTF converted to a node-set: i have hardcodet this example: the xml structure: <xsl:variable name="test">
3
by: james.dixon | last post by:
Hi I was wondering if anyone else had had this problem before (can't find anything on the web about it). I have three select elements (list boxes - from here on I'll refer to them as 'the...
4
by: Lucius | last post by:
Hello everyone, I have a query problem. I'll put it like this. There is a 'publishers' table, and there is a 'titles' table. Publishers publish titles (of course). Now I want to make a query (in...
0
by: Chris Kettenbach | last post by:
Good Morning, I have an xml file that's generated from a database. How do I select distinct values from a field in xslt and then loop through the records and produce output. Example ...
1
by: Cheryl | last post by:
I have problems selecting the attribute when I use the SelectNode function. <book> <title store='7456' >Nutshell</title> <author id='1'>Drayton</author> <xauthor publish = "1" publishtime =...
6
by: aaj | last post by:
Hi all I use a data adapter to read numerous tables in to a dataset. The dataset holds tables which in turn holds full details of the records i.e. keys, extra colums etc.. In some cases I...
2
Chittaranjan
by: Chittaranjan | last post by:
Hi All, I have a problem and hope I can get the better solution from here. I have a form written in HTML and I need to write that in perl so the main problem I am facing is that I need to...
9
by: ice | last post by:
Hello, I have a couple of tables. The client tables and the contacted tables. I am not sure how to start on this, what I need is a way to query all my clients then show any client that the...
2
by: n4nz | last post by:
I have a table in oracle that contains transaction data by item part number. This means for each part number there will be more than one transaction date, what i am trying to extract is the last...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.