1

Following is the sql query (oracle) that I use to get the last record of the returning result of the select query. Is this the optimum way?

SELECT HAZMAT_PLACARD_NOTATION
        INTO v_pcn
        FROM HAZMAT_CLASS_IRF
        WHERE HAZMAT_CD = p_stcc_cd and ROWID = (SELECT MAX(ROWID) FROM HAZMAT_CLASS_IRF WHERE HAZMAT_CD = p_stcc_cd);

Following is an example result set of the select query where HAZMAT_CD = 4920111 But the last row with the HAZMAT_PLAYCARD_NOTATION value SPONTANEOUSLY COMBUSTIBLE should be retrieved. That's the objective

enter image description here

Asiri Liyana Arachchi
  • 2,663
  • 5
  • 24
  • 43

4 Answers4

2

Get last record of result set

There are many ways:

Oracle Pre-12c version:

  • ROWNUM in sub-query and ORDER BY in outer query
  • Analytic function

Oracle 12c version:

  • Top-n Row Limiting feature

Using ROWNUM

SELECT HAZMAT_PLACARD_NOTATION
INTO v_pcn
FROM
  (SELECT HAZMAT_PLACARD_NOTATION,
    ROWNUM rn
  FROM HAZMAT_CLASS_IRF
  WHERE HAZMAT_CD = p_stcc_cd
  ORDER BY        <sort_column> DESC
  )
WHERE rn =1;

Using Top-n Row Limiting feature

SELECT HAZMAT_PLACARD_NOTATION
 INTO v_pcn
  FROM HAZMAT_CLASS_IRF
  WHERE HAZMAT_CD = p_stcc_cd
  ORDER BY        <sort_column> DESC
FETCH FIRST 1 ROW ONLY;

Have a look at this answer for examples and detailed explanation.

Community
  • 1
  • 1
Lalit Kumar B
  • 47,486
  • 13
  • 97
  • 124
  • This is a good explanation. But what's the advantage using the query with ROWNUM you've provided over the query in my question. Is that the optimum way? – Asiri Liyana Arachchi Jul 15 '15 at 05:54
  • @AsiriLiyanaArachchi Using **ROWNUM** you scan the table only once, and apply ROWNUM on the result set of the sub-query . In your query, you are scanning the table twice, which is not optimal. – Lalit Kumar B Jul 15 '15 at 06:20
  • @AsiriLiyanaArachchi Please mark it as answered, would help others. – Lalit Kumar B Jul 15 '15 at 06:21
  • I've edited the question. I can't use order by . Given that the query I've mentioned is the only way I could come up with to get the last row. Any idea? – Asiri Liyana Arachchi Jul 15 '15 at 09:05
  • I've noticed that in your solution with ROWNUM there may be a mistake based on the question, what we want is the last record of a result set, so by doing the later filtering "WHERE rn =1" you are getting the row with the rownum value of 1 regardless of ordering in the sub-query. In this case using WHERE ROWNUM = 1 would give the wanted result. – Hyque Oct 27 '21 at 19:37
0

In mysql ,you can try this.

SELECT * 
FROM  `table` 
ORDER BY id DESC 
LIMIT 0 , 1
0
select * from
   ( 
     select * from table order by id desc
   )
where rownum = 1
Glorfindel
  • 21,988
  • 13
  • 81
  • 109
lookslikeanevo
  • 566
  • 1
  • 5
  • 14
0

In older sqlserver versions (<2014) you don't have the ROW_NUMBER at your disposal.

But you can easily use Top 1 and order by desc

select top 1 * from tblc2scdocumentcodes where C2scDocumentCode like '%.14.%' and C2scDocumentCode like '%DTP.SO.%' order by C2scDocumentCode desc

The query returns a resultset. The order of this resultset is 'reversed' and then limited to 1 record

real_yggdrasil
  • 1,213
  • 4
  • 14
  • 27