Question

I have a table:

Table1(DDate date,Deposit_Key Number , Amount Number)

some example data are:

DDate       Deposit_key        Amount
1/1/2019          1              10
1/2/2019          1              20
1/3/2019          1              30
1/4/2019          1              40
  ----           ---             ---
1/31/2019         1              100

I have data for one month and I need to retrieve the Amount for the last day of the month . I mean I need to have 100 . What is the best query possible for this? I'm new to oracle and I want to know whether there are better ways than using row_number() over(partition)? Thanks in advance

Was it helpful?

Solution

The Oracle built in function called LAST_DAY can help you here.

It can be used with dates or timestamps and an example based on your table description is:

SELECT * FROM TABLE1 
WHERE EXTRACT(DAY FROM DDATE) = EXTRACT(DAY FROM LAST_DAY(DDATE))
AND EXTRACT (MONTH FROM DDATE) = 1;

There are many ways to work with dates in Oracle but I like using EXTRACT as it works with dates and timestamps and means you don't have to use the TO_CHAR function.

Licensed under: CC-BY-SA with attribution
Not affiliated with dba.stackexchange
scroll top