We help IT Professionals succeed at work.

TOP(1) query for Oracle AND Microsoft SQL Server

109 Views
Last Modified: 2018-12-27
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

CERTIFIED EXPERT
Most Valuable Expert 2012
Distinguished Expert 2019
Commented:
This problem has been solved!
(Unlock this solution with a 7-day Free Trial)
UNLOCK SOLUTION
Jim HornSQL Server Data Dude
CERTIFIED EXPERT
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
CERTIFIED EXPERT
Most Valuable Expert 2018
Distinguished Expert 2019

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.
CERTIFIED EXPERT
Most Valuable Expert 2012
Distinguished Expert 2019

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
CERTIFIED EXPERT
Most Valuable Expert 2018
Distinguished Expert 2019

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
CERTIFIED EXPERT

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.

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions