TOP(1) query for Oracle AND Microsoft SQL Server

Russ Suter
Russ Suter used Ask the Experts™
on
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. :(
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2012
Distinguished Expert 2018
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

Jim HornSQL Server Data Dude
Most Valuable Expert 2013
Author of the Year 2015

Commented:
>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 DBA
Most Valuable Expert 2018
Top Expert 2014

Commented:
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.
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Most Valuable Expert 2012
Distinguished Expert 2018

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 DBA
Most Valuable Expert 2018
Top Expert 2014

Commented:
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 Developer

Author

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.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial