-4

I need to get First Two Rows which are having C = '1', I'm ordering the query by TimeStamp Descending, I dont want the Last row to be Fetched by query

    A      B  C   TimeStamp
 ------------------------------------
  foo    one  1   20180405153936
  foo    two  1   20180405153936
  foo    two  2   20180405115417
  foo    one  2   20180405115053
  foo  three  1   20180405113923
ViKu
  • 235
  • 2
  • 14

2 Answers2

1

You can use the ROWNUM construct, but keep in mind that unless you wrap the ordering select, ROWNUM will bring back the rows in whatever order they were added to the table.

SELECT * FROM
    (SELECT * FROM 
         yourTable
     WHERE C = 1 
     ORDER BY "TimeStamp" DESC)
WHERE ROWNUM <= 2 

EDIT

Given the added information, the following will probably work better than using a ROWNUM.

SELECT * FROM 
    yourTable
WHERE 
    C = 1 AND 
    "TimeStamp" > (
        SELECT MIN("TimeStamp") FROM yourTable WHERE C = 1
    ) 
ORDER BY "TimeStamp" DESC
Peter Abolins
  • 1,520
  • 1
  • 11
  • 18
  • The thing is i'll be not knowing how many rows at the top which are satisfying the condition. – ViKu Apr 10 '18 at 06:25
0

You can use ROWNUM

SELECT * FROM
(SELECT * FROM 
     Table1 
 WHERE C=1
 ORDER BY "TimeStamp" DESC)
WHERE ROWNUM <= (SELECT COUNT(*) FROM Table1 WHERE C=1)-1

Demo

http://sqlfiddle.com/#!4/08ed98/11

Jay Shankar Gupta
  • 5,918
  • 1
  • 10
  • 27