1

I have very limited SQL knowledge and need to report on the top most current row of data within a table. The table holds Effective Date, Information and a Renewal Date.

select DATA_KEY_ID, 
TO_DATE('1799/12/31', 'YYYY/MM/DD') + TO_NUMBER(RTRIM(DATA_VALUE)) as "EFFDATE", DATA_KEY_REF, DATA_KEY_ID  
from TABLENAME 
where SCREEN_REF = 'TEST' and FIELD_REF = (select REF from REF_TABLE where SCREEN_REF = 'TEST' and ID = 'EFFDTE')

This brings the correct effective date, however it also brings out all the rows that have been entered. I need to only bring out the top row.

Sorry if this is a bit random.

4
  • 1
    I don't see this as being random - it's merely a question about how to return the TOP(1) row.
    – Hannah Vernon
    Jan 21, 2016 at 15:06
  • 1
    You should edit your question to add the a tag for the type of platform, or RDBMS, you are using.
    – Hannah Vernon
    Jan 21, 2016 at 15:11
  • @Tina, I tagged with Oracle, considering the use of TO_NUMBER and TO_DATE functions. Please edit if you use a different DBMS. Jan 21, 2016 at 18:53
  • And when you say "the top most current row", what do you mean? The top, according to what order (which column should be used for the order)? Jan 21, 2016 at 19:11

3 Answers 3

1

The best syntax I have found is:

select * where  ( Conditions) and rownum = 1;

You can use order by with the rownum psudocolumn to get only the top row in the ordered query.

Select * from (
select * from ( Conditons ) order by column_1
) where rownum = 1;
1

Have you tried something like this (syntax is for SQL Server):

SELECT TOP(1) 
    DATA_KEY_ID
    , TO_DATE('1799/12/31', 'YYYY/MM/DD') + TO_NUMBER(RTRIM(DATA_VALUE)) as "EFFDATE"
    , DATA_KEY_REF
    , DATA_KEY_ID  
FROM TableName 
WHERE SCREEN_REF = 'TEST' 
    and FIELD_REF = (
            select REF 
            from REF_TABLE 
            where SCREEN_REF = 'TEST' 
                    and ID = 'EFFDTE'
            )

The TOP(1) clause just after the SELECT tells the database engine to return a single row. In order for this to return the same row each time it is executed, you should also include an ORDER BY clause at the end such as:

ORDER BY DATA_KEY_ID;

If you are using MySQL, the syntax would be something like:

SELECT  
    DATA_KEY_ID
    , TO_DATE('1799/12/31', 'YYYY/MM/DD') + TO_NUMBER(RTRIM(DATA_VALUE)) as "EFFDATE"
    , DATA_KEY_REF
    , DATA_KEY_ID  
FROM TableName 
WHERE SCREEN_REF = 'TEST' 
    and FIELD_REF = (
            select REF 
            from REF_TABLE 
            where SCREEN_REF = 'TEST' 
                    and ID = 'EFFDTE'
            )
ORDER BY DATA_KEY_ID
LIMIT 0,1;

If you specify the type of RDBMS you are using, that would help identify the actual syntax you should use.

For Oracle, which seems like the platform you may be using, you need to add a reference to ROWNUM to the WHERE clause, like:

SELECT *
FROM
  ( SELECT  
        DATA_KEY_ID
        , TO_DATE('1799/12/31', 'YYYY/MM/DD') + TO_NUMBER(RTRIM(DATA_VALUE)) as "EFFDATE"
        , DATA_KEY_REF
        , DATA_KEY_ID  
    FROM TableName 
    WHERE SCREEN_REF = 'TEST' 
        and FIELD_REF = (
                select REF 
                from REF_TABLE 
                where SCREEN_REF = 'TEST' 
                        and ID = 'EFFDTE'
                )
    ORDER BY DATA_KEY_ID
  ) x
WHERE ROWNUM = 1;
5
  • Good point, @ypercubeᵀᴹ - I always tend to think SQL Server is the standard.
    – Hannah Vernon
    Jan 21, 2016 at 18:43
  • You can assume it's Oracle and use FETCH FIRST instead of TOP. That would work in recent versions (of both Oracle and SQL Server)! Jan 21, 2016 at 18:45
  • Interesting - I believe FETCH in SQL Server is only applicable to cursors. Unless I'm missing something?
    – Hannah Vernon
    Jan 21, 2016 at 18:53
  • I'm talking about OFFSET - FETCH, available in SQL Server 2012 version and in Oracle 12. Jan 21, 2016 at 18:57
  • 1
    If you are going to use the rownum approach in Oracle, you'd need to have the order by nested a level below the rownum predicate. As written, your query gets 5 arbitrary rows and then sorts them. There is no guarantee that it gets the top 5 rows. Jan 21, 2016 at 19:08
1

First, you need to be very mindful of what you mean by "top row". In a relational table, rows are like balls in a basket. There is no inherent 'first' or 'top' until you specifically and explicitly include an appropriate ORDER BY in your query. Also be careful of the use of rownum. In oracle, the ROWNUM is applied to the rows as they are chosen for the result set. It does not mean anything about the rows as they exist on the table.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.