0

I am currently writing a Java program.

Brief description of the component:

I have an "Entries" table. This table has the following columns: Date (which is entered automatically when the user makes a double entry) Input (This is the double number from the user)

With 5 entries, for example, the program should now access the last 2 entries made by the user and reflect them in the program.

For example, the table looks like this:

Date --------- Entry

21.01.2022 -- 500

01.03.2022 -- 551

04.05.2022 -- 629

30.06.2022 -- 701

15.07.2022 -- 781

Then the program should give me the 701 and the 781.

What is the most sensible way to do this? It makes no sense to use the following "SQL statement": Select where date 06/30/2022 because it is no longer useful when the user makes a new entry.

Please help!!

  • 1
    Does this answer your question? [SQL Server SELECT LAST N Rows](https://stackoverflow.com/questions/4193705/sql-server-select-last-n-rows) – sorifiend Aug 11 '22 at 11:48
  • @sorifiend, MS SQL Server (which is discussed in link you attached) is **not** Oracle (which is database the OP is using) so ... I wouldn't say that it answers the question. "SQL" tag is related to the language, not database vendor. – Littlefoot Aug 11 '22 at 12:12
  • No this question is not helping. Thanks tho – singhKirat Aug 11 '22 at 13:32

2 Answers2

0

You can use the following SQL statement to select the last two rows:

select * from Entries order by date desc limit 2;
Rusu Dinu
  • 477
  • 2
  • 5
  • 16
  • This gives me the error: "ORA-00936: Expression missing". I assume "Where" is missing. A select must always have SELECT, FROM, WHERE. In your example we only have Select and From ...? – singhKirat Aug 11 '22 at 11:55
  • Oh? Since when is WHERE obligatory? `select * from dual` works perfectly fine, @singhKirat. Problem with *this* code is in `limit` clause - Oracle doesn't support it. Which database version do you use? – Littlefoot Aug 11 '22 at 11:58
  • I am using Oracle SQL Developer Version 21.4.2.018 Build 018.1706. – singhKirat Aug 11 '22 at 13:33
0
select Entry
  from your_table
 order by date desc -- show most recent entries above in the results
 fetch first 2 rows only; -- show first 2 records only

You might be running on an old DB (version less than 12) so "fetch" might not be introduced.

Try this then

select * 
  from (select Entry
          from your_table
         order by date desc)
 where rownum <= 2;

Answering to your question how to get a penultimate value

select * 
  from (select Entry, row_number() over(order by date_col desc) rn
          from your_table)
 where rn = 2;

the "rn = 2" condition will get you not the last date but a date before the last. Setting it to 1 will get you the row with most recent date

ekochergin
  • 4,109
  • 2
  • 12
  • 19
  • This also gives me the error: "ORA-00936: Expression missing". I assume "Where" is missing. A select must always have SELECT, FROM, WHERE. In your example we only have Select and From ...? – singhKirat Aug 11 '22 at 11:57
  • @singhKirat Nope, WHERE clause is not required. It has to have SELECT (to know what to select) and FROM (to know where to select from) What db version are you on? Another reason might be the column name. "date" is a reserved word and it would be better to rename it to, say, "date_col" – ekochergin Aug 11 '22 at 12:01
  • I am using Oracle SQL Developer Version 21.4.2.018 Build 018.1706. Nope "date" was just an example, i acctually am using another name. – singhKirat Aug 11 '22 at 13:34
  • @singhKirat SQL Developer is just a tool for connecting to the DB and performing queries. Please perform "select version from v$instance;" in order to get database version – ekochergin Aug 11 '22 at 14:13
  • Could you please repeat the statement maybe there is a typo. It says "ORA-00942: table or view does not exist" – singhKirat Aug 11 '22 at 14:32
  • I have tried on my machine and it is correct. Such an error means you have no access to that object. Check updated answer please – ekochergin Aug 11 '22 at 14:41
  • SELECT * FROM v$version; works for me. It says: Oracle Database 12c Enterprise Edition Release 12.1.0.2.0 - 64bit Production – singhKirat Aug 11 '22 at 14:44
  • Your updatet answer also says: ORA-00936: "missing expression".... I don't get it :( – singhKirat Aug 11 '22 at 14:46
  • @singhKirat Hmm, then I don't get it.. It works fine on my 12. Please post the real query you're trying to execute – ekochergin Aug 11 '22 at 14:47
  • I've tried all the different answers here and that i found on google, but I'm getting a variety of error messages: select * from TABLE_NAME order by date desc fetch next 2 rows only; ORA-00936: "missing expression" select * from TABLE_NAME order by date desc limit 2; same error select COLUMN_NAME from TABLE_NAME order by date desc fetch first 2 rows only; Same Error SELECT TOP 2 FROM TABLE_NAME ORDER BY Id DESC; ORA-00923: "FROM keyword not found where expected" – singhKirat Aug 11 '22 at 14:51
  • @singhKirat What are the real column names you're trying to select and sort by? – ekochergin Aug 11 '22 at 14:57
  • So i am not trying to sort anything. I just want the User to make an entry. Then the program is supposed to look at the last to entries and compare them with the new entry. In my table "counter" i have 2 columns: "counter_date" and "counter_entry". – singhKirat Aug 11 '22 at 15:14
  • I kinda figured it out: SELECT * FROM ( SELECT * FROM table_name ORDER BY date DESC) WHERE ROWNUM <= 2. This gives me the last two entries. But still i would like to have them seperated. When I type 1 instead of 2, i get the last entry but how can i get the penultimate entry – singhKirat Aug 11 '22 at 15:22
  • @singhKirat please check updated answer – ekochergin Aug 11 '22 at 18:56
  • Ok well everything works!! Thank you very very much @ekochergin – singhKirat Aug 12 '22 at 07:57
  • @singhKirat, no probs. Glad it helped. Please mark my answer as answer to your question. Thanks – ekochergin Aug 12 '22 at 09:51
  • Hey @ekochergin, i have an problem. I want to implement the final statement we had, in java and print it (console). When I try it gives me just a number: So my Java code is this: ConnectTodb daba = new ConnectTodb("password"); daba.connect(); System.out.println(db.getStatement().executeUpdate("SELECT * FROM (select entry, row_number() over(order by entrie_date desc) rn from entry) where rn = 1")); When I do this, it prints the nuber 0... When I do the same command (SELECT * FROM ... blablabla ....1) in my oracle sql Developer, it gives me the right number... – singhKirat Aug 12 '22 at 12:14
  • Hi @singhKirat It seems to be a java related issue. I can't help here as I have no java experience. It would be better to post another question – ekochergin Aug 12 '22 at 12:17