TOP(1) query for Oracle AND Microsoft SQL Server

I have two tables. One called EMAILQUEUE and another called EMAILQUEUE_STATUS. EMAILQUEUE contains information about emails sent or to be sent. EMAILQUEUE_STATUS contains a record of the status of each item in EMAILQUEUE. I need to get a list of all items in EMAILQUEUE whose most recent status from the EMAILQUEUE_STATUS table is "Ready to send". If I was using an Oracle database I could use something like this:
SELECT ... FROM EMAILQUEUE_STATUS WHERE STATUS = 'Ready' AND ROWNUM = 1 ORDER BY TIME_STAMP DESC

Open in new window

For Microsoft SQL Server I could use something similar but different due to differences in syntax:
SELECT TOP(1) ... WHERE STATUS = 'READY' ORDER BY TIME_STAMP DESC

Open in new window

Here's the catch. I need a single query statement that will work with the same syntax for both Oracle and Microsoft SQL Server. I'm thinking some kind of group by or partition might be the way to go but I'm not quite figuring it out. Anyone have any ideas?

Oh and by the way, this needs to be compatible with Oracle 11g or later and Microsoft SQL Server 2012 or later. Unfortunately, Oracle 11g doesn't support CROSS APPLY so that option is out. :(
LVL 22
Russ SuterSenior Software DeveloperAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

slightwv (䄆 Netminder) Commented:
>>If I was using an Oracle database I could use something like this:

No you couldn't.  ROWNUM is assigned BEFORE the order by.  It is valid syntax and will run but won't give you what you think it will.

For cross database try something like:
SELECT col1,col2 FROM (
SELECT col1,col2, row_number() over(order by time_stamp desc) rn FROM EMAILQUEUE_STATUS WHERE STATUS = 'Ready'
) mydata
where rn=1

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Jim HornSQL Server Data DudeCommented:
>I need a single query statement that will work with the same syntax for both Oracle and Microsoft SQL Server.
Why?  This is not a realistic expectation that for every situation there will be SQL code that works in both databases.
Scott PletcherSenior DBACommented:
I think this will do it:

SELECT ...
FROM EMAILQUEUE_STATUS
WHERE STATUS = 'Ready'
ORDER BY TIME_STAMP DESC
/* OFFSET ... [optional, of course] */
FETCH NEXT 1 ROWS ONLY

I guess sometimes ANSI standards do actually help.
SolarWinds® IP Control Bundle (IPCB)

Combines SolarWinds IP Address Manager and User Device Tracker to help detect IP conflicts, quickly identify affected systems, and help your team take near instantaneous action. Help improve visibility and enhance reliability with SolarWinds IP Control Bundle.

slightwv (䄆 Netminder) Commented:
>>I think this will do it:  ... FETCH NEXT 1 ROWS ONLY

I think that syntax is a new 12c feature and not available in 11g.
Scott PletcherSenior DBACommented:
I'm certain you're right.  I'm a SQL DBA only now.  I knew SQL supported it, so I Googled it for Oracle, but I have no idea what version it started in.  [The last time I was an Oracle DBA was for Oracle8.]
Russ SuterSenior Software DeveloperAuthor Commented:
@slightwv

Thanks. That was pretty much it. I knew this already but was hitting a mental block for some reason. Thanks for jogging my memory.

@Jim Horn

I don't know what you're getting at here. I have no such expectation. I just knew that in this particular situation there was an answer. I just couldn't remember what the answer was.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Query Syntax

From novice to tech pro — start learning today.